blob: f39b98eb05d41f14ecb47f3410c99386cd64401d [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package com.sun.imageio.stream;
27
28import java.io.IOException;
29import java.util.Set;
30import java.util.WeakHashMap;
31import javax.imageio.stream.ImageInputStream;
32
33/**
34 * This class provide means to properly close hanging
35 * image input/output streams on VM shutdown.
36 * This might be useful for proper cleanup such as removal
37 * of temporary files.
38 *
39 * Addition of stream do not prevent it from being garbage collected
40 * if no other references to it exists. Stream can be closed
41 * explicitly without removal from StreamCloser queue.
42 * Explicit removal from the queue only helps to save some memory.
43 */
44public class StreamCloser {
45
46 private static WeakHashMap<ImageInputStream, Object> toCloseQueue;
47 private static Thread streamCloser;
48
49 public static void addToQueue(ImageInputStream iis) {
50 synchronized (StreamCloser.class) {
51 if (toCloseQueue == null) {
52 toCloseQueue =
53 new WeakHashMap<ImageInputStream, Object>();
54 }
55
56 toCloseQueue.put(iis, null);
57
58 if (streamCloser == null) {
59 final Runnable streamCloserRunnable = new Runnable() {
60 public void run() {
61 if (toCloseQueue != null) {
62 synchronized (StreamCloser.class) {
63 Set<ImageInputStream> set =
64 toCloseQueue.keySet();
65 // Make a copy of the set in order to avoid
66 // concurrent modification (the is.close()
67 // will in turn call removeFromQueue())
68 ImageInputStream[] streams =
69 new ImageInputStream[set.size()];
70 streams = set.toArray(streams);
71 for (ImageInputStream is : streams) {
72 if (is != null) {
73 try {
74 is.close();
75 } catch (IOException e) {
76 }
77 }
78 }
79 }
80 }
81 }
82 };
83
84 java.security.AccessController.doPrivileged(
85 new java.security.PrivilegedAction() {
86 public Object run() {
87 /* The thread must be a member of a thread group
88 * which will not get GCed before VM exit.
89 * Make its parent the top-level thread group.
90 */
91 ThreadGroup tg =
92 Thread.currentThread().getThreadGroup();
93 for (ThreadGroup tgn = tg;
94 tgn != null;
95 tg = tgn, tgn = tg.getParent());
96 streamCloser = new Thread(tg, streamCloserRunnable);
97 Runtime.getRuntime().addShutdownHook(streamCloser);
98 return null;
99 }
100 });
101 }
102 }
103 }
104
105 public static void removeFromQueue(ImageInputStream iis) {
106 synchronized (StreamCloser.class) {
107 if (toCloseQueue != null) {
108 toCloseQueue.remove(iis);
109 }
110 }
111 }
112}