4954348: JPGWriter.getNumThumbnailsSupported does not return -1 when passing null values
authorpnarayanan
Thu, 29 Mar 2018 12:03:40 +0530
changeset 49500 3c68768d3904
parent 49499 58d26b495f24
child 49501 5daa8ef17089
4954348: JPGWriter.getNumThumbnailsSupported does not return -1 when passing null values Reviewed-by: prr, serb, jdv
src/java.desktop/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageWriter.java
test/jdk/javax/imageio/plugins/jpeg/JpegNumThumbnailsTest.java
--- a/src/java.desktop/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageWriter.java	Wed Mar 28 15:14:46 2018 -0700
+++ b/src/java.desktop/share/classes/com/sun/imageio/plugins/jpeg/JPEGImageWriter.java	Thu Mar 29 12:03:40 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2015, 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
@@ -303,6 +303,14 @@
                                          ImageWriteParam param,
                                          IIOMetadata streamMetadata,
                                          IIOMetadata imageMetadata) {
+        // Check whether sufficient data is available.
+        if (imageType == null && imageMetadata == null) {
+            // The method has been invoked with insufficient data. Henceforth
+            // we return -1 as recommended by ImageWriter specification.
+            return -1;
+        }
+
+        // Check if the image type and metadata are JFIF compatible.
         if (jfifOK(imageType, param, streamMetadata, imageMetadata)) {
             return Integer.MAX_VALUE;
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/javax/imageio/plugins/jpeg/JpegNumThumbnailsTest.java	Thu Mar 29 12:03:40 2018 +0530
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 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.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4954348
+ * @summary Checks whether JpegImageWriter returns -1 as recommended by the
+ *          specification when getNumThumbnailsSupported method is invoked
+ *          with insufficient data.
+ * @run main JpegNumThumbnailsTest
+ */
+import javax.imageio.ImageIO;
+import javax.imageio.ImageTypeSpecifier;
+import javax.imageio.ImageWriter;
+import javax.imageio.metadata.IIOMetadata;
+import java.awt.image.BufferedImage;
+import java.util.Iterator;
+import static java.awt.image.BufferedImage.TYPE_INT_RGB;
+
+public class JpegNumThumbnailsTest {
+
+    public static void main(String args[]) {
+        // Test variables.
+        Iterator<ImageWriter> iterWriter = null;
+        ImageWriter jpgWriter = null;
+        IIOMetadata imgMetadata = null;
+        BufferedImage testImage = null;
+        ImageTypeSpecifier imgType = null;
+        int numThumbnails = 0;
+
+        iterWriter = ImageIO.getImageWritersByFormatName("JPEG");
+        if (iterWriter.hasNext()) {
+            try {
+                // JpegImageWriter requires either image type or image metadata
+                // to determine the number of thumbnails that could be
+                // supported. Hence we test for all possible input combinations
+                // and observe the result.
+                jpgWriter = iterWriter.next();
+                testImage = new BufferedImage(32, 32, TYPE_INT_RGB);
+                imgType = ImageTypeSpecifier.createFromRenderedImage(testImage);
+                imgMetadata = jpgWriter.getDefaultImageMetadata(imgType, null);
+
+                // Observe the result with insufficient data.
+                numThumbnails = jpgWriter.getNumThumbnailsSupported(null,
+                                        null, null, null);
+                if (numThumbnails != -1) {
+                    reportException("Incorrect number of thumbnails returned.");
+                }
+
+                // Observe the result with valid image type.
+                numThumbnails = jpgWriter.getNumThumbnailsSupported(imgType,
+                                        null, null, null);
+                if (numThumbnails != Integer.MAX_VALUE) {
+                    reportException("Incorrect number of thumbnails returned.");
+                }
+
+                // Observe the result with valid image metadata.
+                numThumbnails = jpgWriter.getNumThumbnailsSupported(null,
+                                        null, null, imgMetadata);
+                if (numThumbnails != Integer.MAX_VALUE) {
+                    reportException("Incorrect number of thumbnails returned.");
+                }
+
+                // Observe the result with valid image type and metadata.
+                numThumbnails = jpgWriter.getNumThumbnailsSupported(imgType,
+                                        null, null, imgMetadata);
+                if (numThumbnails != Integer.MAX_VALUE) {
+                    reportException("Incorrect number of thumbnails returned.");
+                }
+            } finally {
+                // Dispose the writer
+                jpgWriter.dispose();
+            }
+        }
+    }
+
+    private static void reportException(String message) {
+        // Report a runtime exception with the required message.
+        throw new RuntimeException("Test Failed. " + message);
+    }
+}