13

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:

ByteArrayOutputStream baos = new ByteArrayOutputStream ();
ImageIO.write(image,"png",baos);
return baos.toByteArray();

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:

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)

Is there an efficient (like in-memory conversion or efficient I/O) way to do the conversion without I/O? Please advise.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
user1344933
  • 131
  • 1
  • 4
  • 3
    I am...surprised that this happens. – Louis Wasserman Apr 19 '12 at 19:56
  • This is almost certainly a bug in the JVM. Have you tried the latest version? – Peter Lawrey Apr 19 '12 at 19:58
  • @PeterLawrey: why do you consider this a bug? There is a whole class [`FileCacheImageOutputStream`](http://docs.oracle.com/javase/7/docs/api/javax/imageio/stream/FileCacheImageOutputStream.html). However I wasn't aware of this mechanism in `ImageIO` at all. – Tomasz Nurkiewicz Apr 19 '12 at 20:04
  • IMHO, A disk cache shouldn't automagically write data until it fills your drive. When was the last time your browser crashed because it filled your drive. ;) – Peter Lawrey Apr 19 '12 at 20:08
  • Is there a way that we can do this without using ImageIO? I'm having a similar problem but I would like to use an alternative because of performance reasons – tier1 Apr 19 '12 at 20:09
  • The OS temp directory was filled by other application dump. hence the disk write error occures. I set the cache flag as false. However, ImageIO still takes fair amount of time. The invocation takes around 100ms for small images. Is there another efficient way to do this? – user1344933 Apr 19 '12 at 20:31

4 Answers4

12

Disable the ImageIO cache through the ImageIO.setUseCache() method:

ImageIO.setUseCache(false);

It is on by default according to the javadoc:

Sets a flag indicating whether a disk-based cache file should be used when creating ImageInputStreams and ImageOutputStreams.

When reading from a standard InputStream>, it may be necessary to save previously read information in a cache since the underlying stream does not allow data to be re-read. Similarly, when writing to a standard OutputStream, a cache may be used to allow a previously written value to be changed before flushing it to the final destination.

The cache may reside in main memory or on disk. Setting this flag to false disallows the use of disk for future streams, which may be advantageous when working with small images, as the overhead of creating and destroying files is removed.

On startup, the value is set to true.

Community
  • 1
  • 1
prunge
  • 22,460
  • 3
  • 73
  • 80
5

Both mentions of ImageIO.setUseCache(false) is correct. However, if you don't like to disable disk caching for ImageIO globally, an alternative is to explicitly wrap the stream in a MemoryCacheImageOutputStream (which does in-memory caching instead of disk caching):

ByteArrayOutputStream baos = new ByteArrayOutputStream ();
ImageOutputStream stream = new MemoryCacheImageOutputStream(baos);
ImageIO.write(image, "png", stream);
stream.close();
return baos.toByteArray();
Harald K
  • 26,314
  • 7
  • 65
  • 111
  • 1
    Seems, it doesn't true anymore, at least in 1.8. `com.sun.imageio.spi.OutputStreamImageOutputStreamSpi#createOutputStreamInstance` doesn't check the type of the stream but does check the `useCache` parameter. Anyway, it would be really nice to have a way to disable caching locally. – sedovav Oct 13 '17 at 15:38
  • 1
    @sedovav The above solution does indeed provide a way for “locally” disabling the disk cache. When you create the MemoryCacheImageOutputStream directly like that you never hit the Spi, so whatever it does is irrelevant. PS: Note that ImageOutputSream is not a subclass of OutputStream, despite its name. And there’s a separate overload of ImageIO.write for this. – Harald K Oct 13 '17 at 19:43
  • 2
    Indeed. Thank you very much! – sedovav Oct 16 '17 at 09:55
3

ImageIO by default writes it's cache to disk even when you only use streams. Try disabling the cache with:

ImageIO.setUseCache(false);
Roger Lindsjö
  • 11,330
  • 1
  • 42
  • 53
2

((DataBufferByte)img.getRaster().getDataBuffer()).getData() automatically returns a byte array if your image was in a byte format. No need for any IO at all.

warren
  • 563
  • 2
  • 13