[英]ArrayIndexOutOfBoundsException: 4096 while reading gif file
I am able to read png file. 我能够读取png文件。 But getting ArrayIndexOutOfBoundsException: 4096 while reading gif file.
但是在读取gif文件时出现ArrayIndexOutOfBoundsException:4096。
byte[] fileData = imageFile.getFileData();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(fileData);
RenderedImage image = ImageIO.read(byteArrayInputStream)
Exception thrown looks like 抛出异常看起来像
java.lang.ArrayIndexOutOfBoundsException: 4096
at com.sun.imageio.plugins.gif.GIFImageReader.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
at javax.imageio.ImageIO.read(Unknown Source)
what could be the issue and what is the resolution? 可能是什么问题,解决方案是什么?
Update 3: Solution 更新3:解决方案
I ended up developing my own GifDecoder and released it as open source under the Apache License 2.0. 我最终开发了自己的GifDecoder,并根据Apache License 2.0将其作为开源发布。 You can get it from here: https://github.com/DhyanB/Open-Imaging .
您可以从这里获取: https : //github.com/DhyanB/Open-Imaging 。 It does not suffer from the
ArrayIndexOutOfBoundsException
issue and delivers decent performance. 它不受
ArrayIndexOutOfBoundsException
问题的困扰,并提供了不错的性能。
Any feedback is highly appreciated. 任何反馈都非常感谢。 In particular, I'd like to know if it works correctly for all of your images and if you are happy with its speed.
特别是,我想知道它是否对所有图像都能正常工作,以及您是否对它的速度感到满意。
I hope this is helpful to you (: 希望对您有帮助(:
Initial answer 初步答案
Maybe this bug report is related to or describes the same problem: https://bugs.openjdk.java.net/browse/JDK-7132728 . 可能此错误报告与相同的问题有关或描述了相同的问题: https : //bugs.openjdk.java.net/browse/JDK-7132728 。
Quote: 引用:
FULL PRODUCT VERSION : java version "1.7.0_02" Java(TM) SE Runtime Environment (build 1.7.0_02-b13) Java HotSpot(TM) 64-Bit Server VM (build 22.0-b10, mixed mode) ADDITIONAL OS VERSION INFORMATION : Microsoft Windows [Version 6.1.7601] A DESCRIPTION OF THE PROBLEM : according to specification http://www.w3.org/Graphics/GIF/spec-gif89a.txt > There is not a requirement to send a clear code when the string table is full. However, GIFImageReader requires the clear code when the string table is full. GIFImageReader violates the specification, clearly. In the real world, sometimes people finds such high compressed gif image. so you should fix this bug. STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : javac -cp .;PATH_TO_COMMONS_CODEC GIF_OverflowStringList_Test.java java -cp .;PATH_TO_COMMONS_CODEC GIF_OverflowStringList_Test EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - complete normally. no output ACTUAL - ArrayIndexOutOfBounds occurs. ERROR MESSAGES/STACK TRACES THAT OCCUR : Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4096 at com.sun.imageio.plugins.gif.GIFImageReader.read(GIFImageReader.java:1 075) at javax.imageio.ImageIO.read(ImageIO.java:1400) at javax.imageio.ImageIO.read(ImageIO.java:1322) at GIF_OverflowStringList_Test.main(GIF_OverflowStringList_Test.java:8) REPRODUCIBILITY : This bug can be reproduced always.
The bug report also provides code to reproduce the bug. 错误报告还提供了重现该错误的代码。
Update 1 更新1
And here is an image that causes the bug in my own code: 这是在我自己的代码中导致该错误的图像:
Update 2 更新2
I tried to read the same image using Apache Commons Imaging, which led to the following exception: 我尝试使用Apache Commons Imaging读取相同的图像,这导致以下异常:
java.io.IOException: AddStringToTable: codes: 4096 code_size: 12 at org.apache.commons.imaging.common.mylzw.MyLzwDecompressor.addStringToTable(MyLzwDecompressor.java:112) at org.apache.commons.imaging.common.mylzw.MyLzwDecompressor.decompress(MyLzwDecompressor.java:168) at org.apache.commons.imaging.formats.gif.GifImageParser.readImageDescriptor(GifImageParser.java:388) at org.apache.commons.imaging.formats.gif.GifImageParser.readBlocks(GifImageParser.java:251) at org.apache.commons.imaging.formats.gif.GifImageParser.readFile(GifImageParser.java:455) at org.apache.commons.imaging.formats.gif.GifImageParser.readFile(GifImageParser.java:435) at org.apache.commons.imaging.formats.gif.GifImageParser.getBufferedImage(GifImageParser.java:646) at org.apache.commons.imaging.Imaging.getBufferedImage(Imaging.java:1378) at org.apache.commons.imaging.Imaging.getBufferedImage(Imaging.java:1292)
That looks very similar to the problem we have with ImageIO, so I reported the bug at the Apache Commons JIRA: https://issues.apache.org/jira/browse/IMAGING-130 . 这看起来与ImageIO存在的问题非常相似,因此我在Apache Commons JIRA中报告了该错误: https : //issues.apache.org/jira/browse/IMAGING-130 。
I encountered the exact same problem you did, but I had to stick to an ImageIO interface, which no other library did. 我遇到了与您完全相同的问题,但是我必须坚持使用ImageIO接口,而其他任何库都没有。 Apart from Jack's great answer, I simply patched the existing
GIFImageReader
class with a few lines of code, and got it marginally working. 除了杰克的好答案之外,我还简单地用几行代码修补了现有的
GIFImageReader
类,并使其GIFImageReader
工作。
Copy this link into PatchedGIFImageReader.java
and use as such: 将此链接复制到
PatchedGIFImageReader.java
并这样使用:
reader = new PatchedGIFImageReader(null);
reader.setInput(ImageIO.createImageInputStream(new FileInputStream(files[i])));
int ub = reader.getNumImages(true);
for (int x=0;x<ub;x++) {
BufferedImage img = reader.read(x);
//Do whatever with the new img bufferedimage
Be sure to change the package name to whatever you're using. 确保将软件包名称更改为您正在使用的名称。
Unfortunately results may vary, as the patch was a 1 minute bugfix that basically just exits the loop if it goes past the buffer. 不幸的是,结果可能会有所不同,因为该修补程序是1分钟的错误修复,如果它经过缓冲区,则基本上只会退出循环。 Some gifs it loads fine, others have a few visual artifacts.
它可以很好地加载一些gif,而另一些则带有一些视觉瑕疵。
Such is life. 这就是生活。 If anyone knows a better fix instead of mine, please do tell.
如果有人知道更好的解决方案,而不是我的,请告诉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.