--- old/src/java.desktop/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageReader.java 2018-01-08 12:45:27.473068000 +0530 +++ new/src/java.desktop/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageReader.java 2018-01-08 12:45:27.000832000 +0530 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -374,7 +374,11 @@ // And set current image since we've read it now currentImage = 0; } - if (seekForwardOnly) { + // If imagePositions list doesn't contain any of the image stream + // starting position(i.e tables-only image) we should not try to access + // imagePositions.size() as it done below, because it will lead to + // IndexOutOfBoundsException with index -1. + if (seekForwardOnly && (!(imagePositions.isEmpty()))) { Long pos = imagePositions.get(imagePositions.size()-1); iis.flushBefore(pos.longValue()); } @@ -492,6 +496,11 @@ if (!tablesOnlyChecked) { checkTablesOnly(); } + // We should not try to read image information from an input stream + // which only contains tables-only(StreamMetadata) information. + if (imagePositions.isEmpty()) { + throw new IIOException("No image data present to read"); + } if (imageIndex < imagePositions.size()) { iis.seek(imagePositions.get(imageIndex).longValue()); } else {