Hi,
I have a quite slow Internet connection (sth like 3kB/s). In my
application I create ImageIcon (and then Image from it) from an URL.
Sometimes when I download quite big PNG images (sth like 400 kB) the
download process made by internal JVM methods breaks up. All my methods
finish properly, but suddenly I get an error:
java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at sun.awt.image.PNGFilterInputStream.read(PNGImageDecoder.java:830)
at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:213)
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:134)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:183)
at java.io.BufferedInputStream.read(BufferedInputStream.java:201)
at sun.awt.image.PNGImageDecoder.produceImage(PNGImageDecoder.java:355)
at
sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:254)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:172)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:136)
and part of the image downloaded is totally black when I display it.
Is there a way I can somehow catch this exception or change the code:
Image img = (new ImageIcon(url)).getImage();
so I'm sure that the whole image is downloaded properly?
jacek
Filip Larsen - 11 Mar 2006 09:21 GMT
> I have a quite slow Internet connection (sth like 3kB/s). In my
> application I create ImageIcon (and then Image from it) from an URL.
> Sometimes when I download quite big PNG images (sth like 400 kB) the
> download process made by internal JVM methods breaks up.
I have had a similar problem with image files being scrambled on slow
connections when loaded using some of the Java image API. My theory is
that the client do not read all the image data from the server in one
go, but leave some data "in the pipe" so to speak. If this happens, the
server may abort the connection after a while and when the full the
image is finally needed on the client (e.g. for rendering) is now only
partially loaded.
Theory aside, the work-around that worked for me was to preload all the
image data into my own buffer before handing it over to the image
constructor. A simple ByteArrayInputStream could do, but if your images
are large you probably want to use a chunked buffer where you can
allocate and discard chunks as they are decoded. If you use
javax.imageio you should take a look at
javax.imageio.stream.ImageInputStream.
Regards,

Signature
Filip Larsen
Thomas Hawtin - 12 Mar 2006 22:51 GMT
> I have a quite slow Internet connection (sth like 3kB/s). In my
> application I create ImageIcon (and then Image from it) from an URL.
[quoted text clipped - 5 lines]
> at java.lang.System.arraycopy(Native Method)
> at sun.awt.image.PNGFilterInputStream.read(PNGImageDecoder.java:830)
Well, it certainly looks like a bug in the JRE. I can't find anything on
bugs.sun.com.
Which (exact) version of Java are you using? Looking at Sun's 1.5.0_05,
PNGImageDecoder doesn't have 830 lines in it. However, it does look as
if a short stream will cause that error (getChunk will update
chunkLength but then return false; getData will not clear chunkLength in
that case). There's some odd code in there, and odd code generally means
bugs.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/