您好我在内存中有一个 BufferedImage 实例,出于性能考虑,我想将其转换为 byte[] 以编码为 base64 字符串而无需 I/O 操作。我正在使用以下 API:
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
ImageIO.write(image,"png",baos);
return baos.toByteArray();
但是这个API还是会隐式的将镜像写入OS temp目录,这样会导致底层OS temp目录已满,无法创建temp文件的情况下失败。堆栈跟踪:
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)
是否有一种有效的(如内存转换或高效的 I/O)方式来进行没有 I/O 的转换?请指教。