OK, I have a little bit more info. If I work with text and a vanilla
OutputStream then I don't need to close the stream using Comet for the
applet to read it but as soon as I change the otherwise identical code to
use either GZIPOutputStream or ImageIO then I have to close the stream for
the applet to be able to read anything through that stream. Flushing just
seems to do nothing. Can anyone suggest why this would be so and why it
only happens if I use Comet? Without Comet the exact same code (without the
call to close()) works fine.
From: John C. Turnbull [mailto:ozemale_at_ozemail.com.au]
Sent: Thursday, 2 July 2009 19:37
To: users_at_grizzly.dev.java.net
Subject: RE: Comet issue with requirement to close output streams
Oops, the line "If I uncomment the call to os.close() the applet blocks on
trying to read the image" should have read "If I DON'T uncomment the call to
os.close() the applet blocks on trying to read the image".
From: John C. Turnbull [mailto:ozemale_at_ozemail.com.au]
Sent: Thursday, 2 July 2009 16:29
To: users_at_grizzly.dev.java.net
Subject: Comet issue with requirement to close output streams
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