1 /*
   2  * Copyright (c) 2001, 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 4414990 4415041
  27  * @summary Checks that the output is flushed properly when using various
  28  *          ImageOutputStreams and writers
  29  */
  30 
  31 import java.awt.image.BufferedImage;
  32 import java.io.BufferedOutputStream;
  33 import java.io.ByteArrayOutputStream;
  34 import java.io.File;
  35 import java.io.FileOutputStream;
  36 import java.io.IOException;
  37 
  38 import javax.imageio.ImageIO;
  39 import javax.imageio.stream.ImageOutputStream;
  40 
  41 public class StreamFlush {
  42 
  43     public static void main(String[] args) throws IOException {
  44         ImageIO.setUseCache(true);
  45 
  46         // Create a FileImageOutputStream from a FileOutputStream
  47         File temp1 = File.createTempFile("imageio", ".tmp");
  48         temp1.deleteOnExit();
  49         ImageOutputStream fios = ImageIO.createImageOutputStream(temp1);
  50 
  51         // Create a FileCacheImageOutputStream from a BufferedOutputStream
  52         File temp2 = File.createTempFile("imageio", ".tmp");
  53         temp2.deleteOnExit();
  54         FileOutputStream fos2 = new FileOutputStream(temp2);
  55         BufferedOutputStream bos = new BufferedOutputStream(fos2);
  56         ImageOutputStream fcios1 = ImageIO.createImageOutputStream(bos);
  57 
  58         // Create a FileCacheImageOutputStream from a ByteArrayOutputStream
  59         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  60         ImageOutputStream fcios2 = ImageIO.createImageOutputStream(baos);
  61 
  62         BufferedImage bi =
  63             new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR);
  64 
  65         ImageIO.write(bi, "jpg", fios); // No bug, check it anyway
  66         ImageIO.write(bi, "png", fcios1); // Bug 4414990
  67         ImageIO.write(bi, "jpg", fcios2); // Bug 4415041
  68 
  69         // It should not be necessary to flush any of the streams
  70         // If flushing does make a difference, it indicates a bug
  71         // in the writer or the stream implementation
  72 
  73         // Get length of temp1 before and after flushing
  74         long file1NoFlushLength = temp1.length();
  75         fios.flush();
  76         long file1FlushLength = temp1.length();
  77 
  78         // Get length of temp2 before and after flushing
  79         long file2NoFlushLength = temp2.length();
  80         fcios1.flush();
  81         bos.flush();
  82         long file2FlushLength = temp2.length();
  83 
  84         byte[] b0 = baos.toByteArray();
  85         int cacheNoFlushLength = b0.length;
  86         fcios2.flush();
  87         byte[] b1 = baos.toByteArray();
  88         int cacheFlushLength = b1.length;
  89 
  90         if (file1NoFlushLength != file1FlushLength) {
  91             // throw new RuntimeException
  92             System.out.println
  93                 ("FileImageOutputStream not flushed!");
  94         }
  95 
  96         if (file2NoFlushLength != file2FlushLength) {
  97             // throw new RuntimeException
  98             System.out.println
  99              ("FileCacheImageOutputStream/BufferedOutputStream not flushed!");
 100         }
 101 
 102         if (cacheNoFlushLength != cacheFlushLength) {
 103             // throw new RuntimeException
 104             System.out.println
 105             ("FileCacheImageOutputStream/ByteArrayOutputStream not flushed!");
 106         }
 107     }
 108 }