< prev index next >

src/java.desktop/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageReader.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -214,10 +214,15 @@
     /**
      * Set to true once stream has been checked for stream metadata
      */
     private boolean tablesOnlyChecked = false;
 
+    /**
+     * Set to true if the input stream contains only a tables-only image.
+     */
+    private boolean tablesOnlyStream = false;
+
     /** The referent to be registered with the Disposer. */
     private Object disposerReferent = new Object();
 
     /** The DisposerRecord that handles the actual disposal of this reader. */
     private DisposerRecord disposerRecord;

@@ -289,10 +294,11 @@
             super.setInput(input, seekForwardOnly, ignoreMetadata);
             this.ignoreMetadata = ignoreMetadata;
             resetInternalState();
             iis = (ImageInputStream) input; // Always works
             setSource(structPointer);
+            tablesOnlyStream = false;
         } finally {
             clearThreadLock();
         }
     }
 

@@ -362,21 +368,36 @@
                 if (debug) {
                     System.out.println
                         ("pos after constructing stream metadata is " + pos);
                 }
             }
-            // Now we are at the first image if there are any, so add it
-            // to the list
+            /* Now we are at the first image if there are any, so add it
+             * to the list.
+             */
             if (hasNextImage()) {
                 imagePositions.add(iis.getStreamPosition());
+            } else {
+                /*
+                 * if there is no image data after reading the first tables-only
+                 * image, we should store this state so that if user tries
+                 * to read image data from this input stream we should throw
+                 * an IIOException in gotoImage(int) function.
+                 */
+                tablesOnlyStream = true;
             }
         } else { // Not tables only, so add original pos to the list
             imagePositions.add(savePos);
             // And set current image since we've read it now
             currentImage = 0;
         }
-        if (seekForwardOnly) {
+        /*
+         * If imagePositions list doesn't contain any 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 for imagePositions list.
+         */
+        if (seekForwardOnly && (!(imagePositions.isEmpty()))) {
             Long pos = imagePositions.get(imagePositions.size()-1);
             iis.flushBefore(pos.longValue());
         }
         tablesOnlyChecked = true;
     }

@@ -490,10 +511,17 @@
             throw new IndexOutOfBoundsException();
         }
         if (!tablesOnlyChecked) {
             checkTablesOnly();
         }
+        /*
+         * We should not try to read image information from an input stream
+         * which only contains tables-only(StreamMetadata) information.
+         */
+        if (tablesOnlyStream) {
+            throw new IIOException("No image data present to read");
+        }
         if (imageIndex < imagePositions.size()) {
             iis.seek(imagePositions.get(imageIndex).longValue());
         } else {
             // read to start of image, saving positions
             // First seek to the last position we already have, and skip the
< prev index next >