Line by line image swing
- by user1046017
I want to show an image as it is downloading, I have the URL, and I am trying to get the image parts like this:
InputStream openStream = url.openStream();
DataInputStream dis = new DataInputStream(new BufferedInputStream(openStream));
ByteArrayOutputStream os = new ByteArrayOutputStream();
while ((s = dis.read(b)) != -1) {
os.write(b , 0, s);
support.firePropertyChange("stream", null, os);
}
This way, any listener get the stream and creates an image, this way:
if("stream".equals(evt.getPropertyName())){
try {
ByteArrayOutputStream stream = (ByteArrayOutputStream) evt.getNewValue();
byte[] byteArray = stream.toByteArray();
stream.flush();
Image createImage = Toolkit.getDefaultToolkit().createImage(byteArray);
this.getContentPane().add(new JLabel(new ImageIcon(createImage)));
} catch (IOException ex) {
Logger.getLogger(ImageTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
However, I am getting a "Premature end of JPEG file sun.awt.image.ImageFormatException: JPEG datastream contains no image" error, the image is a JPG image format, is there any library or method known to make something similar?