1 7 8 package com.sun.imageio.stream; 9 10 import java.io.IOException ; 11 import java.util.Set ; 12 import java.util.WeakHashMap ; 13 import javax.imageio.stream.ImageInputStream ; 14 15 26 public class StreamCloser { 27 28 private static WeakHashMap <ImageInputStream , Object > toCloseQueue = null; 29 private static Thread streamCloser = null; 30 31 public static void addToQueue(ImageInputStream iis) { 32 synchronized (StreamCloser.class) { 33 if (toCloseQueue == null) { 34 toCloseQueue = 35 new WeakHashMap <ImageInputStream , Object >(); 36 } 37 38 toCloseQueue.put(iis, null); 39 40 if (streamCloser == null) { 41 final Runnable streamCloserRunnable = new Runnable () { 42 public void run() { 43 if (toCloseQueue != null) { 44 synchronized (StreamCloser.class) { 45 Set <ImageInputStream > set = 46 toCloseQueue.keySet(); 47 ImageInputStream [] streams = 48 new ImageInputStream [set.size()]; 49 streams = set.toArray(streams); 50 for (ImageInputStream is : streams) { 51 if (is != null) { 52 try { 53 is.close(); 54 } catch (IOException e) { 55 } 56 } 57 } 58 } 59 } 60 } 61 }; 62 63 java.security.AccessController.doPrivileged( 64 new java.security.PrivilegedAction () { 65 public Object run() { 66 70 ThreadGroup tg = 71 Thread.currentThread().getThreadGroup(); 72 for (ThreadGroup tgn = tg; 73 tgn != null; 74 tg = tgn, tgn = tg.getParent()); 75 streamCloser = new Thread (tg, streamCloserRunnable); 76 Runtime.getRuntime().addShutdownHook(streamCloser); 77 return null; 78 } 79 }); 80 } 81 } 82 } 83 84 public static void removeFromQueue(ImageInputStream iis) { 85 if (toCloseQueue != null) { 86 toCloseQueue.remove(iis); 87 } 88 } 89 } 90 | Popular Tags |