Path: blob/master/test/jdk/javax/imageio/stream/StreamFlush.java
41152 views
/*1* Copyright (c) 2001, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4414990 441504126* @summary Checks that the output is flushed properly when using various27* ImageOutputStreams and writers28*/2930import java.awt.image.BufferedImage;31import java.io.BufferedOutputStream;32import java.io.ByteArrayOutputStream;33import java.io.File;34import java.io.FileOutputStream;35import java.io.IOException;36import java.nio.file.Files;37import java.nio.file.Paths;3839import javax.imageio.ImageIO;40import javax.imageio.stream.ImageOutputStream;4142public class StreamFlush {4344public static void main(String[] args) throws IOException {45ImageIO.setUseCache(true);4647// Create a FileImageOutputStream from a FileOutputStream48File temp1 = File.createTempFile("StreamFlush_fis_", ".tmp");49// Create a FileCacheImageOutputStream from a BufferedOutputStream50File temp2 = File.createTempFile("StreamFlush_bos_", ".tmp");51try (ImageOutputStream fios = ImageIO.createImageOutputStream(temp1);52FileOutputStream fos2 = new FileOutputStream(temp2)) {53test(temp1, fios, temp2, fos2);54} finally {55Files.delete(Paths.get(temp1.getAbsolutePath()));56Files.delete(Paths.get(temp2.getAbsolutePath()));57}58}5960private static void test(File temp1, ImageOutputStream fios, File temp2,61FileOutputStream fos2) throws IOException {62BufferedOutputStream bos = new BufferedOutputStream(fos2);63ImageOutputStream fcios1 = ImageIO.createImageOutputStream(bos);6465// Create a FileCacheImageOutputStream from a ByteArrayOutputStream66ByteArrayOutputStream baos = new ByteArrayOutputStream();67ImageOutputStream fcios2 = ImageIO.createImageOutputStream(baos);6869BufferedImage bi =70new BufferedImage(10, 10, BufferedImage.TYPE_3BYTE_BGR);7172ImageIO.write(bi, "jpg", fios); // No bug, check it anyway73ImageIO.write(bi, "png", fcios1); // Bug 441499074ImageIO.write(bi, "jpg", fcios2); // Bug 44150417576// It should not be necessary to flush any of the streams77// If flushing does make a difference, it indicates a bug78// in the writer or the stream implementation7980// Get length of temp1 before and after flushing81long file1NoFlushLength = temp1.length();82fios.flush();83long file1FlushLength = temp1.length();8485// Get length of temp2 before and after flushing86long file2NoFlushLength = temp2.length();87fcios1.flush();88bos.flush();89long file2FlushLength = temp2.length();9091byte[] b0 = baos.toByteArray();92int cacheNoFlushLength = b0.length;93fcios2.flush();94byte[] b1 = baos.toByteArray();95int cacheFlushLength = b1.length;9697if (file1NoFlushLength != file1FlushLength) {98// throw new RuntimeException99System.out.println100("FileImageOutputStream not flushed!");101}102103if (file2NoFlushLength != file2FlushLength) {104// throw new RuntimeException105System.out.println106("FileCacheImageOutputStream/BufferedOutputStream not flushed!");107}108109if (cacheNoFlushLength != cacheFlushLength) {110// throw new RuntimeException111System.out.println112("FileCacheImageOutputStream/ByteArrayOutputStream not flushed!");113}114}115}116117118