users@grizzly.java.net

Comet issue with requirement to close output streams

From: John C. Turnbull <ozemale_at_ozemail.com.au>
Date: Thu, 2 Jul 2009 16:29:16 +1000

I am trying to get Comet to work between an applet and a servlet with
GlassFish v2 UR2 for loading images but I have hit a bit of a road block. I
can get it to work without using Comet no problem but when I put the exact
same code into my Comet handler I find that I have to explicitly close the
output stream after sending each image (which completely defeats the purpose
of using Comet) for the applet to be able to read it whereas when the same
code is just used in the servlet I can get by just doing a flush.

 

In my Comet handler I have this code:

 

public class ImageCometHandler implements CometHandler<ServletOutputStream>
{

 

        private ServletOutputStream os;

 

        public void attach(ServletOutputStream os){

            this.os = os;

        }

 

        public void onEvent(final CometEvent event) throws IOException{

                                                BufferedImage image =
(BufferedImage)event.attachment();

 
JPEGCodec.createJPEGEncoder(os).encode(image);

                                                os.flush();

// os.close();

        }

 

...

 

}

 

If I uncomment the call to os.close() the applet blocks on trying to read
the image. The code to read the image in the applet is:

 

            BufferedImage bi = null;

            try {

                  bi = ImageIO.read(this.stream);

            } catch (final IOException ioe) {

                  System.err.println("Exception loading image: " + ioe);

            }

 

Why does putting the code to send the image into the Comet handler require
that I close the output stream whereas the same code gets by with just a
flush when Comet is not used? Is there any way around this?

 

Thanks,

 

John