Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/nio/channels/spi/AsynchronousChannelProvider.java
41161 views
1
/*
2
* Copyright (c) 2007, 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 java.nio.channels.spi;
27
28
import java.nio.channels.*;
29
import java.io.IOException;
30
import java.util.Iterator;
31
import java.util.ServiceLoader;
32
import java.util.ServiceConfigurationError;
33
import java.util.concurrent.*;
34
import java.security.AccessController;
35
import java.security.PrivilegedAction;
36
37
/**
38
* Service-provider class for asynchronous channels.
39
*
40
* <p> An asynchronous channel provider is a concrete subclass of this class that
41
* has a zero-argument constructor and implements the abstract methods specified
42
* below. A given invocation of the Java virtual machine maintains a single
43
* system-wide default provider instance, which is returned by the {@link
44
* #provider() provider} method. The first invocation of that method will locate
45
* the default provider as specified below.
46
*
47
* <p> All of the methods in this class are safe for use by multiple concurrent
48
* threads. </p>
49
*
50
* @since 1.7
51
*/
52
53
public abstract class AsynchronousChannelProvider {
54
private static Void checkPermission() {
55
@SuppressWarnings("removal")
56
SecurityManager sm = System.getSecurityManager();
57
if (sm != null)
58
sm.checkPermission(new RuntimePermission("asynchronousChannelProvider"));
59
return null;
60
}
61
private AsynchronousChannelProvider(Void ignore) { }
62
63
/**
64
* Initializes a new instance of this class.
65
*
66
* @throws SecurityException
67
* If a security manager has been installed and it denies
68
* {@link RuntimePermission}{@code ("asynchronousChannelProvider")}
69
*/
70
protected AsynchronousChannelProvider() {
71
this(checkPermission());
72
}
73
74
// lazy initialization of default provider
75
private static class ProviderHolder {
76
static final AsynchronousChannelProvider provider = load();
77
78
@SuppressWarnings("removal")
79
private static AsynchronousChannelProvider load() {
80
return AccessController
81
.doPrivileged(new PrivilegedAction<>() {
82
public AsynchronousChannelProvider run() {
83
AsynchronousChannelProvider p;
84
p = loadProviderFromProperty();
85
if (p != null)
86
return p;
87
p = loadProviderAsService();
88
if (p != null)
89
return p;
90
return sun.nio.ch.DefaultAsynchronousChannelProvider.create();
91
}});
92
}
93
94
private static AsynchronousChannelProvider loadProviderFromProperty() {
95
String cn = System.getProperty("java.nio.channels.spi.AsynchronousChannelProvider");
96
if (cn == null)
97
return null;
98
try {
99
@SuppressWarnings("deprecation")
100
Object tmp = Class.forName(cn, true,
101
ClassLoader.getSystemClassLoader()).newInstance();
102
return (AsynchronousChannelProvider)tmp;
103
} catch (ClassNotFoundException x) {
104
throw new ServiceConfigurationError(null, x);
105
} catch (IllegalAccessException x) {
106
throw new ServiceConfigurationError(null, x);
107
} catch (InstantiationException x) {
108
throw new ServiceConfigurationError(null, x);
109
} catch (SecurityException x) {
110
throw new ServiceConfigurationError(null, x);
111
}
112
}
113
114
private static AsynchronousChannelProvider loadProviderAsService() {
115
ServiceLoader<AsynchronousChannelProvider> sl =
116
ServiceLoader.load(AsynchronousChannelProvider.class,
117
ClassLoader.getSystemClassLoader());
118
Iterator<AsynchronousChannelProvider> i = sl.iterator();
119
for (;;) {
120
try {
121
return (i.hasNext()) ? i.next() : null;
122
} catch (ServiceConfigurationError sce) {
123
if (sce.getCause() instanceof SecurityException) {
124
// Ignore the security exception, try the next provider
125
continue;
126
}
127
throw sce;
128
}
129
}
130
}
131
}
132
133
/**
134
* Returns the system-wide default asynchronous channel provider for this
135
* invocation of the Java virtual machine.
136
*
137
* <p> The first invocation of this method locates the default provider
138
* object as follows: </p>
139
*
140
* <ol>
141
*
142
* <li><p> If the system property
143
* {@systemProperty java.nio.channels.spi.AsynchronousChannelProvider} is
144
* defined then it is taken to be the fully-qualified name of a concrete
145
* provider class. The class is loaded and instantiated; if this process
146
* fails then an unspecified error is thrown. </p></li>
147
*
148
* <li><p> If a provider class has been installed in a jar file that is
149
* visible to the system class loader, and that jar file contains a
150
* provider-configuration file named
151
* {@code java.nio.channels.spi.AsynchronousChannelProvider} in the resource
152
* directory {@code META-INF/services}, then the first class name
153
* specified in that file is taken. The class is loaded and
154
* instantiated; if this process fails then an unspecified error is
155
* thrown. </p></li>
156
*
157
* <li><p> Finally, if no provider has been specified by any of the above
158
* means then the system-default provider class is instantiated and the
159
* result is returned. </p></li>
160
*
161
* </ol>
162
*
163
* <p> Subsequent invocations of this method return the provider that was
164
* returned by the first invocation. </p>
165
*
166
* @return The system-wide default AsynchronousChannel provider
167
*/
168
public static AsynchronousChannelProvider provider() {
169
return ProviderHolder.provider;
170
}
171
172
/**
173
* Constructs a new asynchronous channel group with a fixed thread pool.
174
*
175
* @param nThreads
176
* The number of threads in the pool
177
* @param threadFactory
178
* The factory to use when creating new threads
179
*
180
* @return A new asynchronous channel group
181
*
182
* @throws IllegalArgumentException
183
* If {@code nThreads <= 0}
184
* @throws IOException
185
* If an I/O error occurs
186
*
187
* @see AsynchronousChannelGroup#withFixedThreadPool
188
*/
189
public abstract AsynchronousChannelGroup
190
openAsynchronousChannelGroup(int nThreads, ThreadFactory threadFactory) throws IOException;
191
192
/**
193
* Constructs a new asynchronous channel group with the given thread pool.
194
*
195
* @param executor
196
* The thread pool
197
* @param initialSize
198
* A value {@code >=0} or a negative value for implementation
199
* specific default
200
*
201
* @return A new asynchronous channel group
202
*
203
* @throws IOException
204
* If an I/O error occurs
205
*
206
* @see AsynchronousChannelGroup#withCachedThreadPool
207
*/
208
public abstract AsynchronousChannelGroup
209
openAsynchronousChannelGroup(ExecutorService executor, int initialSize) throws IOException;
210
211
/**
212
* Opens an asynchronous server-socket channel.
213
*
214
* @param group
215
* The group to which the channel is bound, or {@code null} to
216
* bind to the default group
217
*
218
* @return The new channel
219
*
220
* @throws IllegalChannelGroupException
221
* If the provider that created the group differs from this provider
222
* @throws ShutdownChannelGroupException
223
* The group is shutdown
224
* @throws IOException
225
* If an I/O error occurs
226
*/
227
public abstract AsynchronousServerSocketChannel openAsynchronousServerSocketChannel
228
(AsynchronousChannelGroup group) throws IOException;
229
230
/**
231
* Opens an asynchronous socket channel.
232
*
233
* @param group
234
* The group to which the channel is bound, or {@code null} to
235
* bind to the default group
236
*
237
* @return The new channel
238
*
239
* @throws IllegalChannelGroupException
240
* If the provider that created the group differs from this provider
241
* @throws ShutdownChannelGroupException
242
* The group is shutdown
243
* @throws IOException
244
* If an I/O error occurs
245
*/
246
public abstract AsynchronousSocketChannel openAsynchronousSocketChannel
247
(AsynchronousChannelGroup group) throws IOException;
248
}
249
250