tags:

views:

44

answers:

2

I've written a proxy of sorts in Java (and Jetty). Anyway, it works great, but sometimes

...
final OutputStream realOs = res.getOutputStream();
...
InputStream is = url.openStream();
int i;
while ((i = is.read(buffer)) != -1) {
  realOs.write(buffer, 0, i);
}

fails with IOException. I've noticed that it mostly happens with large binary files, i.e. flash and Safari browser...

I'm puzzled...

+2  A: 

This can happen if the browser is closed (or the user cancels the download) while you're still writing to the socket. The browser closes the socket, so your OutputStream no longer has anything to write to.

Unfortunately it's hard to tell for sure whether this is really the case - in which case it's not an issue - or whether there's something more insidious going on.

Jon Skeet
I'm sure that the browser was not closed - in this particular case, i.e. unit testing, I'm the user
Demiurg
So how exactly are you testing this? What does the client see?
Jon Skeet
As I mentioned, you can think of my app as a proxy. The client is a normal browser that sees a normal web page, loaded through this proxy. The problem - sometimes some objects like images and flash embedded in the web page are not loaded.
Demiurg
@Demiurg: Have you tried running Wireshark to see what's going on at the network level?
Jon Skeet
Maybe I'll try... the problem is that it happens once in a while and only on pages with lots of embedded staff... so it is going to be painful... thanks anyway
Demiurg
Eventually I discovered that the problem was on the browser side, 10x!
Demiurg
A: 

Please post the text of the IOException. There are quite a few possibilities and each one has its own explanation and mitigation.

EJP