Line by line image swing

Posted by user1046017 on Stack Overflow See other posts from Stack Overflow or by user1046017
Published on 2011-11-14T17:48:41Z Indexed on 2011/11/14 17:50 UTC
Read the original article Hit count: 189

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about image