KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > stream > StreamCloser


1 /*
2  * @(#)StreamCloser.java 1.1 05/08/10
3  *
4  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.stream;
9
10 import java.io.IOException JavaDoc;
11 import java.util.Set JavaDoc;
12 import java.util.WeakHashMap JavaDoc;
13 import javax.imageio.stream.ImageInputStream JavaDoc;
14
15 /**
16  * This class provide means to properly close hanging
17  * image input/output streams on VM shutdown.
18  * This might be usefull for proper cleanup such as removal
19  * of temporary files.
20  *
21  * Addition of stream do not prevent it from being garbage collected
22  * if no other references to it exists. Stream can be closed
23  * explicitly without removal from StremCloser queue.
24  * Explicit removal from the queue only helps to save some memory.
25  */

26 public class StreamCloser {
27
28     private static WeakHashMap JavaDoc<ImageInputStream JavaDoc, Object JavaDoc> toCloseQueue = null;
29     private static Thread JavaDoc streamCloser = null;
30
31     public static void addToQueue(ImageInputStream JavaDoc iis) {
32         synchronized (StreamCloser.class) {
33             if (toCloseQueue == null) {
34                 toCloseQueue =
35                     new WeakHashMap JavaDoc<ImageInputStream JavaDoc, Object JavaDoc>();
36             }
37             
38             toCloseQueue.put(iis, null);
39
40             if (streamCloser == null) {
41                 final Runnable JavaDoc streamCloserRunnable = new Runnable JavaDoc() {
42                     public void run() {
43                         if (toCloseQueue != null) {
44                             synchronized (StreamCloser.class) {
45                                 Set JavaDoc<ImageInputStream JavaDoc> set =
46                                     toCloseQueue.keySet();
47                                 ImageInputStream JavaDoc[] streams =
48                                     new ImageInputStream JavaDoc[set.size()];
49                                 streams = set.toArray(streams);
50                                 for (ImageInputStream JavaDoc is : streams) {
51                                     if (is != null) {
52                                         try {
53                                             is.close();
54                                         } catch (IOException JavaDoc e) {
55                                         }
56                                     }
57                                 }
58                             }
59                         }
60                     }
61                 };
62                 
63                 java.security.AccessController.doPrivileged(
64                     new java.security.PrivilegedAction JavaDoc() {
65                         public Object JavaDoc run() {
66                             /* The thread must be a member of a thread group
67                              * which will not get GCed before VM exit.
68                              * Make its parent the top-level thread group.
69                              */

70                             ThreadGroup JavaDoc tg =
71                                 Thread.currentThread().getThreadGroup();
72                             for (ThreadGroup JavaDoc tgn = tg;
73                                  tgn != null;
74                                  tg = tgn, tgn = tg.getParent());
75                             streamCloser = new Thread JavaDoc(tg, streamCloserRunnable);
76                             Runtime.getRuntime().addShutdownHook(streamCloser);
77                             return null;
78                         }
79                     });
80             }
81         }
82     }
83
84     public static void removeFromQueue(ImageInputStream JavaDoc iis) {
85         if (toCloseQueue != null) {
86             toCloseQueue.remove(iis);
87         }
88     }
89 }
90
Popular Tags