This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Howdy Ranchers. I have a simple servlet that produces a jpg based on posted data. If I simply return the image it always works. However, if I create the file on the server filesystem, and forward to a view that links to it, sometimes the image file is corrupt. If I reload a few times, I'll get a good version, and reloading again might produce a corrupt one. I'm wondering if one of you might notice something obvious in my code that might explain the inconsistent result, or have any other idea about why this is happening.
Hi Bear. Thanks much for the reply. The image data comes from a flash module, the source of which I don't have :-(. I did just no notice I was processing the output stream differently. In the case that always worked:
And in the case that wasn't:
When I added the other two arguments to my write, it started working every time.
From the api:
I'm happy it works, but not quite satisfied I understand why. Or why it would be unpredictable -- I could repost the same data multiple times, with an inconsistent result.... weird.
Read the API documentation for the read(byte) method of InputStream carefully. Then ask yourself what would happen if that method put 389 bytes into that array. Would writing out 389 bytes be the right thing to do, or would writing out 1024 bytes be the right thing to do?
Joined: May 28, 2008
After the read I'd have 389 meaningful bytes in my 1024 array. With the first form of write, I was writing all 1024 (because bytes.length is 1024 of course), and with the second, I was writing only the number of bytes read into the buffer.