Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/com/sun/imageio/stream/StreamCloser.java
41161 views
1
/*
2
* Copyright (c) 2005, 2021, 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package com.sun.imageio.stream;
27
28
import sun.awt.util.ThreadGroupUtils;
29
30
import java.io.IOException;
31
import java.security.AccessController;
32
import java.security.PrivilegedAction;
33
import java.util.Set;
34
import java.util.WeakHashMap;
35
import javax.imageio.stream.ImageInputStream;
36
37
/**
38
* This class provide means to properly close hanging
39
* image input/output streams on VM shutdown.
40
* This might be useful for proper cleanup such as removal
41
* of temporary files.
42
*
43
* Addition of stream do not prevent it from being garbage collected
44
* if no other references to it exists. Stream can be closed
45
* explicitly without removal from StreamCloser queue.
46
* Explicit removal from the queue only helps to save some memory.
47
*/
48
public class StreamCloser {
49
50
private static WeakHashMap<CloseAction, Object> toCloseQueue;
51
private static Thread streamCloser;
52
53
@SuppressWarnings("removal")
54
public static void addToQueue(CloseAction ca) {
55
synchronized (StreamCloser.class) {
56
if (toCloseQueue == null) {
57
toCloseQueue =
58
new WeakHashMap<CloseAction, Object>();
59
}
60
61
toCloseQueue.put(ca, null);
62
63
if (streamCloser == null) {
64
final Runnable streamCloserRunnable = new Runnable() {
65
public void run() {
66
if (toCloseQueue != null) {
67
synchronized (StreamCloser.class) {
68
Set<CloseAction> set =
69
toCloseQueue.keySet();
70
// Make a copy of the set in order to avoid
71
// concurrent modification (the is.close()
72
// will in turn call removeFromQueue())
73
CloseAction[] actions =
74
new CloseAction[set.size()];
75
actions = set.toArray(actions);
76
for (CloseAction ca : actions) {
77
if (ca != null) {
78
try {
79
ca.performAction();
80
} catch (IOException e) {
81
}
82
}
83
}
84
}
85
}
86
}
87
};
88
89
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
90
/* The thread must be a member of a thread group
91
* which will not get GCed before VM exit.
92
* Make its parent the top-level thread group.
93
*/
94
ThreadGroup tg = ThreadGroupUtils.getRootThreadGroup();
95
streamCloser = new Thread(tg, streamCloserRunnable,
96
"StreamCloser", 0, false);
97
/* Set context class loader to null in order to avoid
98
* keeping a strong reference to an application classloader.
99
*/
100
streamCloser.setContextClassLoader(null);
101
Runtime.getRuntime().addShutdownHook(streamCloser);
102
return null;
103
});
104
}
105
}
106
}
107
108
public static void removeFromQueue(CloseAction ca) {
109
synchronized (StreamCloser.class) {
110
if (toCloseQueue != null) {
111
toCloseQueue.remove(ca);
112
}
113
}
114
}
115
116
public static CloseAction createCloseAction(ImageInputStream iis) {
117
return new CloseAction(iis);
118
}
119
120
public static final class CloseAction {
121
private ImageInputStream iis;
122
123
private CloseAction(ImageInputStream iis) {
124
this.iis = iis;
125
}
126
127
public void performAction() throws IOException {
128
if (iis != null) {
129
iis.close();
130
}
131
}
132
}
133
}
134
135