title: Convert BufferedImage into byte[] without I/O
body: <p>Hi I have a BufferedImage instance in memory and want to convert it into byte[] to encode as base64 string without I/O operation for performance consideration. I was using the following API:</p>
  
  <pre><code>ByteArrayOutputStream baos = new ByteArrayOutputStream ();
  ImageIO.write(image,"png",baos);
  return baos.toByteArray();
  </code></pre>
  
  <p>However, this API still implicitly writes the image to the OS temp directory, which will lead to failure in case that the underlying OS temp directory is full and the temp file cannot be created.
  Stack Trace:</p>
  
  <pre><code>Caused by: java.io.IOException: No space left on device
      at java.io.RandomAccessFile.write(RandomAccessFile.java:493)
      at javax.imageio.stream.FileCacheImageOutputStream.write(FileCacheImageOutputStream.java:134)
      at javax.imageio.stream.ImageOutputStreamImpl.write(ImageOutputStreamImpl.java:66)
      at com.sun.imageio.plugins.png.PNGImageWriter.write_magic(PNGImageWriter.java:376)
      at com.sun.imageio.plugins.png.PNGImageWriter.write(PNGImageWriter.java:1115)
      at javax.imageio.ImageWriter.write(ImageWriter.java:628)
      at javax.imageio.ImageIO.write(ImageIO.java:1480)
      at javax.imageio.ImageIO.write(ImageIO.java:1554)
  </code></pre>
  
  <p>Is there an efficient (like in-memory conversion or efficient I/O) way to do the conversion without I/O? Please advise.</p>