Path: blob/master/test/jdk/javax/imageio/ImageIOWriteFile.java
41145 views
/*1* Copyright (c) 2000, 2017, 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 439317426* @summary Checks that ImageIO.write(..., ..., File) truncates the file27*/2829import java.awt.image.BufferedImage;30import java.io.File;31import java.io.FileOutputStream;3233import javax.imageio.ImageIO;3435public class ImageIOWriteFile {3637public static void main(String[] args) {38long length0 = -1L;39long length1 = -1L;4041try {42BufferedImage bi =43new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);4445File outFile = File.createTempFile("imageiowritefile", ".tmp");4647// Write image to an empty file48outFile.delete();49ImageIO.write(bi, "png", outFile);50length0 = outFile.length();5152// Write a larger file full of junk53outFile.delete();54FileOutputStream fos = new FileOutputStream(outFile);55for (int i = 0; i < length0*2; i++) {56fos.write(1);57}58fos.close();5960// Write image again61ImageIO.write(bi, "png", outFile);62length1 = outFile.length();6364outFile.delete();65} catch (Exception e) {66e.printStackTrace();67throw new RuntimeException("Unexpected exception!");68}6970if (length0 == 0) {71throw new RuntimeException("File length is zero!");72}73if (length1 != length0) {74throw new RuntimeException("File length changed!");75}76}77}787980