2
|
1 |
/*
|
|
2 |
* Copyright 2000-2007 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 |
|
|
26 |
package sun.nio.ch;
|
|
27 |
|
|
28 |
import java.lang.ref.SoftReference;
|
|
29 |
import java.lang.reflect.*;
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.nio.ByteBuffer;
|
|
32 |
import java.nio.MappedByteBuffer;
|
|
33 |
import java.nio.channels.*;
|
|
34 |
import java.nio.channels.spi.*;
|
|
35 |
import java.security.AccessController;
|
|
36 |
import java.security.PrivilegedAction;
|
|
37 |
import java.util.*;
|
|
38 |
import sun.misc.Unsafe;
|
|
39 |
import sun.misc.Cleaner;
|
|
40 |
import sun.security.action.GetPropertyAction;
|
|
41 |
|
|
42 |
|
|
43 |
class Util {
|
|
44 |
|
|
45 |
|
|
46 |
// -- Caches --
|
|
47 |
|
|
48 |
// The number of temp buffers in our pool
|
|
49 |
private static final int TEMP_BUF_POOL_SIZE = 3;
|
|
50 |
|
|
51 |
// Per-thread soft cache of the last temporary direct buffer
|
51
|
52 |
private static ThreadLocal<SoftReference<ByteBuffer>>[] bufferPool;
|
2
|
53 |
|
|
54 |
static {
|
51
|
55 |
bufferPool = (ThreadLocal<SoftReference<ByteBuffer>>[])
|
|
56 |
new ThreadLocal[TEMP_BUF_POOL_SIZE];
|
2
|
57 |
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++)
|
51
|
58 |
bufferPool[i] = new ThreadLocal<SoftReference<ByteBuffer>>();
|
2
|
59 |
}
|
|
60 |
|
|
61 |
static ByteBuffer getTemporaryDirectBuffer(int size) {
|
|
62 |
ByteBuffer buf = null;
|
|
63 |
// Grab a buffer if available
|
|
64 |
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
|
51
|
65 |
SoftReference<ByteBuffer> ref = bufferPool[i].get();
|
|
66 |
if ((ref != null) && ((buf = ref.get()) != null) &&
|
2
|
67 |
(buf.capacity() >= size)) {
|
|
68 |
buf.rewind();
|
|
69 |
buf.limit(size);
|
|
70 |
bufferPool[i].set(null);
|
|
71 |
return buf;
|
|
72 |
}
|
|
73 |
}
|
|
74 |
|
|
75 |
// Make a new one
|
|
76 |
return ByteBuffer.allocateDirect(size);
|
|
77 |
}
|
|
78 |
|
|
79 |
static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
|
|
80 |
if (buf == null)
|
|
81 |
return;
|
|
82 |
// Put it in an empty slot if such exists
|
|
83 |
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
|
51
|
84 |
SoftReference<ByteBuffer> ref = bufferPool[i].get();
|
2
|
85 |
if ((ref == null) || (ref.get() == null)) {
|
51
|
86 |
bufferPool[i].set(new SoftReference<ByteBuffer>(buf));
|
2
|
87 |
return;
|
|
88 |
}
|
|
89 |
}
|
|
90 |
// Otherwise replace a smaller one in the cache if such exists
|
|
91 |
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
|
51
|
92 |
SoftReference<ByteBuffer> ref = bufferPool[i].get();
|
|
93 |
ByteBuffer inCacheBuf = ref.get();
|
2
|
94 |
if ((inCacheBuf == null) || (buf.capacity() > inCacheBuf.capacity())) {
|
51
|
95 |
bufferPool[i].set(new SoftReference<ByteBuffer>(buf));
|
2
|
96 |
return;
|
|
97 |
}
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
|
101 |
private static class SelectorWrapper {
|
|
102 |
private Selector sel;
|
|
103 |
private SelectorWrapper (Selector sel) {
|
|
104 |
this.sel = sel;
|
|
105 |
Cleaner.create(this, new Closer(sel));
|
|
106 |
}
|
|
107 |
private static class Closer implements Runnable {
|
|
108 |
private Selector sel;
|
|
109 |
private Closer (Selector sel) {
|
|
110 |
this.sel = sel;
|
|
111 |
}
|
|
112 |
public void run () {
|
|
113 |
try {
|
|
114 |
sel.close();
|
|
115 |
} catch (Throwable th) {
|
|
116 |
throw new Error(th);
|
|
117 |
}
|
|
118 |
}
|
|
119 |
}
|
|
120 |
public Selector get() { return sel;}
|
|
121 |
}
|
|
122 |
|
|
123 |
// Per-thread cached selector
|
51
|
124 |
private static ThreadLocal<SoftReference<SelectorWrapper>> localSelector
|
|
125 |
= new ThreadLocal<SoftReference<SelectorWrapper>>();
|
2
|
126 |
// Hold a reference to the selWrapper object to prevent it from
|
|
127 |
// being cleaned when the temporary selector wrapped is on lease.
|
51
|
128 |
private static ThreadLocal<SelectorWrapper> localSelectorWrapper
|
|
129 |
= new ThreadLocal<SelectorWrapper>();
|
2
|
130 |
|
|
131 |
// When finished, invoker must ensure that selector is empty
|
|
132 |
// by cancelling any related keys and explicitly releasing
|
|
133 |
// the selector by invoking releaseTemporarySelector()
|
|
134 |
static Selector getTemporarySelector(SelectableChannel sc)
|
|
135 |
throws IOException
|
|
136 |
{
|
51
|
137 |
SoftReference<SelectorWrapper> ref = localSelector.get();
|
2
|
138 |
SelectorWrapper selWrapper = null;
|
|
139 |
Selector sel = null;
|
|
140 |
if (ref == null
|
51
|
141 |
|| ((selWrapper = ref.get()) == null)
|
2
|
142 |
|| ((sel = selWrapper.get()) == null)
|
|
143 |
|| (sel.provider() != sc.provider())) {
|
|
144 |
sel = sc.provider().openSelector();
|
51
|
145 |
localSelector.set(new SoftReference<SelectorWrapper>(
|
|
146 |
new SelectorWrapper(sel)));
|
2
|
147 |
} else {
|
|
148 |
localSelectorWrapper.set(selWrapper);
|
|
149 |
}
|
|
150 |
return sel;
|
|
151 |
}
|
|
152 |
|
|
153 |
static void releaseTemporarySelector(Selector sel)
|
|
154 |
throws IOException
|
|
155 |
{
|
|
156 |
// Selector should be empty
|
|
157 |
sel.selectNow(); // Flush cancelled keys
|
|
158 |
assert sel.keys().isEmpty() : "Temporary selector not empty";
|
|
159 |
localSelectorWrapper.set(null);
|
|
160 |
}
|
|
161 |
|
|
162 |
|
|
163 |
// -- Random stuff --
|
|
164 |
|
|
165 |
static ByteBuffer[] subsequence(ByteBuffer[] bs, int offset, int length) {
|
|
166 |
if ((offset == 0) && (length == bs.length))
|
|
167 |
return bs;
|
|
168 |
int n = length;
|
|
169 |
ByteBuffer[] bs2 = new ByteBuffer[n];
|
|
170 |
for (int i = 0; i < n; i++)
|
|
171 |
bs2[i] = bs[offset + i];
|
|
172 |
return bs2;
|
|
173 |
}
|
|
174 |
|
|
175 |
static <E> Set<E> ungrowableSet(final Set<E> s) {
|
|
176 |
return new Set<E>() {
|
|
177 |
|
|
178 |
public int size() { return s.size(); }
|
|
179 |
public boolean isEmpty() { return s.isEmpty(); }
|
|
180 |
public boolean contains(Object o) { return s.contains(o); }
|
|
181 |
public Object[] toArray() { return s.toArray(); }
|
|
182 |
public <T> T[] toArray(T[] a) { return s.toArray(a); }
|
|
183 |
public String toString() { return s.toString(); }
|
|
184 |
public Iterator<E> iterator() { return s.iterator(); }
|
|
185 |
public boolean equals(Object o) { return s.equals(o); }
|
|
186 |
public int hashCode() { return s.hashCode(); }
|
|
187 |
public void clear() { s.clear(); }
|
|
188 |
public boolean remove(Object o) { return s.remove(o); }
|
|
189 |
|
|
190 |
public boolean containsAll(Collection<?> coll) {
|
|
191 |
return s.containsAll(coll);
|
|
192 |
}
|
|
193 |
public boolean removeAll(Collection<?> coll) {
|
|
194 |
return s.removeAll(coll);
|
|
195 |
}
|
|
196 |
public boolean retainAll(Collection<?> coll) {
|
|
197 |
return s.retainAll(coll);
|
|
198 |
}
|
|
199 |
|
|
200 |
public boolean add(E o){
|
|
201 |
throw new UnsupportedOperationException();
|
|
202 |
}
|
|
203 |
public boolean addAll(Collection<? extends E> coll) {
|
|
204 |
throw new UnsupportedOperationException();
|
|
205 |
}
|
|
206 |
|
|
207 |
};
|
|
208 |
}
|
|
209 |
|
|
210 |
|
|
211 |
// -- Unsafe access --
|
|
212 |
|
|
213 |
private static Unsafe unsafe = Unsafe.getUnsafe();
|
|
214 |
|
|
215 |
private static byte _get(long a) {
|
|
216 |
return unsafe.getByte(a);
|
|
217 |
}
|
|
218 |
|
|
219 |
private static void _put(long a, byte b) {
|
|
220 |
unsafe.putByte(a, b);
|
|
221 |
}
|
|
222 |
|
|
223 |
static void erase(ByteBuffer bb) {
|
|
224 |
unsafe.setMemory(((DirectBuffer)bb).address(), bb.capacity(), (byte)0);
|
|
225 |
}
|
|
226 |
|
|
227 |
static Unsafe unsafe() {
|
|
228 |
return unsafe;
|
|
229 |
}
|
|
230 |
|
|
231 |
private static int pageSize = -1;
|
|
232 |
|
|
233 |
static int pageSize() {
|
|
234 |
if (pageSize == -1)
|
|
235 |
pageSize = unsafe().pageSize();
|
|
236 |
return pageSize;
|
|
237 |
}
|
|
238 |
|
|
239 |
private static volatile Constructor directByteBufferConstructor = null;
|
|
240 |
|
|
241 |
private static void initDBBConstructor() {
|
51
|
242 |
AccessController.doPrivileged(new PrivilegedAction<Void>() {
|
|
243 |
public Void run() {
|
2
|
244 |
try {
|
51
|
245 |
Class<?> cl = Class.forName("java.nio.DirectByteBuffer");
|
2
|
246 |
Constructor ctor = cl.getDeclaredConstructor(
|
|
247 |
new Class[] { int.class,
|
|
248 |
long.class,
|
|
249 |
Runnable.class });
|
|
250 |
ctor.setAccessible(true);
|
|
251 |
directByteBufferConstructor = ctor;
|
|
252 |
} catch (ClassNotFoundException x) {
|
|
253 |
throw new InternalError();
|
|
254 |
} catch (NoSuchMethodException x) {
|
|
255 |
throw new InternalError();
|
|
256 |
} catch (IllegalArgumentException x) {
|
|
257 |
throw new InternalError();
|
|
258 |
} catch (ClassCastException x) {
|
|
259 |
throw new InternalError();
|
|
260 |
}
|
|
261 |
return null;
|
|
262 |
}});
|
|
263 |
}
|
|
264 |
|
|
265 |
static MappedByteBuffer newMappedByteBuffer(int size, long addr,
|
|
266 |
Runnable unmapper)
|
|
267 |
{
|
|
268 |
MappedByteBuffer dbb;
|
|
269 |
if (directByteBufferConstructor == null)
|
|
270 |
initDBBConstructor();
|
|
271 |
try {
|
|
272 |
dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance(
|
|
273 |
new Object[] { new Integer(size),
|
|
274 |
new Long(addr),
|
|
275 |
unmapper });
|
|
276 |
} catch (InstantiationException e) {
|
|
277 |
throw new InternalError();
|
|
278 |
} catch (IllegalAccessException e) {
|
|
279 |
throw new InternalError();
|
|
280 |
} catch (InvocationTargetException e) {
|
|
281 |
throw new InternalError();
|
|
282 |
}
|
|
283 |
return dbb;
|
|
284 |
}
|
|
285 |
|
|
286 |
private static volatile Constructor directByteBufferRConstructor = null;
|
|
287 |
|
|
288 |
private static void initDBBRConstructor() {
|
51
|
289 |
AccessController.doPrivileged(new PrivilegedAction<Void>() {
|
|
290 |
public Void run() {
|
2
|
291 |
try {
|
51
|
292 |
Class<?> cl = Class.forName("java.nio.DirectByteBufferR");
|
2
|
293 |
Constructor ctor = cl.getDeclaredConstructor(
|
|
294 |
new Class[] { int.class,
|
|
295 |
long.class,
|
|
296 |
Runnable.class });
|
|
297 |
ctor.setAccessible(true);
|
|
298 |
directByteBufferRConstructor = ctor;
|
|
299 |
} catch (ClassNotFoundException x) {
|
|
300 |
throw new InternalError();
|
|
301 |
} catch (NoSuchMethodException x) {
|
|
302 |
throw new InternalError();
|
|
303 |
} catch (IllegalArgumentException x) {
|
|
304 |
throw new InternalError();
|
|
305 |
} catch (ClassCastException x) {
|
|
306 |
throw new InternalError();
|
|
307 |
}
|
|
308 |
return null;
|
|
309 |
}});
|
|
310 |
}
|
|
311 |
|
|
312 |
static MappedByteBuffer newMappedByteBufferR(int size, long addr,
|
|
313 |
Runnable unmapper)
|
|
314 |
{
|
|
315 |
MappedByteBuffer dbb;
|
|
316 |
if (directByteBufferRConstructor == null)
|
|
317 |
initDBBRConstructor();
|
|
318 |
try {
|
|
319 |
dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance(
|
|
320 |
new Object[] { new Integer(size),
|
|
321 |
new Long(addr),
|
|
322 |
unmapper });
|
|
323 |
} catch (InstantiationException e) {
|
|
324 |
throw new InternalError();
|
|
325 |
} catch (IllegalAccessException e) {
|
|
326 |
throw new InternalError();
|
|
327 |
} catch (InvocationTargetException e) {
|
|
328 |
throw new InternalError();
|
|
329 |
}
|
|
330 |
return dbb;
|
|
331 |
}
|
|
332 |
|
|
333 |
|
|
334 |
// -- Bug compatibility --
|
|
335 |
|
|
336 |
private static volatile String bugLevel = null;
|
|
337 |
|
|
338 |
static boolean atBugLevel(String bl) { // package-private
|
|
339 |
if (bugLevel == null) {
|
|
340 |
if (!sun.misc.VM.isBooted())
|
|
341 |
return false;
|
|
342 |
String value = AccessController.doPrivileged(
|
|
343 |
new GetPropertyAction("sun.nio.ch.bugLevel"));
|
|
344 |
bugLevel = (value != null) ? value : "";
|
|
345 |
}
|
|
346 |
return bugLevel.equals(bl);
|
|
347 |
}
|
|
348 |
|
|
349 |
|
|
350 |
|
|
351 |
// -- Initialization --
|
|
352 |
|
|
353 |
private static boolean loaded = false;
|
|
354 |
|
|
355 |
static void load() {
|
|
356 |
synchronized (Util.class) {
|
|
357 |
if (loaded)
|
|
358 |
return;
|
|
359 |
loaded = true;
|
|
360 |
java.security.AccessController
|
|
361 |
.doPrivileged(new sun.security.action.LoadLibraryAction("net"));
|
|
362 |
java.security.AccessController
|
|
363 |
.doPrivileged(new sun.security.action.LoadLibraryAction("nio"));
|
|
364 |
// IOUtil must be initialized; Its native methods are called from
|
|
365 |
// other places in native nio code so they must be set up.
|
|
366 |
IOUtil.initIDs();
|
|
367 |
}
|
|
368 |
}
|
|
369 |
|
|
370 |
}
|