Search code examples
javaimagejava-8javax.imageio

ImageIO.read() can't read some images


I have a GIF image and try to read it into BufferedImage.

public class ImageReadTest {

    public static void main(String[] args) throws IOException {
        f(new File("/Users/hieugioi/Downloads/Photos/butterfly.gif"));
    }

    public static void f(File file) throws IOException {
        BufferedImage image = ImageIO.read(file);
    }

}

It shows errors:

Exception in thread "main" java.lang.IndexOutOfBoundsException
    at java.io.RandomAccessFile.readBytes(Native Method)
    at java.io.RandomAccessFile.read(RandomAccessFile.java:377)
    at javax.imageio.stream.FileImageInputStream.read(FileImageInputStream.java:117)
    at com.sun.imageio.plugins.gif.GIFImageReader.getCode(GIFImageReader.java:351)
    at com.sun.imageio.plugins.gif.GIFImageReader.read(GIFImageReader.java:950)
    at javax.imageio.ImageIO.read(ImageIO.java:1448)
    at javax.imageio.ImageIO.read(ImageIO.java:1308)
    at com.hieutest.ImageReadTest.f(ImageReadTest.java:16)
    at com.hieutest.ImageReadTest.main(ImageReadTest.java:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

The "butterfly" image got from this open source project:

enter image description here

It succeeds with other regular images but it fails with this image. Is it image problem or API bug?


Solution

  • This is know JDK bug see:

    These bug are fixed in Java 9.