Path: blob/master/test/jdk/javax/imageio/stream/StreamCloserLeak/testapp/Main.java
41155 views
/*1* Copyright (c) 2009, 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*/2223package testapp;2425import java.io.ByteArrayInputStream;26import java.io.File;27import java.io.IOException;28import java.io.InputStream;29import java.util.HashMap;30import java.util.concurrent.ConcurrentLinkedQueue;31import javax.imageio.stream.FileCacheImageInputStream;32import javax.imageio.stream.ImageInputStream;3334public class Main {3536public static void main(String[] args) {37Main o = new Main("testapp.some.class", null);38o.launch(null);39}4041private final String uniqClassName;42private final ConcurrentLinkedQueue<Throwable> problems;4344public Main(String uniq, ConcurrentLinkedQueue<Throwable> p) {45uniqClassName = uniq;46problems = p;47}4849public void launch(HashMap<String, ImageInputStream> refs) {50System.out.printf("%s: current context class loader: %s\n",51uniqClassName,52Thread.currentThread().getContextClassLoader());53try {54byte[] data = new byte[1024];55ByteArrayInputStream bais = new ByteArrayInputStream(data);56MyImageInputStream iis = new MyImageInputStream(bais,57uniqClassName,58problems);59if (refs != null) {60System.out.printf("%s: added to strong store\n",61uniqClassName);62refs.put(uniqClassName, iis);63}64iis.read();65//leave stream open : let's shutdown hook work!66} catch (IOException e) {67problems.add(e);68}69}7071private static class MyImageInputStream extends FileCacheImageInputStream {72private final String uniqClassName;73private ConcurrentLinkedQueue<Throwable> problems;74public MyImageInputStream(InputStream is, String uniq,75ConcurrentLinkedQueue<Throwable> p) throws IOException76{77super(is, new File("tmp"));78uniqClassName = uniq;79problems = p;80}8182@Override83public void close() throws IOException {84Test t = new Test();85try {86t.doTest(uniqClassName);87} catch (Throwable e) {88problems.add(e);89}9091super.close();9293problems = null;94}95}96}9798class Test {99public void doTest(String uniqClassName) throws ClassNotFoundException {100System.out.printf("%s: Current thread: %s\n", uniqClassName,101Thread.currentThread());102103ClassLoader thisCL = this.getClass().getClassLoader();104Class uniq = thisCL.loadClass(uniqClassName);105106System.out.printf("%s: test is done!\n",uniqClassName);107}108}109110111