Problem loading ImageIcon in applet: java.io.IOException: Stream closed

Hello everybody,

This is a very strange problem: I have a Swing app, but I need to convert it to a Applet so, I did some changes but now it doesn't load the images it has for menu toolbar, now I get this error message:

java.io.IOException: Stream closed

at java.io.BufferedInputStream.getInIfOpen(Unknown Source)

at java.io.BufferedInputStream.fill(Unknown Source)

at java.io.BufferedInputStream.read1(Unknown Source)

at java.io.BufferedInputStream.read(Unknown Source)

at java.io.FilterInputStream.read(Unknown Source)

at co.gov.superservicios.orfeo.flujos.java.GraphEd.createAppletImageIcon(Unknown Source)

at co.gov.superservicios.orfeo.flujos.java.GraphEd.createToolBar(Unknown Source)

at co.gov.superservicios.orfeo.flujos.java.GraphEdX.createToolBar(Unknown Source)

at co.gov.superservicios.orfeo.flujos.java.GraphEd.populateContentPane(Unknown Source)

at co.gov.superservicios.orfeo.flujos.java.GraphEd.<init>(Unknown Source)

at co.gov.superservicios.orfeo.flujos.java.GraphEdX.<init>(Unknown Source)

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

at java.lang.reflect.Constructor.newInstance(Unknown Source)

at java.lang.Class.newInstance0(Unknown Source)

at java.lang.Class.newInstance(Unknown Source)

at sun.applet.AppletPanel.createApplet(Unknown Source)

at sun.plugin.AppletViewer.createApplet(Unknown Source)

at sun.applet.AppletPanel.runLoader(Unknown Source)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

I have a signed jar with this structure:

Java classes:

co\gov\superservicios\orfeo\flujos\java

Toolbar images:

co\gov\superservicios\orfeo\flujos\java\resources

in my code, I'm trying to load images with:

JToolBar toolbar = super.createToolBar();

// Collapse

collapse =new AbstractAction(){

publicvoid actionPerformed(ActionEvent e){

graph.getGraphLayoutCache().collapse(graph.getSelectionCells());

}

};

String url ="co/gov/superservicios/orfeo/flujos/java/resources/collapse.gif";

collapse.putValue(Action.SMALL_ICON,

createAppletImageIcon(url,"Colapsar"));

collapse.setEnabled(false);

toolbar.add(collapse);

protectedstatic ImageIcon createAppletImageIcon(String path, String description){

int MAX_IMAGE_SIZE = 910; /int count = 0;

BufferedInputStream imgStream =new BufferedInputStream(

Graphica.class.getResourceAsStream(path));

if (imgStream !=null){

byte buf[] =newbyte[MAX_IMAGE_SIZE];

try{

count = imgStream.read(buf);

}catch (IOException ieo){

System.err.println("Couldn't read stream from file: " + path);

}

try{

imgStream.close();

}catch (IOException ieo){

System.err.println("No se pudo cerrar el archivo " + path);

}

if (count <= 0){

System.err.println("Empty file: " + path);

returnnull;

}

returnnew ImageIcon(Toolkit.getDefaultToolkit().createImage(buf),

description);

}else{

System.err.println("Couldn't find file: " + path);

returnnull;

}

}

As I can see, it's not a FNF Exception, so it finds the file but it gets closed with every operation, I tried a: stream.available();

and I get the same Exception. What can I do?

Thanks a lot,

spelling, changed son for so :)

Message was edited by:

bogotano

[5323 byte] By [bogotanoa] at [2007-11-26 19:14:41]
# 1

I'm not sure if this would help,

however I had a similar problem in that

my applet worked locally in Sun's Applet Viewer

however it would crash over the internet;

I implemented code similar to yours from:

http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html

(the "loading images into applets" section)

I now verify that the ImageIcon value returned from createAppletImageIcon() is not null,

I believe that it might take some time for the jars to fully download,

and there might exist a problem if the Applet tries to paint()

prior to the image download completing

I now also check for null within paint()

when painting the resulting images

my application processes larger images though,

not toolbar icons,

so I don't know how helpful my experiences might be

michael11717a at 2007-7-9 21:15:59 > top of Java-index,Desktop,Core GUI APIs...
# 2

I'm not an expert at the internals of Java,

however I was thinking that :

I believe that operating systems might have a finite limit

on the quantities of file handles

which are allowed to be simultaneously open,

perhaps if a lot of image references

are stalled due to download delay,

some file handles might get closed

as I said, I'm no expert at Java internals,

however perhaps if you could seperate out

the createAppletImageIcon() calls,

and try not to process the resulting images

until their object references have settled,

before passing their object references along

to the toolbar processing ...

michael11717a at 2007-7-9 21:15:59 > top of Java-index,Desktop,Core GUI APIs...