1 /*
2  * Copyright (c) 2001, 2020, 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 import java.nio.file.Files;
38 import java.nio.file.Paths;
39 
40 import javax.imageio.ImageIO;
41 import javax.imageio.stream.ImageOutputStream;
42 
43 public class StreamFlush {
44 
main(String[] args)45     public static void main(String[] args) throws IOException {
46         ImageIO.setUseCache(true);
47 
48         // Create a FileImageOutputStream from a FileOutputStream
49         File temp1 = File.createTempFile("StreamFlush_fis_", ".tmp");
50         // Create a FileCacheImageOutputStream from a BufferedOutputStream
51         File temp2 = File.createTempFile("StreamFlush_bos_", ".tmp");
52         try (ImageOutputStream fios = ImageIO.createImageOutputStream(temp1);
53              FileOutputStream fos2 = new FileOutputStream(temp2)) {
54             test(temp1, fios, temp2, fos2);
55         } finally {
56             Files.delete(Paths.get(temp1.getAbsolutePath()));
57             Files.delete(Paths.get(temp2.getAbsolutePath()));
58         }
59     }
60 
test(File temp1, ImageOutputStream fios, File temp2, FileOutputStream fos2)61     private static void test(File temp1, ImageOutputStream fios, File temp2,
62                              FileOutputStream fos2) throws IOException {
63         BufferedOutputStream bos = new BufferedOutputStream(fos2);
64         ImageOutputStream fcios1 = ImageIO.createImageOutputStream(bos);
65 
66         // Create a FileCacheImageOutputStream from a ByteArrayOutputStream
67         ByteArrayOutputStream baos = new ByteArrayOutputStream();
68         ImageOutputStream fcios2 = ImageIO.createImageOutputStream(baos);
69 
70         BufferedImage bi =
71             new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR);
72 
73         ImageIO.write(bi, "jpg", fios); // No bug, check it anyway
74         ImageIO.write(bi, "png", fcios1); // Bug 4414990
75         ImageIO.write(bi, "jpg", fcios2); // Bug 4415041
76 
77         // It should not be necessary to flush any of the streams
78         // If flushing does make a difference, it indicates a bug
79         // in the writer or the stream implementation
80 
81         // Get length of temp1 before and after flushing
82         long file1NoFlushLength = temp1.length();
83         fios.flush();
84         long file1FlushLength = temp1.length();
85 
86         // Get length of temp2 before and after flushing
87         long file2NoFlushLength = temp2.length();
88         fcios1.flush();
89         bos.flush();
90         long file2FlushLength = temp2.length();
91 
92         byte[] b0 = baos.toByteArray();
93         int cacheNoFlushLength = b0.length;
94         fcios2.flush();
95         byte[] b1 = baos.toByteArray();
96         int cacheFlushLength = b1.length;
97 
98         if (file1NoFlushLength != file1FlushLength) {
99             // throw new RuntimeException
100             System.out.println
101                 ("FileImageOutputStream not flushed!");
102         }
103 
104         if (file2NoFlushLength != file2FlushLength) {
105             // throw new RuntimeException
106             System.out.println
107              ("FileCacheImageOutputStream/BufferedOutputStream not flushed!");
108         }
109 
110         if (cacheNoFlushLength != cacheFlushLength) {
111             // throw new RuntimeException
112             System.out.println
113             ("FileCacheImageOutputStream/ByteArrayOutputStream not flushed!");
114         }
115     }
116 }
117