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 import java.io.ByteArrayInputStream; 25 import java.io.ByteArrayOutputStream; 26 import java.io.File; 27 import java.io.IOException; 28 29 import javax.imageio.ImageIO; 30 import javax.imageio.stream.ImageInputStream; 31 import javax.imageio.stream.ImageOutputStream; 32 33 public class DeleteOnExitTest { 34 public static void main(String[] args) throws IOException { 35 ByteArrayInputStream is = 36 new ByteArrayInputStream(new byte[100]); 37 ByteArrayOutputStream os = 38 new ByteArrayOutputStream(); 39 40 String tmp = System.getProperty("java.io.tmpdir", "."); 41 System.out.println("tmp: " + tmp); 42 43 // count number of files before test 44 ImageIO.setUseCache(true); 45 ImageIO.setCacheDirectory(new File(tmp)); 46 47 File tmpDir = ImageIO.getCacheDirectory(); 48 System.out.println("tmpDir is " + tmpDir); 49 int fnum_before = tmpDir.list().length; 50 System.out.println("Files before test: " + fnum_before); 51 52 ImageInputStream iis = 53 ImageIO.createImageInputStream(is); 54 System.out.println("iis = " + iis); 55 56 ImageInputStream iis2 = 57 ImageIO.createImageInputStream(is); 58 59 ImageOutputStream ios = 60 ImageIO.createImageOutputStream(os); 61 System.out.println("ios = " + ios); 62 63 ImageOutputStream ios2 = 64 ImageIO.createImageOutputStream(os); 65 66 iis2.close(); 67 ios2.close(); 68 int fnum_after = tmpDir.list().length; 69 System.out.println("Files after test: " + fnum_after); 70 71 if (fnum_before == fnum_after) { 72 throw new RuntimeException("Test failed: cache was not used."); 73 } 74 } 75 }