ImageIO speed problem

Hi, I’m having a really strange problem. I’m making an applet that loads about 20 small images(32x32 or 64x64) with ImageIO.
This works fine in eclipse (the applet takes about 0.3 seconds to load the images), but when I export it to a jar and run it on my website, the applet takes 30 seconds to load just the images.
The total size of all the images is 160kb. I am on broadband and can download at 1.2mb/s and my webhost isn’t that slow either.
Can anyone help me with this?
Thanks,
roland

public BufferedImage LoadImage(String str)
	{
		System.out.println("loading " + str);
		BufferedImage bf = null;
		URL url = null;
        try 
		{
			url = new URL(codebase, str);
		} 
        catch (Exception e) {}
		
		BufferedImage image = null;
		try 
		{
			image = ImageIO.read(url);
		} 
		catch (IOException e) 
		{
			System.out.println("error");
		}
		if (image == null)
			return null;
		bf = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
		bf.getGraphics().drawImage(image, 0, 0,bf.getWidth(),bf.getHeight(), null);
		
		return bf;
	}

Try to open the Java console. You’ll see that the whole JAR is downloaded every time you load a picture.

Thanks for the reply, theagentd.

That is indeed a problem if it does! I am looking at my java console and it doesn’t say that. Do I need to change a setting?
Also I am not loading the image as a resource inside that applet, but from a file on my website.

I’m not clear how the Java console would help here. If the code doesn’t throw an exception or print a message to the console, then there wouldn’t be much to see, afaik. But there may be tools that I am unaware of, as I only learned how to even get the console to show just a couple weeks ago!

However, each time you load from a file, you are incurring the time needed to bring that data over the internet, which is much slower than loading a “resource” that is part of the jar file. When you include the images as resources, they are brought over along with the rest of the applet, and loading is direct (all occuring on the client) rather than via streaming.

The problem I had was that I was loading images from a single JAR file, but somehow the caching didn’t work and the JAR was downloaded every time I loaded an image. I found this out by first checking the network traffic (which showed about (Jar file size) x (number of images)). I could see in the Java console that the caching didn’t work by starting the applet and quickly opening the console and increasing the trace level (just press 5 or something). It the screamed out “loading resource blah blah, cache post not found” over and over again. I never managed to get it working as it should but I suspect a bad HTTP server program was the culprit. Let’s hope it’s something else. :wink:

Try including the resources inside the jar. That is the fastest way since only 1 file has to ever be downloaded: the jar file.

You can also zip it on the server and then unzip the file and get the resources from there.

Still, 30 seconds seems a bit long. I’d profile how much of that time is the actual loading from the internets, and how much is ImageIO reading it. To do that you’d need to save the data locally first.

Thanks everyone for the replies! :slight_smile:
Sticking the images in the jar did the trick! now the applet takes about 5 seconds to load but ImageIO loads the images just as fast as offline.
:smiley:

If permissions isn’t a problem, the best solution is to store all media locally on the client. (A one time download)