I want to convert an InputStream object representing an image file to a BufferedImage object and after performing some operations on the BufferedImage convert it back to an InputStream so that it can be written to disk.I dont want to create a file object on disk first in order to prevent additional IO overhead.
I think i can do the following to convert a BufferedImage to InputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(image,fileExtension, outputStream);
InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
Is that correct ?. Also, i have the following two questions
- How to get BufferedImage object from an InputStream object
- Can i get the filesize directly from the InputStream object ?
Some example would really help
Thank You
read(InputStream stream)method ofImageIOavailable()but this does not guarantee the size of the stream (it works forFileInputStream)OutputStreamand you can use thewrite(RenderedImage im, String formatName, OutputStream output)of ImageIO