Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2006

Tip: Looking for answers? Try searching our database.

problem with creating Image from a URL (big file and slow Internet connection)

Thread view: 
Jacek.Lubinski@gmail.com - 10 Mar 2006 22:53 GMT
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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.