1 /*
   2  * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6275251
  27  * @summary Verifies that GIF image writer throws IllegalStateException if
  28  *          assigned output stream was cleared by reset() method
  29  */
  30 
  31 import java.awt.Color;
  32 import java.awt.Graphics2D;
  33 import java.awt.image.BufferedImage;
  34 import java.io.ByteArrayOutputStream;
  35 import java.io.IOException;
  36 
  37 import javax.imageio.ImageIO;
  38 import javax.imageio.ImageWriter;
  39 import javax.imageio.stream.ImageOutputStream;
  40 
  41 public class WriterResetTest {
  42     public static void main(String[] args) throws IOException {
  43         ImageWriter w = ImageIO.getImageWritersByFormatName("GIF").next();
  44         if (w == null) {
  45             throw new RuntimeException("No writers available!");
  46         }
  47 
  48         ByteArrayOutputStream baos =
  49             new ByteArrayOutputStream();
  50 
  51         ImageOutputStream ios =
  52             ImageIO.createImageOutputStream(baos);
  53 
  54         w.setOutput(ios);
  55 
  56         BufferedImage img = createTestImage();
  57 
  58         try {
  59             w.reset();
  60             w.write(img);
  61         } catch (IllegalStateException e) {
  62             System.out.println("Test passed");
  63         } catch (Throwable e) {
  64             throw new RuntimeException("Test failed", e);
  65         }
  66     }
  67 
  68     private static BufferedImage createTestImage() {
  69         BufferedImage img = new BufferedImage(100, 100,
  70                                               BufferedImage.TYPE_INT_RGB);
  71         Graphics2D g = img.createGraphics();
  72         g.setColor(Color.white);
  73         g.fillRect(0, 0, 100, 100);
  74         g.setColor(Color.black);
  75         g.fillRect(20, 20, 60, 60);
  76 
  77         return img;
  78     }
  79 }