package sun.nio.ch;
import java.lang.ref.SoftReference;
import java.lang.reflect.*;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.*;
import java.nio.channels.spi.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import sun.misc.Unsafe;
import sun.misc.Cleaner;
import sun.security.action.GetPropertyAction;
class Util {
private static final int TEMP_BUF_POOL_SIZE = 3;
private static ThreadLocal<SoftReference<ByteBuffer>>[] bufferPool;
@SuppressWarnings("unchecked")
static ThreadLocal<SoftReference<ByteBuffer>>[] createThreadLocalBufferPool() {
return new ThreadLocal[TEMP_BUF_POOL_SIZE];
}
static {
bufferPool = createThreadLocalBufferPool();
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++)
bufferPool[i] = new ThreadLocal<SoftReference<ByteBuffer>>();
}
static ByteBuffer getTemporaryDirectBuffer(int size) {
ByteBuffer buf = null;
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
SoftReference<ByteBuffer> ref = bufferPool[i].get();
if ((ref != null) && ((buf = ref.get()) != null) &&
(buf.capacity() >= size)) {
buf.rewind();
buf.limit(size);
bufferPool[i].set(null);
return buf;
}
}
return ByteBuffer.allocateDirect(size);
}
static void releaseTemporaryDirectBuffer(ByteBuffer buf) {
if (buf == null)
return;
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
SoftReference<ByteBuffer> ref = bufferPool[i].get();
if ((ref == null) || (ref.get() == null)) {
bufferPool[i].set(new SoftReference<ByteBuffer>(buf));
return;
}
}
for (int i=0; i<TEMP_BUF_POOL_SIZE; i++) {
SoftReference<ByteBuffer> ref = bufferPool[i].get();
ByteBuffer inCacheBuf = ref.get();
if ((inCacheBuf == null) || (buf.capacity() > inCacheBuf.capacity())) {
bufferPool[i].set(new SoftReference<ByteBuffer>(buf));
return;
}
}
}
private static class SelectorWrapper {
private Selector sel;
private SelectorWrapper (Selector sel) {
this.sel = sel;
Cleaner.create(this, new Closer(sel));
}
private static class Closer implements Runnable {
private Selector sel;
private Closer (Selector sel) {
this.sel = sel;
}
public void run () {
try {
sel.close();
} catch (Throwable th) {
throw new Error(th);
}
}
}
public Selector get() { return sel;}
}
private static ThreadLocal<SoftReference<SelectorWrapper>> localSelector
= new ThreadLocal<SoftReference<SelectorWrapper>>();
private static ThreadLocal<SelectorWrapper> localSelectorWrapper
= new ThreadLocal<SelectorWrapper>();
static Selector getTemporarySelector(SelectableChannel sc)
throws IOException
{
SoftReference<SelectorWrapper> ref = localSelector.get();
SelectorWrapper selWrapper = null;
Selector sel = null;
if (ref == null
|| ((selWrapper = ref.get()) == null)
|| ((sel = selWrapper.get()) == null)
|| (sel.provider() != sc.provider())) {
sel = sc.provider().openSelector();
localSelector.set(new SoftReference<SelectorWrapper>(
new SelectorWrapper(sel)));
} else {
localSelectorWrapper.set(selWrapper);
}
return sel;
}
static void releaseTemporarySelector(Selector sel)
throws IOException
{
sel.selectNow();
assert sel.keys().isEmpty() : "Temporary selector not empty";
localSelectorWrapper.set(null);
}
static ByteBuffer[] subsequence(ByteBuffer[] bs, int offset, int length) {
if ((offset == 0) && (length == bs.length))
return bs;
int n = length;
ByteBuffer[] bs2 = new ByteBuffer[n];
for (int i = 0; i < n; i++)
bs2[i] = bs[offset + i];
return bs2;
}
static <E> Set<E> ungrowableSet(final Set<E> s) {
return new Set<E>() {
public int size() { return s.size(); }
public boolean isEmpty() { return s.isEmpty(); }
public boolean contains(Object o) { return s.contains(o); }
public Object[] toArray() { return s.toArray(); }
public <T> T[] toArray(T[] a) { return s.toArray(a); }
public String toString() { return s.toString(); }
public Iterator<E> iterator() { return s.iterator(); }
public boolean equals(Object o) { return s.equals(o); }
public int hashCode() { return s.hashCode(); }
public void clear() { s.clear(); }
public boolean remove(Object o) { return s.remove(o); }
public boolean containsAll(Collection<?> coll) {
return s.containsAll(coll);
}
public boolean removeAll(Collection<?> coll) {
return s.removeAll(coll);
}
public boolean retainAll(Collection<?> coll) {
return s.retainAll(coll);
}
public boolean add(E o){
throw new UnsupportedOperationException();
}
public boolean addAll(Collection<? extends E> coll) {
throw new UnsupportedOperationException();
}
};
}
private static Unsafe unsafe = Unsafe.getUnsafe();
private static byte _get(long a) {
return unsafe.getByte(a);
}
private static void _put(long a, byte b) {
unsafe.putByte(a, b);
}
static void erase(ByteBuffer bb) {
unsafe.setMemory(((DirectBuffer)bb).address(), bb.capacity(), (byte)0);
}
static Unsafe unsafe() {
return unsafe;
}
private static int pageSize = -1;
static int pageSize() {
if (pageSize == -1)
pageSize = unsafe().pageSize();
return pageSize;
}
private static volatile Constructor directByteBufferConstructor = null;
private static void initDBBConstructor() {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
try {
Class<?> cl = Class.forName("java.nio.DirectByteBuffer");
Constructor ctor = cl.getDeclaredConstructor(
new Class[] { int.class,
long.class,
Runnable.class });
ctor.setAccessible(true);
directByteBufferConstructor = ctor;
} catch (ClassNotFoundException x) {
throw new InternalError();
} catch (NoSuchMethodException x) {
throw new InternalError();
} catch (IllegalArgumentException x) {
throw new InternalError();
} catch (ClassCastException x) {
throw new InternalError();
}
return null;
}});
}
static MappedByteBuffer newMappedByteBuffer(int size, long addr,
Runnable unmapper)
{
MappedByteBuffer dbb;
if (directByteBufferConstructor == null)
initDBBConstructor();
try {
dbb = (MappedByteBuffer)directByteBufferConstructor.newInstance(
new Object[] { new Integer(size),
new Long(addr),
unmapper });
} catch (InstantiationException e) {
throw new InternalError();
} catch (IllegalAccessException e) {
throw new InternalError();
} catch (InvocationTargetException e) {
throw new InternalError();
}
return dbb;
}
private static volatile Constructor directByteBufferRConstructor = null;
private static void initDBBRConstructor() {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
try {
Class<?> cl = Class.forName("java.nio.DirectByteBufferR");
Constructor ctor = cl.getDeclaredConstructor(
new Class[] { int.class,
long.class,
Runnable.class });
ctor.setAccessible(true);
directByteBufferRConstructor = ctor;
} catch (ClassNotFoundException x) {
throw new InternalError();
} catch (NoSuchMethodException x) {
throw new InternalError();
} catch (IllegalArgumentException x) {
throw new InternalError();
} catch (ClassCastException x) {
throw new InternalError();
}
return null;
}});
}
static MappedByteBuffer newMappedByteBufferR(int size, long addr,
Runnable unmapper)
{
MappedByteBuffer dbb;
if (directByteBufferRConstructor == null)
initDBBRConstructor();
try {
dbb = (MappedByteBuffer)directByteBufferRConstructor.newInstance(
new Object[] { new Integer(size),
new Long(addr),
unmapper });
} catch (InstantiationException e) {
throw new InternalError();
} catch (IllegalAccessException e) {
throw new InternalError();
} catch (InvocationTargetException e) {
throw new InternalError();
}
return dbb;
}
private static volatile String bugLevel = null;
static boolean atBugLevel(String bl) {
if (bugLevel == null) {
if (!sun.misc.VM.isBooted())
return false;
String value = AccessController.doPrivileged(
new GetPropertyAction("sun.nio.ch.bugLevel"));
bugLevel = (value != null) ? value : "";
}
return bugLevel.equals(bl);
}
}