I am using javax.imageio.ImageIO to write a BufferedImage b into a byte array and finally to a file. 1 out of 10 times when creating the byte array, a java.lang.IndexOutOfBoundsException is thrown. It is not reproducible by just using the same image though.
BufferedImage img = ...
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// the next line will lead to the exception being thrown
ImageIO.write(img, "png", baos);
byte[] byteArray = baos.toByteArray()
Result is:
java.lang.IndexOutOfBoundsException
Line | Method
->> 166 | seek in
javax.imageio.stream.FileCacheImageOutputStream
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| 227 | close in ''
| 1570 | write . . . . . . . . . . in javax.imageio.ImageIO
I found a bug report, seemingly describing the same problem. But I cannot find a fix though it’s from 2010.
Any ideas? Work arounds? Links to ressources?
Help is highly appreciated!
It is indeed a bug.
Though, if you read through the report you’ll see…
Try to utilize a
MemoryCacheImageOutputStreaminstead, until the bug is squashed eventually.