changeset 13149:644e1d66e301

6967419: IndexOutOfBoundsException when drawing PNGs Reviewed-by: prr, psadhukhan
author jdv
date Wed, 02 Dec 2015 00:47:36 +0530
parents 06f9a2bff766
children de7c47b7b8a6
files src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java test/javax/imageio/plugins/png/PngForceStopWritingTest.java
diffstat 2 files changed, 87 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java	Wed Dec 02 00:34:35 2015 +0530
+++ b/src/java.desktop/share/classes/com/sun/imageio/plugins/png/PNGImageWriter.java	Wed Dec 02 00:47:36 2015 +0530
@@ -193,7 +193,17 @@
 
         // Return to end of chunk and flush to minimize buffering
         stream.seek(pos);
-        stream.flushBefore(pos);
+        try {
+            stream.flushBefore(pos);
+        } catch (IOException e) {
+            /*
+             * If flushBefore() fails we try to access startPos in finally
+             * block of write_IDAT(). We should update startPos to avoid
+             * IndexOutOfBoundException while seek() is happening.
+             */
+            this.startPos = stream.getStreamPosition();
+            throw e;
+        }
     }
 
     public int read() throws IOException {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/imageio/plugins/png/PngForceStopWritingTest.java	Wed Dec 02 00:47:36 2015 +0530
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2015, 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     6967419
+ * @summary Test verifies that when we force stop PNG writing to
+ *          ImageOutputStream, it should not cause IndexOutOfBoundException.
+ * @run     main PngForceStopWritingTest
+ */
+
+import java.awt.Color;
+import java.awt.GradientPaint;
+import java.awt.Graphics2D;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.OutputStream;
+import javax.imageio.ImageIO;
+import javax.imageio.stream.ImageOutputStream;
+
+public class PngForceStopWritingTest {
+
+    public static void main(String[] args) throws IOException {
+
+        OutputStream outputStream = new NullOutputStream();
+        ImageOutputStream imageOutputStream =
+            ImageIO.createImageOutputStream(outputStream);
+        try {
+            ImageIO.write(createImage(2048),"PNG", imageOutputStream);
+        } catch (IOException e) {
+            imageOutputStream.close();
+        }
+    }
+
+    private static BufferedImage createImage(int size) {
+
+        BufferedImage image = new
+            BufferedImage(size, size, BufferedImage.TYPE_3BYTE_BGR);
+        Graphics2D g = image.createGraphics();
+        g.setPaint(new GradientPaint(0, 0, Color.blue, size, size, Color.red));
+        g.fillRect(0, 0, size, size);
+        g.dispose();
+        return image;
+    }
+
+    static class NullOutputStream extends OutputStream {
+        long count = 0;
+        @Override
+        public void write(int b) throws IOException {
+            count++;
+            if (count > 30000L) {
+                throw new IOException("Force stop image writing");
+            }
+        }
+    }
+}