diff -r 3b33b57c0096 -r 4abe842e6c48 jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageReader.java --- a/jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageReader.java Thu Aug 11 10:37:50 2016 -0700 +++ b/jdk/src/java.desktop/share/classes/com/sun/imageio/plugins/tiff/TIFFImageReader.java Thu Aug 11 11:35:47 2016 -0700 @@ -35,6 +35,7 @@ import java.awt.image.Raster; import java.awt.image.RenderedImage; import java.awt.image.SampleModel; +import java.io.EOFException; import java.io.IOException; import java.nio.ByteOrder; import java.util.ArrayList; @@ -189,8 +190,8 @@ // Seek to start of first IFD long offset = stream.readUnsignedInt(); + stream.seek(offset); imageStartPosition.add(Long.valueOf(offset)); - stream.seek(offset); } catch (IOException e) { throw new IIOException("I/O error reading header!", e); } @@ -201,10 +202,10 @@ private int locateImage(int imageIndex) throws IIOException { readHeader(); + // Find closest known index + int index = Math.min(imageIndex, imageStartPosition.size() - 1); + try { - // Find closest known index - int index = Math.min(imageIndex, imageStartPosition.size() - 1); - // Seek to that position Long l = imageStartPosition.get(index); stream.seek(l.longValue()); @@ -212,6 +213,11 @@ // Skip IFDs until at desired index or last image found while (index < imageIndex) { int count = stream.readUnsignedShort(); + // If zero-entry IFD, decrement the index and exit the loop + if (count == 0) { + imageIndex = index > 0 ? index - 1 : 0; + break; + } stream.skipBytes(12 * count); long offset = stream.readUnsignedInt(); @@ -219,12 +225,17 @@ return index; } + stream.seek(offset); imageStartPosition.add(Long.valueOf(offset)); - stream.seek(offset); ++index; } - } catch (IOException e) { - throw new IIOException("Couldn't seek!", e); + } catch (EOFException eofe) { + forwardWarningMessage("Ignored " + eofe); + + // Ran off the end of stream: decrement index + imageIndex = index > 0 ? index - 1 : 0; + } catch (IOException ioe) { + throw new IIOException("Couldn't seek!", ioe); } if (currIndex != imageIndex) {