ArrayIndexOutOfBoundsException: 4096 while reading gif file

10,433

Solution 1

Update 3: Solution

I ended up developing my own GifDecoder and released it as open source under the Apache License 2.0. You can get it from here: https://github.com/DhyanB/Open-Imaging. It does not suffer from the ArrayIndexOutOfBoundsException issue and delivers decent performance.

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.

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

And here is an image that causes the bug in my own code:

enter image description here

Update 2

I tried to read the same image using Apache Commons Imaging, which led to the following exception:

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.

Solution 2

I encountered the exact same problem you did, but I had to stick to an ImageIO interface, which no other library did. Apart from Jack's great answer, I simply patched the existing GIFImageReader class with a few lines of code, and got it marginally working.

Copy this link into PatchedGIFImageReader.java and use as such:

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. Some gifs it loads fine, others have a few visual artifacts.

Such is life. If anyone knows a better fix instead of mine, please do tell.

Share:
10,433

Related videos on Youtube

Venkat Janyavula
Author by

Venkat Janyavula

Updated on July 24, 2022

Comments

  • Venkat Janyavula
    Venkat Janyavula 5 months

    I am able to read png file. But getting ArrayIndexOutOfBoundsException: 4096 while reading gif file.

    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?

    • Svetlin Zarev
      Svetlin Zarev almost 9 years
      does this happen with every gif you try to read or only with specific gifs
    • Venkat Janyavula
      Venkat Janyavula almost 9 years
      It almost happened with all the gif's I have.
    • zapl
      zapl almost 9 years
      Couldn't you simply do ImageIO.read(imageFile.getInputStream())?
    • Marco13
      Marco13 almost 9 years
      Can you provide one of these GIFs?
    • Venkat Janyavula
      Venkat Janyavula almost 9 years
      @zapl -- tried to read as you mentions. But still getting the same exception.
    • zapl
      zapl almost 9 years
      You might have either corrupt .gif files (e.g. too short / not completely transferred yet or so) or a type of gif that is not understood by ImageIO.
    • Harald K
      Harald K almost 9 years
      Your code is good. There is something special about your GIF files, they are either corrupt or of a version not supported by the GIFImageReader. Could also be a bug in your specific JDK/JRE version. We can't help you anymore if you don't provide a GIF that causes the problem AND state what JDK/JRE you are using.
    • Venkat Janyavula
      Venkat Janyavula over 8 years
      Thank You haraldk. I will try with the options you told me. Will reply
    • dragon66 about 7 years
      This is clearly a bug of the internal GIFImageReader which never got fixed.
  • Name McChange
    Name McChange about 8 years
    Wow, this is crazy that they haven't gotten it fixed after all this time.
  • Stephan
    Stephan over 6 years
    JDK 1.8.0_45 here... bug still exists :\
  • Display name
    Display name almost 4 years
    I encountered a similar bug recently. Out of bounds exception occurs on the 11th frame.
  • Bruno P. Kinoshita
    Bruno P. Kinoshita over 3 years
    Excellent explanation. Thanks for including a test image. This SO answer was linked in IMAGING-130, and another contributor submitted a pull request that has already been merged. This fix will be available in the commons-imaging-1.0-alpha2 release. Thanks! Bruno