Oct 19

You need to read all bytes from source stream to the destination. The read() method will return -1 when eof is reached, otherwise it returns the number of bytes read.


public static void copy(InputStream in, OutputStream out
   int bufferSize)
   throws IOException
{
   // Read bytes and write to destination until eof

   byte[] buf = new byte[bufferSize];
   int len = 0;
   while ((len = in.read(buf)) >= 0)
   {
      out.write(buf, 0, len);
   }
}

written by objects \\ tags: , , ,

Sep 29

The ImageIO class can be used to write an image as a PNG encoded stream.If you write that stream to a ByteArrayOutputStream then you will end up with a byte array that contains the PNG encoded image.


ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "PNG", out);
byte[] imageBytes = out.toByteArray(); 

written by objects \\ tags: , ,

Sep 29

The ImageIO class can be used to write an image as a JPEG encoded stream.If you write that stream to a ByteArrayOutputStream then you will end up with a byte array that contains the JPEG encoded image.


ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", out);
byte[] imageBytes = out.toByteArray(); 

written by objects \\ tags: , ,