upgrade to java 21, remove netty unsafe, remove deprecated features
This commit is contained in:
parent
b14e539464
commit
18be47866c
58 changed files with 120 additions and 2887 deletions
|
@ -31,7 +31,7 @@ dependencies {
|
|||
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(8)
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2214,7 +2214,7 @@ public class Client implements IThreadListener {
|
|||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
public void run(long time) {
|
||||
Log.SYSTEM.info("Java " + System.getProperty("java.version"));
|
||||
Log.SYSTEM.info(VERSION + " (Protokoll #" + Util.PROTOCOL + ")");
|
||||
if(!Window.createWindow(VERSION, System.getProperty("opengl.debug") != null))
|
||||
|
@ -2240,6 +2240,7 @@ public class Client implements IThreadListener {
|
|||
// this.startSound(true);
|
||||
this.getVar("tic_target").setDefault();
|
||||
this.displayGuiScreen(GuiMenu.INSTANCE);
|
||||
Log.SYSTEM.info("Client gestartet in " + String.format("%.1f", (double)(System.currentTimeMillis() - time) / 1000.0) + " Sekunden");
|
||||
|
||||
while(!this.interrupted) {
|
||||
PerfSection.swap();
|
||||
|
@ -2733,13 +2734,15 @@ public class Client implements IThreadListener {
|
|||
|
||||
|
||||
public static void main(String[] args) {
|
||||
Util.checkOs();
|
||||
Log.init(CLIENT);
|
||||
long time = System.currentTimeMillis();
|
||||
Util.checkPlatform();
|
||||
Log.init();
|
||||
Log.setSync(CLIENT);
|
||||
Window.init();
|
||||
ModelBlock.setAsProvider();
|
||||
Registry.setup("Render thread");
|
||||
UniverseRegistry.register();
|
||||
CLIENT.run();
|
||||
CLIENT.run(time);
|
||||
Window.end();
|
||||
}
|
||||
|
||||
|
|
|
@ -3,6 +3,8 @@ package client.gui.element;
|
|||
import common.util.Util;
|
||||
|
||||
public class PasswordField extends Field {
|
||||
private byte[] passwordHash;
|
||||
|
||||
public PasswordField(int x, int y, int w, int h, int cap, FieldCallback callback, String text) {
|
||||
super(x, y, w, h, cap, callback, text);
|
||||
}
|
||||
|
@ -24,4 +26,9 @@ public class PasswordField extends Field {
|
|||
protected boolean canCopy() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void updateText() {
|
||||
super.updateText();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,6 @@ plugins {
|
|||
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(8)
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ public abstract class Registry {
|
|||
StringBuilder sb = new StringBuilder();
|
||||
Error error = new Error();
|
||||
for(ThreadInfo threadinfo : info) {
|
||||
if(threadinfo.getThreadId() == thread.getId())
|
||||
if(threadinfo.getThreadId() == thread.threadId())
|
||||
error.setStackTrace(threadinfo.getStackTrace());
|
||||
sb.append(threadinfo);
|
||||
}
|
||||
|
|
|
@ -112,9 +112,13 @@ public enum Log {
|
|||
return time > 0 ? String.format("%dD+%02d:%02d:%02d", time, hrs, mins, secs) : String.format("%02d:%02d:%02d", hrs, mins, secs);
|
||||
}
|
||||
|
||||
public static void init(IThreadListener sync) {
|
||||
Log.sync = sync;
|
||||
public static void init() {
|
||||
Log.colors = System.getProperty("log.nocolor") == null && !Util.WINDOWS;
|
||||
Log.level = LogLevel.parse(System.getProperty("log.level", LogLevel.INFO.id));
|
||||
}
|
||||
|
||||
public static void setSync(IThreadListener sync) {
|
||||
Log.sync = sync;
|
||||
}
|
||||
|
||||
public static void flushLog() {
|
||||
|
|
|
@ -2,6 +2,7 @@ package common.log;
|
|||
|
||||
import common.color.TextColor;
|
||||
import common.util.Displayable;
|
||||
import common.util.ExtMath;
|
||||
import common.util.Identifyable;
|
||||
|
||||
public enum LogLevel implements Displayable, Identifyable {
|
||||
|
@ -19,6 +20,19 @@ public enum LogLevel implements Displayable, Identifyable {
|
|||
public final String log;
|
||||
public final TextColor color;
|
||||
|
||||
public static LogLevel parse(String str) {
|
||||
for(LogLevel level : values()) {
|
||||
if(level.id.equalsIgnoreCase(str))
|
||||
return level;
|
||||
}
|
||||
try {
|
||||
return values()[ExtMath.clampi(Integer.parseUnsignedInt(str), 0, values().length - 1)];
|
||||
}
|
||||
catch(NumberFormatException e) {
|
||||
return INFO;
|
||||
}
|
||||
}
|
||||
|
||||
private LogLevel(String id, String name, String log, TextColor color) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
|
|
|
@ -441,7 +441,7 @@ public abstract class AbstractBootstrap<B extends AbstractBootstrap<B, C>, C ext
|
|||
@Override
|
||||
public T newChannel() {
|
||||
try {
|
||||
return clazz.newInstance();
|
||||
return clazz.getConstructor().newInstance();
|
||||
} catch (Throwable t) {
|
||||
throw new ChannelException("Unable to create Channel from class " + clazz, t);
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ package common.net.buffer;
|
|||
|
||||
import common.net.util.ResourceLeak;
|
||||
import common.net.util.ResourceLeakDetector;
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
import common.net.util.internal.StringUtil;
|
||||
|
||||
/**
|
||||
|
@ -48,72 +47,45 @@ public abstract class AbstractByteBufAllocator implements ByteBufAllocator {
|
|||
return buf;
|
||||
}
|
||||
|
||||
private final boolean directByDefault;
|
||||
private final ByteBuf emptyBuf;
|
||||
|
||||
/**
|
||||
* Instance use heap buffers by default
|
||||
*/
|
||||
protected AbstractByteBufAllocator() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new instance
|
||||
*
|
||||
* @param preferDirect {@code true} if {@link #buffer(int)} should try to allocate a direct buffer rather than
|
||||
* a heap buffer
|
||||
*/
|
||||
protected AbstractByteBufAllocator(boolean preferDirect) {
|
||||
directByDefault = preferDirect && PlatformDependent.hasUnsafe();
|
||||
protected AbstractByteBufAllocator() {
|
||||
emptyBuf = new EmptyByteBuf(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf buffer() {
|
||||
if (directByDefault) {
|
||||
return directBuffer();
|
||||
}
|
||||
return heapBuffer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf buffer(int initialCapacity) {
|
||||
if (directByDefault) {
|
||||
return directBuffer(initialCapacity);
|
||||
}
|
||||
return heapBuffer(initialCapacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf buffer(int initialCapacity, int maxCapacity) {
|
||||
if (directByDefault) {
|
||||
return directBuffer(initialCapacity, maxCapacity);
|
||||
}
|
||||
return heapBuffer(initialCapacity, maxCapacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf ioBuffer() {
|
||||
if (PlatformDependent.hasUnsafe()) {
|
||||
return directBuffer(DEFAULT_INITIAL_CAPACITY);
|
||||
}
|
||||
return heapBuffer(DEFAULT_INITIAL_CAPACITY);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf ioBuffer(int initialCapacity) {
|
||||
if (PlatformDependent.hasUnsafe()) {
|
||||
return directBuffer(initialCapacity);
|
||||
}
|
||||
return heapBuffer(initialCapacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf ioBuffer(int initialCapacity, int maxCapacity) {
|
||||
if (PlatformDependent.hasUnsafe()) {
|
||||
return directBuffer(initialCapacity, maxCapacity);
|
||||
}
|
||||
return heapBuffer(initialCapacity, maxCapacity);
|
||||
}
|
||||
|
||||
|
@ -157,17 +129,11 @@ public abstract class AbstractByteBufAllocator implements ByteBufAllocator {
|
|||
|
||||
@Override
|
||||
public CompositeByteBuf compositeBuffer() {
|
||||
if (directByDefault) {
|
||||
return compositeDirectBuffer();
|
||||
}
|
||||
return compositeHeapBuffer();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompositeByteBuf compositeBuffer(int maxNumComponents) {
|
||||
if (directByDefault) {
|
||||
return compositeDirectBuffer(maxNumComponents);
|
||||
}
|
||||
return compositeHeapBuffer(maxNumComponents);
|
||||
}
|
||||
|
||||
|
@ -214,6 +180,6 @@ public abstract class AbstractByteBufAllocator implements ByteBufAllocator {
|
|||
|
||||
@Override
|
||||
public String toString() {
|
||||
return StringUtil.simpleClassName(this) + "(directByDefault: " + directByDefault + ')';
|
||||
return StringUtil.simpleClassName(this);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1795,19 +1795,15 @@ public abstract class ByteBuf implements ReferenceCounted, Comparable<ByteBuf> {
|
|||
*/
|
||||
public abstract int arrayOffset();
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if this buffer has a reference to the low-level memory address that points
|
||||
* to the backing data.
|
||||
*/
|
||||
public abstract boolean hasMemoryAddress();
|
||||
|
||||
/**
|
||||
* Returns the low-level memory address that point to the first byte of ths backing data.
|
||||
*
|
||||
* @throws UnsupportedOperationException
|
||||
* if this buffer does not support accessing the low-level memory address
|
||||
*/
|
||||
public abstract long memoryAddress();
|
||||
public final long memoryAddress() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes this buffer's readable bytes into a string with the specified
|
||||
|
|
|
@ -28,7 +28,6 @@ import java.util.Locale;
|
|||
import common.net.util.CharsetUtil;
|
||||
import common.net.util.Recycler;
|
||||
import common.net.util.Recycler.Handle;
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
import common.net.util.internal.SystemPropertyUtil;
|
||||
import common.net.util.internal.logging.InternalLogger;
|
||||
import common.net.util.internal.logging.InternalLoggerFactory;
|
||||
|
@ -405,46 +404,8 @@ public final class ByteBufUtil {
|
|||
return null;
|
||||
}
|
||||
|
||||
if (PlatformDependent.hasUnsafe()) {
|
||||
return ThreadLocalUnsafeDirectByteBuf.newInstance();
|
||||
} else {
|
||||
return ThreadLocalDirectByteBuf.newInstance();
|
||||
}
|
||||
}
|
||||
|
||||
static final class ThreadLocalUnsafeDirectByteBuf extends UnpooledUnsafeDirectByteBuf {
|
||||
|
||||
private static final Recycler<ThreadLocalUnsafeDirectByteBuf> RECYCLER =
|
||||
new Recycler<ThreadLocalUnsafeDirectByteBuf>() {
|
||||
@Override
|
||||
protected ThreadLocalUnsafeDirectByteBuf newObject(Handle handle) {
|
||||
return new ThreadLocalUnsafeDirectByteBuf(handle);
|
||||
}
|
||||
};
|
||||
|
||||
static ThreadLocalUnsafeDirectByteBuf newInstance() {
|
||||
ThreadLocalUnsafeDirectByteBuf buf = RECYCLER.get();
|
||||
buf.setRefCnt(1);
|
||||
return buf;
|
||||
}
|
||||
|
||||
private final Handle handle;
|
||||
|
||||
private ThreadLocalUnsafeDirectByteBuf(Handle handle) {
|
||||
super(UnpooledByteBufAllocator.DEFAULT, 256, Integer.MAX_VALUE);
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deallocate() {
|
||||
if (capacity() > THREAD_LOCAL_BUFFER_SIZE) {
|
||||
super.deallocate();
|
||||
} else {
|
||||
clear();
|
||||
RECYCLER.recycle(this, handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static final class ThreadLocalDirectByteBuf extends UnpooledDirectByteBuf {
|
||||
|
||||
|
|
|
@ -465,22 +465,6 @@ public class CompositeByteBuf extends AbstractReferenceCountedByteBuf {
|
|||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
if (components.size() == 1) {
|
||||
return components.get(0).buf.hasMemoryAddress();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
if (components.size() == 1) {
|
||||
return components.get(0).buf.memoryAddress();
|
||||
}
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int capacity() {
|
||||
if (components.isEmpty()) {
|
||||
|
|
|
@ -91,16 +91,6 @@ public class DuplicatedByteBuf extends AbstractDerivedByteBuf {
|
|||
return buffer.arrayOffset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return buffer.hasMemoryAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
return buffer.memoryAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getByte(int index) {
|
||||
return _getByte(index);
|
||||
|
|
|
@ -26,7 +26,6 @@ import java.nio.channels.ScatteringByteChannel;
|
|||
import java.nio.charset.Charset;
|
||||
|
||||
import common.net.util.internal.EmptyArrays;
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
import common.net.util.internal.StringUtil;
|
||||
|
||||
/**
|
||||
|
@ -35,19 +34,6 @@ import common.net.util.internal.StringUtil;
|
|||
public final class EmptyByteBuf extends ByteBuf {
|
||||
|
||||
private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.allocateDirect(0);
|
||||
private static final long EMPTY_BYTE_BUFFER_ADDRESS;
|
||||
|
||||
static {
|
||||
long emptyByteBufferAddress = 0;
|
||||
try {
|
||||
if (PlatformDependent.hasUnsafe()) {
|
||||
emptyByteBufferAddress = PlatformDependent.directBufferAddress(EMPTY_BYTE_BUFFER);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// Ignore
|
||||
}
|
||||
EMPTY_BYTE_BUFFER_ADDRESS = emptyByteBufferAddress;
|
||||
}
|
||||
|
||||
private final ByteBufAllocator alloc;
|
||||
private final ByteOrder order;
|
||||
|
@ -761,20 +747,6 @@ public final class EmptyByteBuf extends ByteBuf {
|
|||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return EMPTY_BYTE_BUFFER_ADDRESS != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
if (hasMemoryAddress()) {
|
||||
return EMPTY_BYTE_BUFFER_ADDRESS;
|
||||
} else {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Charset charset) {
|
||||
return "";
|
||||
|
|
|
@ -18,7 +18,6 @@ package common.net.buffer;
|
|||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
import common.net.util.internal.StringUtil;
|
||||
|
||||
abstract class PoolArena<T> {
|
||||
|
@ -417,9 +416,6 @@ abstract class PoolArena<T> {
|
|||
}
|
||||
|
||||
static final class DirectArena extends PoolArena<ByteBuffer> {
|
||||
|
||||
private static final boolean HAS_UNSAFE = PlatformDependent.hasUnsafe();
|
||||
|
||||
DirectArena(PooledByteBufAllocator parent, int pageSize, int maxOrder, int pageShifts, int chunkSize) {
|
||||
super(parent, pageSize, maxOrder, pageShifts, chunkSize);
|
||||
}
|
||||
|
@ -442,17 +438,12 @@ abstract class PoolArena<T> {
|
|||
|
||||
@Override
|
||||
protected void destroyChunk(PoolChunk<ByteBuffer> chunk) {
|
||||
PlatformDependent.freeDirectBuffer(chunk.memory);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected PooledByteBuf<ByteBuffer> newByteBuf(int maxCapacity) {
|
||||
if (HAS_UNSAFE) {
|
||||
return PooledUnsafeDirectByteBuf.newInstance(maxCapacity);
|
||||
} else {
|
||||
return PooledDirectByteBuf.newInstance(maxCapacity);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void memoryCopy(ByteBuffer src, int srcOffset, ByteBuffer dst, int dstOffset, int length) {
|
||||
|
@ -460,11 +451,6 @@ abstract class PoolArena<T> {
|
|||
return;
|
||||
}
|
||||
|
||||
if (HAS_UNSAFE) {
|
||||
PlatformDependent.copyMemory(
|
||||
PlatformDependent.directBufferAddress(src) + srcOffset,
|
||||
PlatformDependent.directBufferAddress(dst) + dstOffset, length);
|
||||
} else {
|
||||
// We must duplicate the NIO buffers because they may be accessed by other Netty buffers.
|
||||
src = src.duplicate();
|
||||
dst = dst.duplicate();
|
||||
|
@ -474,4 +460,3 @@ abstract class PoolArena<T> {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -116,8 +116,7 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
|
|||
}
|
||||
}
|
||||
|
||||
public static final PooledByteBufAllocator DEFAULT =
|
||||
new PooledByteBufAllocator(PlatformDependent.directBufferPreferred());
|
||||
public static final PooledByteBufAllocator DEFAULT = new PooledByteBufAllocator();
|
||||
|
||||
private final PoolArena<byte[]>[] heapArenas;
|
||||
private final PoolArena<ByteBuffer>[] directArenas;
|
||||
|
@ -128,25 +127,16 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
|
|||
final PoolThreadLocalCache threadCache;
|
||||
|
||||
public PooledByteBufAllocator() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public PooledByteBufAllocator(boolean preferDirect) {
|
||||
this(preferDirect, DEFAULT_NUM_HEAP_ARENA, DEFAULT_NUM_DIRECT_ARENA, DEFAULT_PAGE_SIZE, DEFAULT_MAX_ORDER);
|
||||
this(DEFAULT_NUM_HEAP_ARENA, DEFAULT_NUM_DIRECT_ARENA, DEFAULT_PAGE_SIZE, DEFAULT_MAX_ORDER);
|
||||
}
|
||||
|
||||
public PooledByteBufAllocator(int nHeapArena, int nDirectArena, int pageSize, int maxOrder) {
|
||||
this(false, nHeapArena, nDirectArena, pageSize, maxOrder);
|
||||
}
|
||||
|
||||
public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectArena, int pageSize, int maxOrder) {
|
||||
this(preferDirect, nHeapArena, nDirectArena, pageSize, maxOrder,
|
||||
this(nHeapArena, nDirectArena, pageSize, maxOrder,
|
||||
DEFAULT_TINY_CACHE_SIZE, DEFAULT_SMALL_CACHE_SIZE, DEFAULT_NORMAL_CACHE_SIZE);
|
||||
}
|
||||
|
||||
public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectArena, int pageSize, int maxOrder,
|
||||
public PooledByteBufAllocator(int nHeapArena, int nDirectArena, int pageSize, int maxOrder,
|
||||
int tinyCacheSize, int smallCacheSize, int normalCacheSize) {
|
||||
super(preferDirect);
|
||||
threadCache = new PoolThreadLocalCache();
|
||||
this.tinyCacheSize = tinyCacheSize;
|
||||
this.smallCacheSize = smallCacheSize;
|
||||
|
@ -181,15 +171,6 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
|
|||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
|
||||
public PooledByteBufAllocator(boolean preferDirect, int nHeapArena, int nDirectArena, int pageSize, int maxOrder,
|
||||
int tinyCacheSize, int smallCacheSize, int normalCacheSize,
|
||||
long cacheThreadAliveCheckInterval) {
|
||||
this(preferDirect, nHeapArena, nDirectArena, pageSize, maxOrder,
|
||||
tinyCacheSize, smallCacheSize, normalCacheSize);
|
||||
}
|
||||
|
||||
|
||||
private static <T> PoolArena<T>[] newArenaArray(int size) {
|
||||
return new PoolArena[size];
|
||||
|
@ -248,13 +229,9 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
|
|||
ByteBuf buf;
|
||||
if (directArena != null) {
|
||||
buf = directArena.allocate(cache, initialCapacity, maxCapacity);
|
||||
} else {
|
||||
if (PlatformDependent.hasUnsafe()) {
|
||||
buf = new UnpooledUnsafeDirectByteBuf(this, initialCapacity, maxCapacity);
|
||||
} else {
|
||||
buf = new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity);
|
||||
}
|
||||
}
|
||||
|
||||
return toLeakAwareBuffer(buf);
|
||||
}
|
||||
|
@ -264,23 +241,6 @@ public class PooledByteBufAllocator extends AbstractByteBufAllocator {
|
|||
return directArenas != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the calling {@link Thread} has a {@link ThreadLocal} cache for the allocated
|
||||
* buffers.
|
||||
*/
|
||||
@Deprecated
|
||||
public boolean hasThreadLocalCache() {
|
||||
return threadCache.isSet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Free all cached buffers for the calling {@link Thread}.
|
||||
*/
|
||||
@Deprecated
|
||||
public void freeThreadLocalCache() {
|
||||
threadCache.remove();
|
||||
}
|
||||
|
||||
final class PoolThreadLocalCache extends FastThreadLocal<PoolThreadCache> {
|
||||
private final AtomicInteger index = new AtomicInteger();
|
||||
|
||||
|
|
|
@ -360,16 +360,6 @@ final class PooledDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
|||
throw new UnsupportedOperationException("direct buffer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Recycler<?> recycler() {
|
||||
return RECYCLER;
|
||||
|
|
|
@ -23,7 +23,6 @@ import java.nio.channels.GatheringByteChannel;
|
|||
import java.nio.channels.ScatteringByteChannel;
|
||||
|
||||
import common.net.util.Recycler;
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
|
||||
final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
|
||||
|
||||
|
@ -94,9 +93,7 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
|
|||
@Override
|
||||
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
||||
checkDstIndex(index, length, dstIndex, dst.capacity());
|
||||
if (dst.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(memory, idx(index), dst.memoryAddress() + dstIndex, length);
|
||||
} else if (dst.hasArray()) {
|
||||
if (dst.hasArray()) {
|
||||
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
|
||||
} else {
|
||||
dst.setBytes(dstIndex, memory, idx(index), length);
|
||||
|
@ -195,9 +192,7 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
|
|||
@Override
|
||||
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
||||
checkSrcIndex(index, length, srcIndex, src.capacity());
|
||||
if (src.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, memory, idx(index), length);
|
||||
} else if (src.hasArray()) {
|
||||
if (src.hasArray()) {
|
||||
setBytes(index, src.array(), src.arrayOffset() + srcIndex, length);
|
||||
} else {
|
||||
src.getBytes(srcIndex, memory, idx(index), length);
|
||||
|
@ -285,16 +280,6 @@ final class PooledHeapByteBuf extends PooledByteBuf<byte[]> {
|
|||
return offset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ByteBuffer newInternalNioBuffer(byte[] memory) {
|
||||
return ByteBuffer.wrap(memory);
|
||||
|
|
|
@ -1,394 +0,0 @@
|
|||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package common.net.buffer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.nio.channels.GatheringByteChannel;
|
||||
import java.nio.channels.ScatteringByteChannel;
|
||||
|
||||
import common.net.util.Recycler;
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
|
||||
final class PooledUnsafeDirectByteBuf extends PooledByteBuf<ByteBuffer> {
|
||||
|
||||
private static final boolean NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
||||
|
||||
private static final Recycler<PooledUnsafeDirectByteBuf> RECYCLER = new Recycler<PooledUnsafeDirectByteBuf>() {
|
||||
@Override
|
||||
protected PooledUnsafeDirectByteBuf newObject(Handle handle) {
|
||||
return new PooledUnsafeDirectByteBuf(handle, 0);
|
||||
}
|
||||
};
|
||||
|
||||
static PooledUnsafeDirectByteBuf newInstance(int maxCapacity) {
|
||||
PooledUnsafeDirectByteBuf buf = RECYCLER.get();
|
||||
buf.setRefCnt(1);
|
||||
buf.maxCapacity(maxCapacity);
|
||||
return buf;
|
||||
}
|
||||
|
||||
private long memoryAddress;
|
||||
|
||||
private PooledUnsafeDirectByteBuf(Recycler.Handle recyclerHandle, int maxCapacity) {
|
||||
super(recyclerHandle, maxCapacity);
|
||||
}
|
||||
|
||||
@Override
|
||||
void init(PoolChunk<ByteBuffer> chunk, long handle, int offset, int length, int maxLength) {
|
||||
super.init(chunk, handle, offset, length, maxLength);
|
||||
initMemoryAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
void initUnpooled(PoolChunk<ByteBuffer> chunk, int length) {
|
||||
super.initUnpooled(chunk, length);
|
||||
initMemoryAddress();
|
||||
}
|
||||
|
||||
private void initMemoryAddress() {
|
||||
memoryAddress = PlatformDependent.directBufferAddress(memory) + offset;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ByteBuffer newInternalNioBuffer(ByteBuffer memory) {
|
||||
return memory.duplicate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirect() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte _getByte(int index) {
|
||||
return PlatformDependent.getByte(addr(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected short _getShort(int index) {
|
||||
short v = PlatformDependent.getShort(addr(index));
|
||||
return NATIVE_ORDER? v : Short.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int _getUnsignedMedium(int index) {
|
||||
long addr = addr(index);
|
||||
return (PlatformDependent.getByte(addr) & 0xff) << 16 |
|
||||
(PlatformDependent.getByte(addr + 1) & 0xff) << 8 |
|
||||
PlatformDependent.getByte(addr + 2) & 0xff;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int _getInt(int index) {
|
||||
int v = PlatformDependent.getInt(addr(index));
|
||||
return NATIVE_ORDER? v : Integer.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected long _getLong(int index) {
|
||||
long v = PlatformDependent.getLong(addr(index));
|
||||
return NATIVE_ORDER? v : Long.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
||||
checkIndex(index, length);
|
||||
if (dst == null) {
|
||||
throw new NullPointerException("dst");
|
||||
}
|
||||
if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
|
||||
throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
|
||||
}
|
||||
|
||||
if (length != 0) {
|
||||
if (dst.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length);
|
||||
} else if (dst.hasArray()) {
|
||||
PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
|
||||
} else {
|
||||
dst.setBytes(dstIndex, this, index, length);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
||||
checkIndex(index, length);
|
||||
if (dst == null) {
|
||||
throw new NullPointerException("dst");
|
||||
}
|
||||
if (dstIndex < 0 || dstIndex > dst.length - length) {
|
||||
throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
|
||||
}
|
||||
if (length != 0) {
|
||||
PlatformDependent.copyMemory(addr(index), dst, dstIndex, length);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
||||
getBytes(index, dst, false);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void getBytes(int index, ByteBuffer dst, boolean internal) {
|
||||
checkIndex(index);
|
||||
int bytesToCopy = Math.min(capacity() - index, dst.remaining());
|
||||
ByteBuffer tmpBuf;
|
||||
if (internal) {
|
||||
tmpBuf = internalNioBuffer();
|
||||
} else {
|
||||
tmpBuf = memory.duplicate();
|
||||
}
|
||||
index = idx(index);
|
||||
tmpBuf.clear().position(index).limit(index + bytesToCopy);
|
||||
dst.put(tmpBuf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf readBytes(ByteBuffer dst) {
|
||||
int length = dst.remaining();
|
||||
checkReadableBytes(length);
|
||||
getBytes(readerIndex, dst, true);
|
||||
readerIndex += length;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
|
||||
checkIndex(index, length);
|
||||
if (length != 0) {
|
||||
byte[] tmp = new byte[length];
|
||||
PlatformDependent.copyMemory(addr(index), tmp, 0, length);
|
||||
out.write(tmp);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
|
||||
return getBytes(index, out, length, false);
|
||||
}
|
||||
|
||||
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
|
||||
checkIndex(index, length);
|
||||
if (length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ByteBuffer tmpBuf;
|
||||
if (internal) {
|
||||
tmpBuf = internalNioBuffer();
|
||||
} else {
|
||||
tmpBuf = memory.duplicate();
|
||||
}
|
||||
index = idx(index);
|
||||
tmpBuf.clear().position(index).limit(index + length);
|
||||
return out.write(tmpBuf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readBytes(GatheringByteChannel out, int length)
|
||||
throws IOException {
|
||||
checkReadableBytes(length);
|
||||
int readBytes = getBytes(readerIndex, out, length, true);
|
||||
readerIndex += readBytes;
|
||||
return readBytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _setByte(int index, int value) {
|
||||
PlatformDependent.putByte(addr(index), (byte) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _setShort(int index, int value) {
|
||||
PlatformDependent.putShort(addr(index), NATIVE_ORDER ? (short) value : Short.reverseBytes((short) value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _setMedium(int index, int value) {
|
||||
long addr = addr(index);
|
||||
PlatformDependent.putByte(addr, (byte) (value >>> 16));
|
||||
PlatformDependent.putByte(addr + 1, (byte) (value >>> 8));
|
||||
PlatformDependent.putByte(addr + 2, (byte) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _setInt(int index, int value) {
|
||||
PlatformDependent.putInt(addr(index), NATIVE_ORDER ? value : Integer.reverseBytes(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _setLong(int index, long value) {
|
||||
PlatformDependent.putLong(addr(index), NATIVE_ORDER ? value : Long.reverseBytes(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
||||
checkIndex(index, length);
|
||||
if (src == null) {
|
||||
throw new NullPointerException("src");
|
||||
}
|
||||
if (srcIndex < 0 || srcIndex > src.capacity() - length) {
|
||||
throw new IndexOutOfBoundsException("srcIndex: " + srcIndex);
|
||||
}
|
||||
|
||||
if (length != 0) {
|
||||
if (src.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, addr(index), length);
|
||||
} else if (src.hasArray()) {
|
||||
PlatformDependent.copyMemory(src.array(), src.arrayOffset() + srcIndex, addr(index), length);
|
||||
} else {
|
||||
src.getBytes(srcIndex, this, index, length);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
|
||||
checkIndex(index, length);
|
||||
if (length != 0) {
|
||||
PlatformDependent.copyMemory(src, srcIndex, addr(index), length);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setBytes(int index, ByteBuffer src) {
|
||||
checkIndex(index, src.remaining());
|
||||
ByteBuffer tmpBuf = internalNioBuffer();
|
||||
if (src == tmpBuf) {
|
||||
src = src.duplicate();
|
||||
}
|
||||
|
||||
index = idx(index);
|
||||
tmpBuf.clear().position(index).limit(index + src.remaining());
|
||||
tmpBuf.put(src);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int setBytes(int index, InputStream in, int length) throws IOException {
|
||||
checkIndex(index, length);
|
||||
byte[] tmp = new byte[length];
|
||||
int readBytes = in.read(tmp);
|
||||
if (readBytes > 0) {
|
||||
PlatformDependent.copyMemory(tmp, 0, addr(index), readBytes);
|
||||
}
|
||||
return readBytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
|
||||
checkIndex(index, length);
|
||||
ByteBuffer tmpBuf = internalNioBuffer();
|
||||
index = idx(index);
|
||||
tmpBuf.clear().position(index).limit(index + length);
|
||||
try {
|
||||
return in.read(tmpBuf);
|
||||
} catch (ClosedChannelException ignored) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf copy(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
ByteBuf copy = alloc().directBuffer(length, maxCapacity());
|
||||
if (length != 0) {
|
||||
if (copy.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(addr(index), copy.memoryAddress(), length);
|
||||
copy.setIndex(0, length);
|
||||
} else {
|
||||
copy.writeBytes(this, index, length);
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nioBufferCount() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer[] nioBuffers(int index, int length) {
|
||||
return new ByteBuffer[] { nioBuffer(index, length) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer nioBuffer(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
index = idx(index);
|
||||
return ((ByteBuffer) memory.duplicate().position(index).limit(index + length)).slice();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer internalNioBuffer(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
index = idx(index);
|
||||
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasArray() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] array() {
|
||||
throw new UnsupportedOperationException("direct buffer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int arrayOffset() {
|
||||
throw new UnsupportedOperationException("direct buffer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
return memoryAddress;
|
||||
}
|
||||
|
||||
private long addr(int index) {
|
||||
return memoryAddress + index;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Recycler<?> recycler() {
|
||||
return RECYCLER;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SwappedByteBuf newSwappedByteBuf() {
|
||||
return new UnsafeDirectSwappedByteBuf(this);
|
||||
}
|
||||
}
|
|
@ -89,16 +89,6 @@ public class ReadOnlyByteBuf extends AbstractDerivedByteBuf {
|
|||
throw new ReadOnlyBufferException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
throw new ReadOnlyBufferException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf discardReadBytes() {
|
||||
throw new ReadOnlyBufferException();
|
||||
|
|
|
@ -322,14 +322,4 @@ class ReadOnlyByteBufferBuf extends AbstractReferenceCountedByteBuf {
|
|||
public int arrayOffset() {
|
||||
return buffer.arrayOffset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,138 +0,0 @@
|
|||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package common.net.buffer;
|
||||
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Read-only ByteBuf which wraps a read-only direct ByteBuffer and use unsafe for best performance.
|
||||
*/
|
||||
final class ReadOnlyUnsafeDirectByteBuf extends ReadOnlyByteBufferBuf {
|
||||
private static final boolean NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
||||
private final long memoryAddress;
|
||||
|
||||
ReadOnlyUnsafeDirectByteBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
|
||||
super(allocator, buffer);
|
||||
memoryAddress = PlatformDependent.directBufferAddress(buffer);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte _getByte(int index) {
|
||||
return PlatformDependent.getByte(addr(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected short _getShort(int index) {
|
||||
short v = PlatformDependent.getShort(addr(index));
|
||||
return NATIVE_ORDER? v : Short.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int _getUnsignedMedium(int index) {
|
||||
long addr = addr(index);
|
||||
return (PlatformDependent.getByte(addr) & 0xff) << 16 |
|
||||
(PlatformDependent.getByte(addr + 1) & 0xff) << 8 |
|
||||
PlatformDependent.getByte(addr + 2) & 0xff;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int _getInt(int index) {
|
||||
int v = PlatformDependent.getInt(addr(index));
|
||||
return NATIVE_ORDER? v : Integer.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected long _getLong(int index) {
|
||||
long v = PlatformDependent.getLong(addr(index));
|
||||
return NATIVE_ORDER? v : Long.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
||||
checkIndex(index, length);
|
||||
if (dst == null) {
|
||||
throw new NullPointerException("dst");
|
||||
}
|
||||
if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
|
||||
throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
|
||||
}
|
||||
|
||||
if (dst.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length);
|
||||
} else if (dst.hasArray()) {
|
||||
PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
|
||||
} else {
|
||||
dst.setBytes(dstIndex, this, index, length);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
||||
checkIndex(index, length);
|
||||
if (dst == null) {
|
||||
throw new NullPointerException("dst");
|
||||
}
|
||||
if (dstIndex < 0 || dstIndex > dst.length - length) {
|
||||
throw new IndexOutOfBoundsException(String.format(
|
||||
"dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length));
|
||||
}
|
||||
|
||||
if (length != 0) {
|
||||
PlatformDependent.copyMemory(addr(index), dst, dstIndex, length);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
||||
checkIndex(index);
|
||||
if (dst == null) {
|
||||
throw new NullPointerException("dst");
|
||||
}
|
||||
|
||||
int bytesToCopy = Math.min(capacity() - index, dst.remaining());
|
||||
ByteBuffer tmpBuf = internalNioBuffer();
|
||||
tmpBuf.clear().position(index).limit(index + bytesToCopy);
|
||||
dst.put(tmpBuf);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf copy(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
ByteBuf copy = alloc().directBuffer(length, maxCapacity());
|
||||
if (length != 0) {
|
||||
if (copy.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(addr(index), copy.memoryAddress(), length);
|
||||
copy.setIndex(0, length);
|
||||
} else {
|
||||
copy.writeBytes(this, index, length);
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
private long addr(int index) {
|
||||
return memoryAddress + index;
|
||||
}
|
||||
}
|
|
@ -102,16 +102,6 @@ public class SlicedByteBuf extends AbstractDerivedByteBuf {
|
|||
return buffer.arrayOffset() + adjustment;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return buffer.hasMemoryAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
return buffer.memoryAddress() + adjustment;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte _getByte(int index) {
|
||||
return buffer.getByte(index + adjustment);
|
||||
|
|
|
@ -777,16 +777,6 @@ public class SwappedByteBuf extends ByteBuf {
|
|||
return buf.arrayOffset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return buf.hasMemoryAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
return buf.memoryAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Charset charset) {
|
||||
return buf.toString(charset);
|
||||
|
|
|
@ -22,8 +22,6 @@ import java.nio.charset.Charset;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link ByteBuf} by allocating new space or by wrapping
|
||||
|
@ -190,17 +188,8 @@ public final class Unpooled {
|
|||
buffer.array(),
|
||||
buffer.arrayOffset() + buffer.position(),
|
||||
buffer.remaining()).order(buffer.order());
|
||||
} else if (PlatformDependent.hasUnsafe()) {
|
||||
if (buffer.isReadOnly()) {
|
||||
if (buffer.isDirect()) {
|
||||
return new ReadOnlyUnsafeDirectByteBuf(ALLOC, buffer);
|
||||
} else {
|
||||
return new ReadOnlyByteBufferBuf(ALLOC, buffer);
|
||||
}
|
||||
} else {
|
||||
return new UnpooledUnsafeDirectByteBuf(ALLOC, buffer, buffer.remaining());
|
||||
}
|
||||
} else {
|
||||
else {
|
||||
if (buffer.isReadOnly()) {
|
||||
return new ReadOnlyByteBufferBuf(ALLOC, buffer);
|
||||
} else {
|
||||
|
|
|
@ -15,8 +15,6 @@
|
|||
*/
|
||||
package common.net.buffer;
|
||||
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
|
||||
/**
|
||||
* Simplistic {@link ByteBufAllocator} implementation that does not pool anything.
|
||||
*/
|
||||
|
@ -25,18 +23,7 @@ public final class UnpooledByteBufAllocator extends AbstractByteBufAllocator {
|
|||
/**
|
||||
* Default instance
|
||||
*/
|
||||
public static final UnpooledByteBufAllocator DEFAULT =
|
||||
new UnpooledByteBufAllocator(PlatformDependent.directBufferPreferred());
|
||||
|
||||
/**
|
||||
* Create a new instance
|
||||
*
|
||||
* @param preferDirect {@code true} if {@link #buffer(int)} should try to allocate a direct buffer rather than
|
||||
* a heap buffer
|
||||
*/
|
||||
public UnpooledByteBufAllocator(boolean preferDirect) {
|
||||
super(preferDirect);
|
||||
}
|
||||
public static final UnpooledByteBufAllocator DEFAULT = new UnpooledByteBufAllocator();
|
||||
|
||||
@Override
|
||||
protected ByteBuf newHeapBuffer(int initialCapacity, int maxCapacity) {
|
||||
|
@ -46,11 +33,7 @@ public final class UnpooledByteBufAllocator extends AbstractByteBufAllocator {
|
|||
@Override
|
||||
protected ByteBuf newDirectBuffer(int initialCapacity, int maxCapacity) {
|
||||
ByteBuf buf;
|
||||
if (PlatformDependent.hasUnsafe()) {
|
||||
buf = new UnpooledUnsafeDirectByteBuf(this, initialCapacity, maxCapacity);
|
||||
} else {
|
||||
buf = new UnpooledDirectByteBuf(this, initialCapacity, maxCapacity);
|
||||
}
|
||||
|
||||
return toLeakAwareBuffer(buf);
|
||||
}
|
||||
|
|
|
@ -24,8 +24,6 @@ import java.nio.channels.ClosedChannelException;
|
|||
import java.nio.channels.GatheringByteChannel;
|
||||
import java.nio.channels.ScatteringByteChannel;
|
||||
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
|
||||
/**
|
||||
* A NIO {@link ByteBuffer} based buffer. It is recommended to use {@link Unpooled#directBuffer(int)}
|
||||
* and {@link Unpooled#wrappedBuffer(ByteBuffer)} instead of calling the
|
||||
|
@ -109,7 +107,6 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
|||
* Free a direct {@link ByteBuffer}
|
||||
*/
|
||||
protected void freeDirect(ByteBuffer buffer) {
|
||||
PlatformDependent.freeDirectBuffer(buffer);
|
||||
}
|
||||
|
||||
private void setByteBuffer(ByteBuffer buffer) {
|
||||
|
@ -200,16 +197,6 @@ public class UnpooledDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
|||
throw new UnsupportedOperationException("direct buffer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte getByte(int index) {
|
||||
ensureAccessible();
|
||||
|
|
|
@ -24,8 +24,6 @@ import java.nio.channels.ClosedChannelException;
|
|||
import java.nio.channels.GatheringByteChannel;
|
||||
import java.nio.channels.ScatteringByteChannel;
|
||||
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
|
||||
/**
|
||||
* Big endian Java heap buffer implementation.
|
||||
*/
|
||||
|
@ -147,22 +145,10 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
|
|||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
||||
checkDstIndex(index, length, dstIndex, dst.capacity());
|
||||
if (dst.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(array, index, dst.memoryAddress() + dstIndex, length);
|
||||
} else if (dst.hasArray()) {
|
||||
if (dst.hasArray()) {
|
||||
getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
|
||||
} else {
|
||||
dst.setBytes(dstIndex, array, index, length);
|
||||
|
@ -219,9 +205,7 @@ public class UnpooledHeapByteBuf extends AbstractReferenceCountedByteBuf {
|
|||
@Override
|
||||
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
||||
checkSrcIndex(index, length, srcIndex, src.capacity());
|
||||
if (src.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, array, index, length);
|
||||
} else if (src.hasArray()) {
|
||||
if (src.hasArray()) {
|
||||
setBytes(index, src.array(), src.arrayOffset() + srcIndex, length);
|
||||
} else {
|
||||
src.getBytes(srcIndex, array, index, length);
|
||||
|
|
|
@ -1,524 +0,0 @@
|
|||
/*
|
||||
* Copyright 2012 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package common.net.buffer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.nio.channels.ClosedChannelException;
|
||||
import java.nio.channels.GatheringByteChannel;
|
||||
import java.nio.channels.ScatteringByteChannel;
|
||||
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
|
||||
/**
|
||||
* A NIO {@link ByteBuffer} based buffer. It is recommended to use {@link Unpooled#directBuffer(int)}
|
||||
* and {@link Unpooled#wrappedBuffer(ByteBuffer)} instead of calling the
|
||||
* constructor explicitly.
|
||||
*/
|
||||
public class UnpooledUnsafeDirectByteBuf extends AbstractReferenceCountedByteBuf {
|
||||
|
||||
private static final boolean NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
||||
|
||||
private final ByteBufAllocator alloc;
|
||||
|
||||
private long memoryAddress;
|
||||
private ByteBuffer buffer;
|
||||
private ByteBuffer tmpNioBuf;
|
||||
private int capacity;
|
||||
private boolean doNotFree;
|
||||
|
||||
/**
|
||||
* Creates a new direct buffer.
|
||||
*
|
||||
* @param initialCapacity the initial capacity of the underlying direct buffer
|
||||
* @param maxCapacity the maximum capacity of the underlying direct buffer
|
||||
*/
|
||||
protected UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, int initialCapacity, int maxCapacity) {
|
||||
super(maxCapacity);
|
||||
if (alloc == null) {
|
||||
throw new NullPointerException("alloc");
|
||||
}
|
||||
if (initialCapacity < 0) {
|
||||
throw new IllegalArgumentException("initialCapacity: " + initialCapacity);
|
||||
}
|
||||
if (maxCapacity < 0) {
|
||||
throw new IllegalArgumentException("maxCapacity: " + maxCapacity);
|
||||
}
|
||||
if (initialCapacity > maxCapacity) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));
|
||||
}
|
||||
|
||||
this.alloc = alloc;
|
||||
setByteBuffer(allocateDirect(initialCapacity));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new direct buffer by wrapping the specified initial buffer.
|
||||
*
|
||||
* @param maxCapacity the maximum capacity of the underlying direct buffer
|
||||
*/
|
||||
protected UnpooledUnsafeDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity) {
|
||||
super(maxCapacity);
|
||||
if (alloc == null) {
|
||||
throw new NullPointerException("alloc");
|
||||
}
|
||||
if (initialBuffer == null) {
|
||||
throw new NullPointerException("initialBuffer");
|
||||
}
|
||||
if (!initialBuffer.isDirect()) {
|
||||
throw new IllegalArgumentException("initialBuffer is not a direct buffer.");
|
||||
}
|
||||
if (initialBuffer.isReadOnly()) {
|
||||
throw new IllegalArgumentException("initialBuffer is a read-only buffer.");
|
||||
}
|
||||
|
||||
int initialCapacity = initialBuffer.remaining();
|
||||
if (initialCapacity > maxCapacity) {
|
||||
throw new IllegalArgumentException(String.format(
|
||||
"initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));
|
||||
}
|
||||
|
||||
this.alloc = alloc;
|
||||
doNotFree = true;
|
||||
setByteBuffer(initialBuffer.slice().order(ByteOrder.BIG_ENDIAN));
|
||||
writerIndex(initialCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate a new direct {@link ByteBuffer} with the given initialCapacity.
|
||||
*/
|
||||
protected ByteBuffer allocateDirect(int initialCapacity) {
|
||||
return ByteBuffer.allocateDirect(initialCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free a direct {@link ByteBuffer}
|
||||
*/
|
||||
protected void freeDirect(ByteBuffer buffer) {
|
||||
PlatformDependent.freeDirectBuffer(buffer);
|
||||
}
|
||||
|
||||
private void setByteBuffer(ByteBuffer buffer) {
|
||||
ByteBuffer oldBuffer = this.buffer;
|
||||
if (oldBuffer != null) {
|
||||
if (doNotFree) {
|
||||
doNotFree = false;
|
||||
} else {
|
||||
freeDirect(oldBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
this.buffer = buffer;
|
||||
memoryAddress = PlatformDependent.directBufferAddress(buffer);
|
||||
tmpNioBuf = null;
|
||||
capacity = buffer.remaining();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDirect() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int capacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf capacity(int newCapacity) {
|
||||
ensureAccessible();
|
||||
if (newCapacity < 0 || newCapacity > maxCapacity()) {
|
||||
throw new IllegalArgumentException("newCapacity: " + newCapacity);
|
||||
}
|
||||
|
||||
int readerIndex = readerIndex();
|
||||
int writerIndex = writerIndex();
|
||||
|
||||
int oldCapacity = capacity;
|
||||
if (newCapacity > oldCapacity) {
|
||||
ByteBuffer oldBuffer = buffer;
|
||||
ByteBuffer newBuffer = allocateDirect(newCapacity);
|
||||
oldBuffer.position(0).limit(oldBuffer.capacity());
|
||||
newBuffer.position(0).limit(oldBuffer.capacity());
|
||||
newBuffer.put(oldBuffer);
|
||||
newBuffer.clear();
|
||||
setByteBuffer(newBuffer);
|
||||
} else if (newCapacity < oldCapacity) {
|
||||
ByteBuffer oldBuffer = buffer;
|
||||
ByteBuffer newBuffer = allocateDirect(newCapacity);
|
||||
if (readerIndex < newCapacity) {
|
||||
if (writerIndex > newCapacity) {
|
||||
writerIndex(writerIndex = newCapacity);
|
||||
}
|
||||
oldBuffer.position(readerIndex).limit(writerIndex);
|
||||
newBuffer.position(readerIndex).limit(writerIndex);
|
||||
newBuffer.put(oldBuffer);
|
||||
newBuffer.clear();
|
||||
} else {
|
||||
setIndex(newCapacity, newCapacity);
|
||||
}
|
||||
setByteBuffer(newBuffer);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBufAllocator alloc() {
|
||||
return alloc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteOrder order() {
|
||||
return ByteOrder.BIG_ENDIAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasArray() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] array() {
|
||||
throw new UnsupportedOperationException("direct buffer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int arrayOffset() {
|
||||
throw new UnsupportedOperationException("direct buffer");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
return memoryAddress;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected byte _getByte(int index) {
|
||||
return PlatformDependent.getByte(addr(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected short _getShort(int index) {
|
||||
short v = PlatformDependent.getShort(addr(index));
|
||||
return NATIVE_ORDER? v : Short.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int _getUnsignedMedium(int index) {
|
||||
long addr = addr(index);
|
||||
return (PlatformDependent.getByte(addr) & 0xff) << 16 |
|
||||
(PlatformDependent.getByte(addr + 1) & 0xff) << 8 |
|
||||
PlatformDependent.getByte(addr + 2) & 0xff;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected int _getInt(int index) {
|
||||
int v = PlatformDependent.getInt(addr(index));
|
||||
return NATIVE_ORDER? v : Integer.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected long _getLong(int index) {
|
||||
long v = PlatformDependent.getLong(addr(index));
|
||||
return NATIVE_ORDER? v : Long.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
|
||||
checkIndex(index, length);
|
||||
if (dst == null) {
|
||||
throw new NullPointerException("dst");
|
||||
}
|
||||
if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
|
||||
throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
|
||||
}
|
||||
|
||||
if (dst.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length);
|
||||
} else if (dst.hasArray()) {
|
||||
PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
|
||||
} else {
|
||||
dst.setBytes(dstIndex, this, index, length);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, byte[] dst, int dstIndex, int length) {
|
||||
checkIndex(index, length);
|
||||
if (dst == null) {
|
||||
throw new NullPointerException("dst");
|
||||
}
|
||||
if (dstIndex < 0 || dstIndex > dst.length - length) {
|
||||
throw new IndexOutOfBoundsException(String.format(
|
||||
"dstIndex: %d, length: %d (expected: range(0, %d))", dstIndex, length, dst.length));
|
||||
}
|
||||
|
||||
if (length != 0) {
|
||||
PlatformDependent.copyMemory(addr(index), dst, dstIndex, length);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, ByteBuffer dst) {
|
||||
getBytes(index, dst, false);
|
||||
return this;
|
||||
}
|
||||
|
||||
private void getBytes(int index, ByteBuffer dst, boolean internal) {
|
||||
checkIndex(index);
|
||||
if (dst == null) {
|
||||
throw new NullPointerException("dst");
|
||||
}
|
||||
|
||||
int bytesToCopy = Math.min(capacity() - index, dst.remaining());
|
||||
ByteBuffer tmpBuf;
|
||||
if (internal) {
|
||||
tmpBuf = internalNioBuffer();
|
||||
} else {
|
||||
tmpBuf = buffer.duplicate();
|
||||
}
|
||||
tmpBuf.clear().position(index).limit(index + bytesToCopy);
|
||||
dst.put(tmpBuf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf readBytes(ByteBuffer dst) {
|
||||
int length = dst.remaining();
|
||||
checkReadableBytes(length);
|
||||
getBytes(readerIndex, dst, true);
|
||||
readerIndex += length;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _setByte(int index, int value) {
|
||||
PlatformDependent.putByte(addr(index), (byte) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _setShort(int index, int value) {
|
||||
PlatformDependent.putShort(addr(index), NATIVE_ORDER ? (short) value : Short.reverseBytes((short) value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _setMedium(int index, int value) {
|
||||
long addr = addr(index);
|
||||
PlatformDependent.putByte(addr, (byte) (value >>> 16));
|
||||
PlatformDependent.putByte(addr + 1, (byte) (value >>> 8));
|
||||
PlatformDependent.putByte(addr + 2, (byte) value);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _setInt(int index, int value) {
|
||||
PlatformDependent.putInt(addr(index), NATIVE_ORDER ? value : Integer.reverseBytes(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void _setLong(int index, long value) {
|
||||
PlatformDependent.putLong(addr(index), NATIVE_ORDER ? value : Long.reverseBytes(value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
|
||||
checkIndex(index, length);
|
||||
if (src == null) {
|
||||
throw new NullPointerException("src");
|
||||
}
|
||||
if (srcIndex < 0 || srcIndex > src.capacity() - length) {
|
||||
throw new IndexOutOfBoundsException("srcIndex: " + srcIndex);
|
||||
}
|
||||
|
||||
if (length != 0) {
|
||||
if (src.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, addr(index), length);
|
||||
} else if (src.hasArray()) {
|
||||
PlatformDependent.copyMemory(src.array(), src.arrayOffset() + srcIndex, addr(index), length);
|
||||
} else {
|
||||
src.getBytes(srcIndex, this, index, length);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
|
||||
checkIndex(index, length);
|
||||
if (length != 0) {
|
||||
PlatformDependent.copyMemory(src, srcIndex, addr(index), length);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setBytes(int index, ByteBuffer src) {
|
||||
ensureAccessible();
|
||||
ByteBuffer tmpBuf = internalNioBuffer();
|
||||
if (src == tmpBuf) {
|
||||
src = src.duplicate();
|
||||
}
|
||||
|
||||
tmpBuf.clear().position(index).limit(index + src.remaining());
|
||||
tmpBuf.put(src);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf getBytes(int index, OutputStream out, int length) throws IOException {
|
||||
ensureAccessible();
|
||||
if (length != 0) {
|
||||
byte[] tmp = new byte[length];
|
||||
PlatformDependent.copyMemory(addr(index), tmp, 0, length);
|
||||
out.write(tmp);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getBytes(int index, GatheringByteChannel out, int length) throws IOException {
|
||||
return getBytes(index, out, length, false);
|
||||
}
|
||||
|
||||
private int getBytes(int index, GatheringByteChannel out, int length, boolean internal) throws IOException {
|
||||
ensureAccessible();
|
||||
if (length == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ByteBuffer tmpBuf;
|
||||
if (internal) {
|
||||
tmpBuf = internalNioBuffer();
|
||||
} else {
|
||||
tmpBuf = buffer.duplicate();
|
||||
}
|
||||
tmpBuf.clear().position(index).limit(index + length);
|
||||
return out.write(tmpBuf);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int readBytes(GatheringByteChannel out, int length) throws IOException {
|
||||
checkReadableBytes(length);
|
||||
int readBytes = getBytes(readerIndex, out, length, true);
|
||||
readerIndex += readBytes;
|
||||
return readBytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int setBytes(int index, InputStream in, int length) throws IOException {
|
||||
checkIndex(index, length);
|
||||
byte[] tmp = new byte[length];
|
||||
int readBytes = in.read(tmp);
|
||||
if (readBytes > 0) {
|
||||
PlatformDependent.copyMemory(tmp, 0, addr(index), readBytes);
|
||||
}
|
||||
return readBytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
|
||||
ensureAccessible();
|
||||
ByteBuffer tmpBuf = internalNioBuffer();
|
||||
tmpBuf.clear().position(index).limit(index + length);
|
||||
try {
|
||||
return in.read(tmpBuf);
|
||||
} catch (ClosedChannelException ignored) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nioBufferCount() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer[] nioBuffers(int index, int length) {
|
||||
return new ByteBuffer[] { nioBuffer(index, length) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf copy(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
ByteBuf copy = alloc().directBuffer(length, maxCapacity());
|
||||
if (length != 0) {
|
||||
if (copy.hasMemoryAddress()) {
|
||||
PlatformDependent.copyMemory(addr(index), copy.memoryAddress(), length);
|
||||
copy.setIndex(0, length);
|
||||
} else {
|
||||
copy.writeBytes(this, index, length);
|
||||
}
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer internalNioBuffer(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
|
||||
}
|
||||
|
||||
private ByteBuffer internalNioBuffer() {
|
||||
ByteBuffer tmpNioBuf = this.tmpNioBuf;
|
||||
if (tmpNioBuf == null) {
|
||||
this.tmpNioBuf = tmpNioBuf = buffer.duplicate();
|
||||
}
|
||||
return tmpNioBuf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuffer nioBuffer(int index, int length) {
|
||||
checkIndex(index, length);
|
||||
return ((ByteBuffer) buffer.duplicate().position(index).limit(index + length)).slice();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void deallocate() {
|
||||
ByteBuffer buffer = this.buffer;
|
||||
if (buffer == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.buffer = null;
|
||||
|
||||
if (!doNotFree) {
|
||||
freeDirect(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf unwrap() {
|
||||
return null;
|
||||
}
|
||||
|
||||
long addr(int index) {
|
||||
return memoryAddress + index;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SwappedByteBuf newSwappedByteBuf() {
|
||||
return new UnsafeDirectSwappedByteBuf(this);
|
||||
}
|
||||
}
|
|
@ -1,186 +0,0 @@
|
|||
/*
|
||||
* Copyright 2014 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package common.net.buffer;
|
||||
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
|
||||
/**
|
||||
* Special {@link SwappedByteBuf} for {@link ByteBuf}s that are backed by a {@code memoryAddress}.
|
||||
*/
|
||||
final class UnsafeDirectSwappedByteBuf extends SwappedByteBuf {
|
||||
private static final boolean NATIVE_ORDER = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
||||
private final boolean nativeByteOrder;
|
||||
private final AbstractByteBuf wrapped;
|
||||
|
||||
UnsafeDirectSwappedByteBuf(AbstractByteBuf buf) {
|
||||
super(buf);
|
||||
wrapped = buf;
|
||||
nativeByteOrder = NATIVE_ORDER == (order() == ByteOrder.BIG_ENDIAN);
|
||||
}
|
||||
|
||||
private long addr(int index) {
|
||||
// We need to call wrapped.memoryAddress() everytime and NOT cache it as it may change if the buffer expand.
|
||||
// See:
|
||||
// - https://github.com/netty/netty/issues/2587
|
||||
// - https://github.com/netty/netty/issues/2580
|
||||
return wrapped.memoryAddress() + index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong(int index) {
|
||||
wrapped.checkIndex(index, 8);
|
||||
long v = PlatformDependent.getLong(addr(index));
|
||||
return nativeByteOrder? v : Long.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getFloat(int index) {
|
||||
return Float.intBitsToFloat(getInt(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(int index) {
|
||||
return Double.longBitsToDouble(getLong(index));
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getChar(int index) {
|
||||
return (char) getShort(index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getUnsignedInt(int index) {
|
||||
return getInt(index) & 0xFFFFFFFFL;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(int index) {
|
||||
wrapped.checkIndex(index, 4);
|
||||
int v = PlatformDependent.getInt(addr(index));
|
||||
return nativeByteOrder? v : Integer.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getUnsignedShort(int index) {
|
||||
return getShort(index) & 0xFFFF;
|
||||
}
|
||||
|
||||
@Override
|
||||
public short getShort(int index) {
|
||||
wrapped.checkIndex(index, 2);
|
||||
short v = PlatformDependent.getShort(addr(index));
|
||||
return nativeByteOrder? v : Short.reverseBytes(v);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setShort(int index, int value) {
|
||||
wrapped.checkIndex(index, 2);
|
||||
_setShort(index, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setInt(int index, int value) {
|
||||
wrapped.checkIndex(index, 4);
|
||||
_setInt(index, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setLong(int index, long value) {
|
||||
wrapped.checkIndex(index, 8);
|
||||
_setLong(index, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setChar(int index, int value) {
|
||||
setShort(index, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setFloat(int index, float value) {
|
||||
setInt(index, Float.floatToRawIntBits(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf setDouble(int index, double value) {
|
||||
setLong(index, Double.doubleToRawLongBits(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf writeShort(int value) {
|
||||
wrapped.ensureAccessible();
|
||||
wrapped.ensureWritable(2);
|
||||
_setShort(wrapped.writerIndex, value);
|
||||
wrapped.writerIndex += 2;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf writeInt(int value) {
|
||||
wrapped.ensureAccessible();
|
||||
wrapped.ensureWritable(4);
|
||||
_setInt(wrapped.writerIndex, value);
|
||||
wrapped.writerIndex += 4;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf writeLong(long value) {
|
||||
wrapped.ensureAccessible();
|
||||
wrapped.ensureWritable(8);
|
||||
_setLong(wrapped.writerIndex, value);
|
||||
wrapped.writerIndex += 8;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf writeChar(int value) {
|
||||
writeShort(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf writeFloat(float value) {
|
||||
writeInt(Float.floatToRawIntBits(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteBuf writeDouble(double value) {
|
||||
writeLong(Double.doubleToRawLongBits(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
private void _setShort(int index, int value) {
|
||||
PlatformDependent.putShort(addr(index), nativeByteOrder ? (short) value : Short.reverseBytes((short) value));
|
||||
}
|
||||
|
||||
private void _setInt(int index, int value) {
|
||||
PlatformDependent.putInt(addr(index), nativeByteOrder ? value : Integer.reverseBytes(value));
|
||||
}
|
||||
|
||||
private void _setLong(int index, long value) {
|
||||
PlatformDependent.putLong(addr(index), nativeByteOrder ? value : Long.reverseBytes(value));
|
||||
}
|
||||
}
|
|
@ -38,16 +38,6 @@ class WrappedByteBuf extends ByteBuf {
|
|||
this.buf = buf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMemoryAddress() {
|
||||
return buf.hasMemoryAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long memoryAddress() {
|
||||
return buf.memoryAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int capacity() {
|
||||
return buf.capacity();
|
||||
|
|
|
@ -31,6 +31,7 @@ import common.net.util.internal.PlatformDependent;
|
|||
import common.net.util.internal.ThreadLocalRandom;
|
||||
import common.net.util.internal.logging.InternalLogger;
|
||||
import common.net.util.internal.logging.InternalLoggerFactory;
|
||||
import common.util.Util;
|
||||
|
||||
/**
|
||||
* A skeletal {@link Channel} implementation.
|
||||
|
@ -458,7 +459,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
|
|||
}
|
||||
|
||||
// See: https://github.com/netty/netty/issues/576
|
||||
if (!PlatformDependent.isWindows() && !PlatformDependent.isRoot() &&
|
||||
if (!Util.WINDOWS && !PlatformDependent.isRoot() &&
|
||||
Boolean.TRUE.equals(config().getOption(ChannelOption.SO_BROADCAST)) &&
|
||||
localAddress instanceof InetSocketAddress &&
|
||||
!((InetSocketAddress) localAddress).getAddress().isAnyLocalAddress()) {
|
||||
|
@ -707,7 +708,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
|
|||
doWrite(outboundBuffer);
|
||||
} catch (Throwable t) {
|
||||
outboundBuffer.failFlushed(t);
|
||||
if (t instanceof IOException && config().isAutoClose()) {
|
||||
if (t instanceof IOException) {
|
||||
close(voidPromise());
|
||||
}
|
||||
} finally {
|
||||
|
|
|
@ -185,24 +185,6 @@ public interface ChannelConfig {
|
|||
*/
|
||||
ChannelConfig setAutoRead(boolean autoRead);
|
||||
|
||||
/**
|
||||
* @deprecated From version 5.0, {@link Channel} will not be closed on write failure.
|
||||
*
|
||||
* Returns {@code true} if and only if the {@link Channel} will be closed automatically and immediately on
|
||||
* write failure. The default is {@code false}.
|
||||
*/
|
||||
@Deprecated
|
||||
boolean isAutoClose();
|
||||
|
||||
/**
|
||||
* @deprecated From version 5.0, {@link Channel} will not be closed on write failure.
|
||||
*
|
||||
* Sets whether the {@link Channel} should be closed automatically and immediately on write faillure.
|
||||
* The default is {@code false}.
|
||||
*/
|
||||
@Deprecated
|
||||
ChannelConfig setAutoClose(boolean autoClose);
|
||||
|
||||
/**
|
||||
* Returns the high water mark of the write buffer. If the number of bytes
|
||||
* queued in the write buffer exceeds this value, {@link Channel#isWritable()}
|
||||
|
|
|
@ -49,15 +49,6 @@ public class ChannelOption<T> extends UniqueName {
|
|||
public static final ChannelOption<Boolean> ALLOW_HALF_CLOSURE = valueOf("ALLOW_HALF_CLOSURE");
|
||||
public static final ChannelOption<Boolean> AUTO_READ = valueOf("AUTO_READ");
|
||||
|
||||
/**
|
||||
* @deprecated From version 5.0, {@link Channel} will not be closed on write failure.
|
||||
*
|
||||
* {@code true} if and only if the {@link Channel} is closed automatically and immediately on write failure.
|
||||
* The default is {@code false}.
|
||||
*/
|
||||
@Deprecated
|
||||
public static final ChannelOption<Boolean> AUTO_CLOSE = valueOf("AUTO_CLOSE");
|
||||
|
||||
public static final ChannelOption<Boolean> SO_BROADCAST = valueOf("SO_BROADCAST");
|
||||
public static final ChannelOption<Boolean> SO_KEEPALIVE = valueOf("SO_KEEPALIVE");
|
||||
public static final ChannelOption<Integer> SO_SNDBUF = valueOf("SO_SNDBUF");
|
||||
|
@ -75,15 +66,6 @@ public class ChannelOption<T> extends UniqueName {
|
|||
|
||||
public static final ChannelOption<Boolean> TCP_NODELAY = valueOf("TCP_NODELAY");
|
||||
|
||||
@Deprecated
|
||||
public static final ChannelOption<Long> AIO_READ_TIMEOUT = valueOf("AIO_READ_TIMEOUT");
|
||||
@Deprecated
|
||||
public static final ChannelOption<Long> AIO_WRITE_TIMEOUT = valueOf("AIO_WRITE_TIMEOUT");
|
||||
|
||||
@Deprecated
|
||||
public static final ChannelOption<Boolean> DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION =
|
||||
valueOf("DATAGRAM_CHANNEL_ACTIVE_ON_REGISTRATION");
|
||||
|
||||
/**
|
||||
* Creates a new {@link ChannelOption} with the specified {@code name}.
|
||||
*/
|
||||
|
@ -91,11 +73,7 @@ public class ChannelOption<T> extends UniqueName {
|
|||
return new ChannelOption<T>(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #valueOf(String)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
protected ChannelOption(String name) {
|
||||
private ChannelOption(String name) {
|
||||
super(names, name);
|
||||
}
|
||||
|
||||
|
|
|
@ -529,11 +529,6 @@ public final class ChannelOutboundBuffer {
|
|||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public void recycle() {
|
||||
// NOOP
|
||||
}
|
||||
|
||||
public long totalPendingWriteBytes() {
|
||||
return totalPendingSize;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
package common.net.channel;
|
||||
|
||||
import static common.net.channel.ChannelOption.ALLOCATOR;
|
||||
import static common.net.channel.ChannelOption.AUTO_CLOSE;
|
||||
import static common.net.channel.ChannelOption.AUTO_READ;
|
||||
import static common.net.channel.ChannelOption.CONNECT_TIMEOUT_MILLIS;
|
||||
import static common.net.channel.ChannelOption.MAX_MESSAGES_PER_READ;
|
||||
|
@ -53,7 +52,6 @@ public class DefaultChannelConfig implements ChannelConfig {
|
|||
private volatile int maxMessagesPerRead;
|
||||
private volatile int writeSpinCount = 16;
|
||||
private volatile boolean autoRead = true;
|
||||
private volatile boolean autoClose = true;
|
||||
private volatile int writeBufferHighWaterMark = 64 * 1024;
|
||||
private volatile int writeBufferLowWaterMark = 32 * 1024;
|
||||
|
||||
|
@ -80,7 +78,7 @@ public class DefaultChannelConfig implements ChannelConfig {
|
|||
return getOptions(
|
||||
null,
|
||||
CONNECT_TIMEOUT_MILLIS, MAX_MESSAGES_PER_READ, WRITE_SPIN_COUNT,
|
||||
ALLOCATOR, AUTO_READ, AUTO_CLOSE, RCVBUF_ALLOCATOR, WRITE_BUFFER_HIGH_WATER_MARK,
|
||||
ALLOCATOR, AUTO_READ, RCVBUF_ALLOCATOR, WRITE_BUFFER_HIGH_WATER_MARK,
|
||||
WRITE_BUFFER_LOW_WATER_MARK, MESSAGE_SIZE_ESTIMATOR);
|
||||
}
|
||||
|
||||
|
@ -137,9 +135,6 @@ public class DefaultChannelConfig implements ChannelConfig {
|
|||
if (option == AUTO_READ) {
|
||||
return (T) Boolean.valueOf(isAutoRead());
|
||||
}
|
||||
if (option == AUTO_CLOSE) {
|
||||
return (T) Boolean.valueOf(isAutoClose());
|
||||
}
|
||||
if (option == WRITE_BUFFER_HIGH_WATER_MARK) {
|
||||
return (T) Integer.valueOf(getWriteBufferHighWaterMark());
|
||||
}
|
||||
|
@ -169,8 +164,6 @@ public class DefaultChannelConfig implements ChannelConfig {
|
|||
setRecvByteBufAllocator((RecvByteBufAllocator) value);
|
||||
} else if (option == AUTO_READ) {
|
||||
setAutoRead((Boolean) value);
|
||||
} else if (option == AUTO_CLOSE) {
|
||||
setAutoClose((Boolean) value);
|
||||
} else if (option == WRITE_BUFFER_HIGH_WATER_MARK) {
|
||||
setWriteBufferHighWaterMark((Integer) value);
|
||||
} else if (option == WRITE_BUFFER_LOW_WATER_MARK) {
|
||||
|
@ -286,17 +279,6 @@ public class DefaultChannelConfig implements ChannelConfig {
|
|||
*/
|
||||
protected void autoReadCleared() { }
|
||||
|
||||
@Override
|
||||
public boolean isAutoClose() {
|
||||
return autoClose;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChannelConfig setAutoClose(boolean autoClose) {
|
||||
this.autoClose = autoClose;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWriteBufferHighWaterMark() {
|
||||
return writeBufferHighWaterMark;
|
||||
|
|
|
@ -260,7 +260,7 @@ final class DefaultChannelPipeline implements ChannelPipeline {
|
|||
}
|
||||
|
||||
private String generateName(ChannelHandler handler) {
|
||||
WeakHashMap<Class<?>, String> cache = nameCaches[(int) (Thread.currentThread().getId() % nameCaches.length)];
|
||||
WeakHashMap<Class<?>, String> cache = nameCaches[(int) (Thread.currentThread().threadId() % nameCaches.length)];
|
||||
Class<?> handlerType = handler.getClass();
|
||||
String name;
|
||||
synchronized (cache) {
|
||||
|
|
|
@ -135,7 +135,7 @@ public final class NioEventLoop extends SingleThreadEventLoop {
|
|||
SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet();
|
||||
|
||||
Class<?> selectorImplClass =
|
||||
Class.forName("sun.nio.ch.SelectorImpl", false, PlatformDependent.getSystemClassLoader());
|
||||
Class.forName("sun.nio.ch.SelectorImpl", false, ClassLoader.getSystemClassLoader());
|
||||
|
||||
// Ensure the current selector implementation is what we can instrument.
|
||||
if (!selectorImplClass.isAssignableFrom(selector.getClass())) {
|
||||
|
|
|
@ -34,7 +34,6 @@ import common.net.channel.ChannelOption;
|
|||
import common.net.channel.DefaultChannelConfig;
|
||||
import common.net.channel.MessageSizeEstimator;
|
||||
import common.net.channel.RecvByteBufAllocator;
|
||||
import common.net.util.internal.PlatformDependent;
|
||||
|
||||
/**
|
||||
* The default {@link SocketChannelConfig} implementation.
|
||||
|
@ -55,15 +54,13 @@ public class DefaultSocketChannelConfig extends DefaultChannelConfig
|
|||
}
|
||||
this.javaSocket = javaSocket;
|
||||
|
||||
// Enable TCP_NODELAY by default if possible.
|
||||
if (PlatformDependent.canEnableTcpNoDelayByDefault()) {
|
||||
// Enable TCP_NODELAY by default.
|
||||
try {
|
||||
setTcpNoDelay(true);
|
||||
} catch (Exception e) {
|
||||
// Ignore.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<ChannelOption<?>, Object> getOptions() {
|
||||
|
@ -322,12 +319,6 @@ public class DefaultSocketChannelConfig extends DefaultChannelConfig
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SocketChannelConfig setAutoClose(boolean autoClose) {
|
||||
super.setAutoClose(autoClose);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
|
||||
super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
|
||||
|
|
|
@ -169,9 +169,6 @@ public interface SocketChannelConfig extends ChannelConfig {
|
|||
@Override
|
||||
SocketChannelConfig setAutoRead(boolean autoRead);
|
||||
|
||||
@Override
|
||||
SocketChannelConfig setAutoClose(boolean autoClose);
|
||||
|
||||
@Override
|
||||
SocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator);
|
||||
}
|
||||
|
|
|
@ -39,11 +39,7 @@ public final class AttributeKey<T> extends UniqueName {
|
|||
return new AttributeKey<T>(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #valueOf(String)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public AttributeKey(String name) {
|
||||
private AttributeKey(String name) {
|
||||
super(names, name);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@ import java.util.StringTokenizer;
|
|||
import common.net.util.internal.PlatformDependent;
|
||||
import common.net.util.internal.logging.InternalLogger;
|
||||
import common.net.util.internal.logging.InternalLoggerFactory;
|
||||
import common.util.Util;
|
||||
|
||||
/**
|
||||
* A class that holds a number of network-related constants.
|
||||
|
@ -183,7 +184,7 @@ public final class NetUtil {
|
|||
// The known defaults:
|
||||
// - Windows NT Server 4.0+: 200
|
||||
// - Linux and Mac OS X: 128
|
||||
int somaxconn = PlatformDependent.isWindows() ? 200 : 128;
|
||||
int somaxconn = Util.WINDOWS ? 200 : 128;
|
||||
File file = new File("/proc/sys/net/core/somaxconn");
|
||||
if (file.exists()) {
|
||||
BufferedReader in = null;
|
||||
|
|
|
@ -95,14 +95,6 @@ public final class ResourceLeakDetector<T> {
|
|||
|
||||
private static final int DEFAULT_SAMPLING_INTERVAL = 113;
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #setLevel(Level)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public static void setEnabled(boolean enabled) {
|
||||
setLevel(enabled? Level.SIMPLE : Level.DISABLED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if resource leak detection is enabled.
|
||||
*/
|
||||
|
|
|
@ -41,11 +41,7 @@ public final class Signal extends Error {
|
|||
return new Signal(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated Use {@link #valueOf(String)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public Signal(String name) {
|
||||
private Signal(String name) {
|
||||
super(name);
|
||||
uname = new UniqueName(map, name);
|
||||
}
|
||||
|
|
|
@ -19,11 +19,8 @@ import java.util.concurrent.ConcurrentMap;
|
|||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @deprecated Known to have problems with class loaders.
|
||||
*
|
||||
* Defines a name that must be unique in the map that is provided during construction.
|
||||
*/
|
||||
@Deprecated
|
||||
public class UniqueName implements Comparable<UniqueName> {
|
||||
|
||||
private static final AtomicInteger nextId = new AtomicInteger();
|
||||
|
@ -36,18 +33,14 @@ public class UniqueName implements Comparable<UniqueName> {
|
|||
*
|
||||
* @param map the map of names to compare with
|
||||
* @param name the name of this {@link UniqueName}
|
||||
* @param args the arguments to process
|
||||
*/
|
||||
public UniqueName(ConcurrentMap<String, Boolean> map, String name, Object... args) {
|
||||
public UniqueName(ConcurrentMap<String, Boolean> map, String name) {
|
||||
if (map == null) {
|
||||
throw new NullPointerException("map");
|
||||
}
|
||||
if (name == null) {
|
||||
throw new NullPointerException("name");
|
||||
}
|
||||
if (args != null && args.length > 0) {
|
||||
validateArgs(args);
|
||||
}
|
||||
|
||||
if (map.putIfAbsent(name, Boolean.TRUE) != null) {
|
||||
throw new IllegalArgumentException(String.format("'%s' is already in use", name));
|
||||
|
@ -57,17 +50,6 @@ public class UniqueName implements Comparable<UniqueName> {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates the given arguments. This method does not do anything on its own, but must be
|
||||
* overridden by its subclasses.
|
||||
*
|
||||
* @param args arguments to validate
|
||||
*/
|
||||
|
||||
protected void validateArgs(Object... args) {
|
||||
// Subclasses will override.
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this {@link UniqueName}'s name
|
||||
*
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
/*
|
||||
* Copyright 2014 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package common.net.util.internal;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import common.net.util.internal.logging.InternalLogger;
|
||||
import common.net.util.internal.logging.InternalLoggerFactory;
|
||||
import sun.misc.Cleaner;
|
||||
|
||||
|
||||
/**
|
||||
* Allows to free direct {@link ByteBuffer} by using {@link Cleaner}. This is encapsulated in an extra class to be able
|
||||
* to use {@link PlatformDependent0} on Android without problems.
|
||||
*
|
||||
* For more details see <a href="https://github.com/netty/netty/issues/2604">#2604</a>.
|
||||
*/
|
||||
final class Cleaner0 {
|
||||
private static final long CLEANER_FIELD_OFFSET;
|
||||
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Cleaner0.class);
|
||||
|
||||
static {
|
||||
ByteBuffer direct = ByteBuffer.allocateDirect(1);
|
||||
Field cleanerField;
|
||||
long fieldOffset = -1;
|
||||
if (PlatformDependent0.hasUnsafe()) {
|
||||
try {
|
||||
cleanerField = direct.getClass().getDeclaredField("cleaner");
|
||||
cleanerField.setAccessible(true);
|
||||
Cleaner cleaner = (Cleaner) cleanerField.get(direct);
|
||||
cleaner.clean();
|
||||
fieldOffset = PlatformDependent0.objectFieldOffset(cleanerField);
|
||||
} catch (Throwable t) {
|
||||
// We don't have ByteBuffer.cleaner().
|
||||
fieldOffset = -1;
|
||||
}
|
||||
}
|
||||
logger.debug("java.nio.ByteBuffer.cleaner(): {}", fieldOffset != -1? "available" : "unavailable");
|
||||
CLEANER_FIELD_OFFSET = fieldOffset;
|
||||
|
||||
// free buffer if possible
|
||||
freeDirectBuffer(direct);
|
||||
}
|
||||
|
||||
static void freeDirectBuffer(ByteBuffer buffer) {
|
||||
if (CLEANER_FIELD_OFFSET == -1 || !buffer.isDirect()) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Cleaner cleaner = (Cleaner) PlatformDependent0.getObject(buffer, CLEANER_FIELD_OFFSET);
|
||||
if (cleaner != null) {
|
||||
cleaner.clean();
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// Nothing we can do here.
|
||||
}
|
||||
}
|
||||
|
||||
private Cleaner0() { }
|
||||
}
|
|
@ -18,16 +18,13 @@ package common.net.util.internal;
|
|||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
||||
|
@ -39,6 +36,7 @@ import java.util.regex.Pattern;
|
|||
import common.net.util.CharsetUtil;
|
||||
import common.net.util.internal.logging.InternalLogger;
|
||||
import common.net.util.internal.logging.InternalLoggerFactory;
|
||||
import common.util.Util;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -56,57 +54,10 @@ public final class PlatformDependent {
|
|||
private static final Pattern MAX_DIRECT_MEMORY_SIZE_ARG_PATTERN = Pattern.compile(
|
||||
"\\s*-XX:MaxDirectMemorySize\\s*=\\s*([0-9]+)\\s*([kKmMgG]?)\\s*$");
|
||||
|
||||
private static final boolean IS_ANDROID = isAndroid0();
|
||||
private static final boolean IS_WINDOWS = isWindows0();
|
||||
private static final boolean IS_ROOT = isRoot0();
|
||||
|
||||
private static final int JAVA_VERSION = javaVersion0();
|
||||
|
||||
private static final boolean CAN_ENABLE_TCP_NODELAY_BY_DEFAULT = !isAndroid();
|
||||
|
||||
private static final boolean HAS_UNSAFE = hasUnsafe0();
|
||||
// private static final boolean CAN_USE_CHM_V8 = HAS_UNSAFE && JAVA_VERSION < 8;
|
||||
private static final boolean DIRECT_BUFFER_PREFERRED =
|
||||
HAS_UNSAFE && !SystemPropertyUtil.getBoolean("game.net.noPreferDirect", false);
|
||||
private static final long MAX_DIRECT_MEMORY = maxDirectMemory0();
|
||||
|
||||
private static final long ARRAY_BASE_OFFSET = arrayBaseOffset0();
|
||||
|
||||
// private static final boolean HAS_JAVASSIST = hasJavassist0();
|
||||
|
||||
// private static final File TMPDIR = tmpdir0();
|
||||
//
|
||||
// private static final int BIT_MODE = bitMode0();
|
||||
//
|
||||
// private static final int ADDRESS_SIZE = addressSize0();
|
||||
|
||||
static {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("-Dgame.net.noPreferDirect: {}", !DIRECT_BUFFER_PREFERRED);
|
||||
}
|
||||
|
||||
if (!hasUnsafe() && !isAndroid()) {
|
||||
logger.info(
|
||||
"Your platform does not provide complete low-level API for accessing direct buffers reliably. " +
|
||||
"Unless explicitly requested, heap buffer will always be preferred to avoid potential system " +
|
||||
"unstability.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if the current platform is Android
|
||||
*/
|
||||
public static boolean isAndroid() {
|
||||
return IS_ANDROID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return {@code true} if the JVM is running on Windows
|
||||
*/
|
||||
public static boolean isWindows() {
|
||||
return IS_WINDOWS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return {@code true} if the current user is root. Note that this method returns
|
||||
* {@code false} if on Windows.
|
||||
|
@ -115,36 +66,6 @@ public final class PlatformDependent {
|
|||
return IS_ROOT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the version of Java under which this library is used.
|
||||
*/
|
||||
public static int javaVersion() {
|
||||
return JAVA_VERSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if it is fine to enable TCP_NODELAY socket option by default.
|
||||
*/
|
||||
public static boolean canEnableTcpNoDelayByDefault() {
|
||||
return CAN_ENABLE_TCP_NODELAY_BY_DEFAULT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return {@code true} if {@code sun.misc.Unsafe} was found on the classpath and can be used for acclerated
|
||||
* direct memory access.
|
||||
*/
|
||||
public static boolean hasUnsafe() {
|
||||
return HAS_UNSAFE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if the platform has reliable low-level direct buffer access API and a user specified
|
||||
* {@code -Dgame.net.preferDirect} option.
|
||||
*/
|
||||
public static boolean directBufferPreferred() {
|
||||
return DIRECT_BUFFER_PREFERRED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum memory reserved for direct buffer allocation.
|
||||
*/
|
||||
|
@ -152,53 +73,13 @@ public final class PlatformDependent {
|
|||
return MAX_DIRECT_MEMORY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if Javassist is available.
|
||||
*/
|
||||
// public static boolean hasJavassist() {
|
||||
// return HAS_JAVASSIST;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Returns the temporary directory.
|
||||
*/
|
||||
// public static File tmpdir() {
|
||||
// return TMPDIR;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Returns the bit mode of the current VM (usually 32 or 64.)
|
||||
*/
|
||||
// public static int bitMode() {
|
||||
// return BIT_MODE;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Return the address size of the OS.
|
||||
* 4 (for 32 bits systems ) and 8 (for 64 bits systems).
|
||||
*/
|
||||
// public static int addressSize() {
|
||||
// return ADDRESS_SIZE;
|
||||
// }
|
||||
|
||||
// public static long allocateMemory(long size) {
|
||||
// return PlatformDependent0.allocateMemory(size);
|
||||
// }
|
||||
|
||||
// public static void freeMemory(long address) {
|
||||
// PlatformDependent0.freeMemory(address);
|
||||
// }
|
||||
|
||||
/**
|
||||
* Raises an exception bypassing compiler checks for checked exceptions.
|
||||
*/
|
||||
public static void throwException(Throwable t) {
|
||||
if (hasUnsafe()) {
|
||||
PlatformDependent0.throwException(t);
|
||||
} else {
|
||||
PlatformDependent.<RuntimeException>throwException0(t);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static <E extends Throwable> void throwException0(Throwable t) throws E {
|
||||
|
@ -266,79 +147,6 @@ public final class PlatformDependent {
|
|||
* the current platform does not support this operation or the specified buffer is not a direct buffer.
|
||||
*/
|
||||
public static void freeDirectBuffer(ByteBuffer buffer) {
|
||||
if (hasUnsafe() && !isAndroid()) {
|
||||
// only direct to method if we are not running on android.
|
||||
// See https://github.com/netty/netty/issues/2604
|
||||
PlatformDependent0.freeDirectBuffer(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
public static long directBufferAddress(ByteBuffer buffer) {
|
||||
return PlatformDependent0.directBufferAddress(buffer);
|
||||
}
|
||||
|
||||
public static Object getObject(Object object, long fieldOffset) {
|
||||
return PlatformDependent0.getObject(object, fieldOffset);
|
||||
}
|
||||
|
||||
public static Object getObjectVolatile(Object object, long fieldOffset) {
|
||||
return PlatformDependent0.getObjectVolatile(object, fieldOffset);
|
||||
}
|
||||
|
||||
public static int getInt(Object object, long fieldOffset) {
|
||||
return PlatformDependent0.getInt(object, fieldOffset);
|
||||
}
|
||||
|
||||
public static long objectFieldOffset(Field field) {
|
||||
return PlatformDependent0.objectFieldOffset(field);
|
||||
}
|
||||
|
||||
public static byte getByte(long address) {
|
||||
return PlatformDependent0.getByte(address);
|
||||
}
|
||||
|
||||
public static short getShort(long address) {
|
||||
return PlatformDependent0.getShort(address);
|
||||
}
|
||||
|
||||
public static int getInt(long address) {
|
||||
return PlatformDependent0.getInt(address);
|
||||
}
|
||||
|
||||
public static long getLong(long address) {
|
||||
return PlatformDependent0.getLong(address);
|
||||
}
|
||||
|
||||
public static void putOrderedObject(Object object, long address, Object value) {
|
||||
PlatformDependent0.putOrderedObject(object, address, value);
|
||||
}
|
||||
|
||||
public static void putByte(long address, byte value) {
|
||||
PlatformDependent0.putByte(address, value);
|
||||
}
|
||||
|
||||
public static void putShort(long address, short value) {
|
||||
PlatformDependent0.putShort(address, value);
|
||||
}
|
||||
|
||||
public static void putInt(long address, int value) {
|
||||
PlatformDependent0.putInt(address, value);
|
||||
}
|
||||
|
||||
public static void putLong(long address, long value) {
|
||||
PlatformDependent0.putLong(address, value);
|
||||
}
|
||||
|
||||
public static void copyMemory(long srcAddr, long dstAddr, long length) {
|
||||
PlatformDependent0.copyMemory(srcAddr, dstAddr, length);
|
||||
}
|
||||
|
||||
public static void copyMemory(byte[] src, int srcIndex, long dstAddr, long length) {
|
||||
PlatformDependent0.copyMemory(src, ARRAY_BASE_OFFSET + srcIndex, null, dstAddr, length);
|
||||
}
|
||||
|
||||
public static void copyMemory(long srcAddr, byte[] dst, int dstIndex, long length) {
|
||||
PlatformDependent0.copyMemory(null, srcAddr, dst, ARRAY_BASE_OFFSET + dstIndex, length);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -348,13 +156,6 @@ public final class PlatformDependent {
|
|||
*/
|
||||
public static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater(
|
||||
Class<U> tclass, String fieldName) {
|
||||
if (hasUnsafe()) {
|
||||
try {
|
||||
return PlatformDependent0.newAtomicReferenceFieldUpdater(tclass, fieldName);
|
||||
} catch (Throwable ignore) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -365,13 +166,6 @@ public final class PlatformDependent {
|
|||
*/
|
||||
public static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater(
|
||||
Class<?> tclass, String fieldName) {
|
||||
if (hasUnsafe()) {
|
||||
try {
|
||||
return PlatformDependent0.newAtomicIntegerFieldUpdater(tclass, fieldName);
|
||||
} catch (Throwable ignore) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -382,13 +176,6 @@ public final class PlatformDependent {
|
|||
*/
|
||||
public static <T> AtomicLongFieldUpdater<T> newAtomicLongFieldUpdater(
|
||||
Class<?> tclass, String fieldName) {
|
||||
if (hasUnsafe()) {
|
||||
try {
|
||||
return PlatformDependent0.newAtomicLongFieldUpdater(tclass, fieldName);
|
||||
} catch (Throwable ignore) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -400,53 +187,8 @@ public final class PlatformDependent {
|
|||
return new MpscLinkedQueue<T>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link ClassLoader} for the given {@link Class}.
|
||||
*/
|
||||
public static ClassLoader getClassLoader(final Class<?> clazz) {
|
||||
return PlatformDependent0.getClassLoader(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the context {@link ClassLoader} for the current {@link Thread}.
|
||||
*/
|
||||
public static ClassLoader getContextClassLoader() {
|
||||
return PlatformDependent0.getContextClassLoader();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the system {@link ClassLoader}.
|
||||
*/
|
||||
public static ClassLoader getSystemClassLoader() {
|
||||
return PlatformDependent0.getSystemClassLoader();
|
||||
}
|
||||
|
||||
private static boolean isAndroid0() {
|
||||
boolean android;
|
||||
try {
|
||||
Class.forName("android.app.Application", false, getSystemClassLoader());
|
||||
android = true;
|
||||
} catch (Exception e) {
|
||||
// Failed to load the class uniquely available in Android.
|
||||
android = false;
|
||||
}
|
||||
|
||||
if (android) {
|
||||
logger.debug("Platform: Android");
|
||||
}
|
||||
return android;
|
||||
}
|
||||
|
||||
private static boolean isWindows0() {
|
||||
boolean windows = SystemPropertyUtil.get("os.name", "").toLowerCase(Locale.US).contains("win");
|
||||
if (windows) {
|
||||
logger.debug("Platform: Windows");
|
||||
}
|
||||
return windows;
|
||||
}
|
||||
|
||||
private static boolean isRoot0() {
|
||||
if (isWindows()) {
|
||||
if (Util.WINDOWS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -539,93 +281,11 @@ public final class PlatformDependent {
|
|||
}
|
||||
|
||||
|
||||
private static int javaVersion0() {
|
||||
int javaVersion;
|
||||
|
||||
// Not really a loop
|
||||
for (;;) {
|
||||
// Android
|
||||
if (isAndroid()) {
|
||||
javaVersion = 6;
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
Class.forName("java.time.Clock", false, getClassLoader(Object.class));
|
||||
javaVersion = 8;
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
try {
|
||||
Class.forName("java.util.concurrent.LinkedTransferQueue", false, getClassLoader(BlockingQueue.class));
|
||||
javaVersion = 7;
|
||||
break;
|
||||
} catch (Exception e) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
javaVersion = 6;
|
||||
break;
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Java version: {}", javaVersion);
|
||||
}
|
||||
return javaVersion;
|
||||
}
|
||||
|
||||
private static boolean hasUnsafe0() {
|
||||
boolean noUnsafe = SystemPropertyUtil.getBoolean("game.net.noUnsafe", false);
|
||||
logger.debug("-Dgame.net.noUnsafe: {}", noUnsafe);
|
||||
|
||||
if (isAndroid()) {
|
||||
logger.debug("sun.misc.Unsafe: unavailable (Android)");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (noUnsafe) {
|
||||
logger.debug("sun.misc.Unsafe: unavailable (game.net.noUnsafe)");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Legacy properties
|
||||
boolean tryUnsafe;
|
||||
if (SystemPropertyUtil.contains("game.net.tryUnsafe")) {
|
||||
tryUnsafe = SystemPropertyUtil.getBoolean("game.net.tryUnsafe", true);
|
||||
} else {
|
||||
tryUnsafe = SystemPropertyUtil.getBoolean("org.jboss.netty.tryUnsafe", true);
|
||||
}
|
||||
|
||||
if (!tryUnsafe) {
|
||||
logger.debug("sun.misc.Unsafe: unavailable (game.net.tryUnsafe/org.jboss.netty.tryUnsafe)");
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
boolean hasUnsafe = PlatformDependent0.hasUnsafe();
|
||||
logger.debug("sun.misc.Unsafe: {}", hasUnsafe ? "available" : "unavailable");
|
||||
return hasUnsafe;
|
||||
} catch (Throwable t) {
|
||||
// Probably failed to initialize PlatformDependent0.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static long arrayBaseOffset0() {
|
||||
if (!hasUnsafe()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return PlatformDependent0.arrayBaseOffset();
|
||||
}
|
||||
|
||||
private static long maxDirectMemory0() {
|
||||
long maxDirectMemory = 0;
|
||||
try {
|
||||
// Try to get from sun.misc.VM.maxDirectMemory() which should be most accurate.
|
||||
Class<?> vmClass = Class.forName("sun.misc.VM", true, getSystemClassLoader());
|
||||
Class<?> vmClass = Class.forName("sun.misc.VM", true, ClassLoader.getSystemClassLoader());
|
||||
Method m = vmClass.getDeclaredMethod("maxDirectMemory");
|
||||
maxDirectMemory = ((Number) m.invoke(null)).longValue();
|
||||
} catch (Throwable t) {
|
||||
|
@ -640,9 +300,9 @@ public final class PlatformDependent {
|
|||
// Now try to get the JVM option (-XX:MaxDirectMemorySize) and parse it.
|
||||
// Note that we are using reflection because Android doesn't have these classes.
|
||||
Class<?> mgmtFactoryClass = Class.forName(
|
||||
"java.lang.management.ManagementFactory", true, getSystemClassLoader());
|
||||
"java.lang.management.ManagementFactory", true, ClassLoader.getSystemClassLoader());
|
||||
Class<?> runtimeClass = Class.forName(
|
||||
"java.lang.management.RuntimeMXBean", true, getSystemClassLoader());
|
||||
"java.lang.management.RuntimeMXBean", true, ClassLoader.getSystemClassLoader());
|
||||
|
||||
Object runtime = mgmtFactoryClass.getDeclaredMethod("getRuntimeMXBean").invoke(null);
|
||||
|
||||
|
@ -682,162 +342,6 @@ public final class PlatformDependent {
|
|||
return maxDirectMemory;
|
||||
}
|
||||
|
||||
// private static boolean hasJavassist0() {
|
||||
// if (isAndroid()) {
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// boolean noJavassist = SystemPropertyUtil.getBoolean("game.net.noJavassist", false);
|
||||
// logger.debug("-Dgame.net.noJavassist: {}", noJavassist);
|
||||
//
|
||||
// if (noJavassist) {
|
||||
// logger.debug("Javassist: unavailable (game.net.noJavassist)");
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// JavassistTypeParameterMatcherGenerator.generate(Object.class, getClassLoader(PlatformDependent.class));
|
||||
// logger.debug("Javassist: available");
|
||||
// return true;
|
||||
// } catch (Throwable t) {
|
||||
// // Failed to generate a Javassist-based matcher.
|
||||
// logger.debug("Javassist: unavailable");
|
||||
// logger.debug(
|
||||
// "You don't have Javassist in your class path or you don't have enough permission " +
|
||||
// "to load dynamically generated classes. Please check the configuration for better performance.");
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
|
||||
// private static File tmpdir0() {
|
||||
// File f;
|
||||
// try {
|
||||
// f = toDirectory(SystemPropertyUtil.get("game.net.tmpdir"));
|
||||
// if (f != null) {
|
||||
// logger.debug("-Dgame.net.tmpdir: {}", f);
|
||||
// return f;
|
||||
// }
|
||||
//
|
||||
// f = toDirectory(SystemPropertyUtil.get("java.io.tmpdir"));
|
||||
// if (f != null) {
|
||||
// logger.debug("-Dgame.net.tmpdir: {} (java.io.tmpdir)", f);
|
||||
// return f;
|
||||
// }
|
||||
//
|
||||
// // This shouldn't happen, but just in case ..
|
||||
// if (isWindows()) {
|
||||
// f = toDirectory(System.getenv("TEMP"));
|
||||
// if (f != null) {
|
||||
// logger.debug("-Dgame.net.tmpdir: {} (%TEMP%)", f);
|
||||
// return f;
|
||||
// }
|
||||
//
|
||||
// String userprofile = System.getenv("USERPROFILE");
|
||||
// if (userprofile != null) {
|
||||
// f = toDirectory(userprofile + "\\AppData\\Local\\Temp");
|
||||
// if (f != null) {
|
||||
// logger.debug("-Dgame.net.tmpdir: {} (%USERPROFILE%\\AppData\\Local\\Temp)", f);
|
||||
// return f;
|
||||
// }
|
||||
//
|
||||
// f = toDirectory(userprofile + "\\Local Settings\\Temp");
|
||||
// if (f != null) {
|
||||
// logger.debug("-Dgame.net.tmpdir: {} (%USERPROFILE%\\Local Settings\\Temp)", f);
|
||||
// return f;
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// f = toDirectory(System.getenv("TMPDIR"));
|
||||
// if (f != null) {
|
||||
// logger.debug("-Dgame.net.tmpdir: {} ($TMPDIR)", f);
|
||||
// return f;
|
||||
// }
|
||||
// }
|
||||
// } catch (Exception ignored) {
|
||||
// // Environment variable inaccessible
|
||||
// }
|
||||
//
|
||||
// // Last resort.
|
||||
// if (isWindows()) {
|
||||
// f = new File("C:\\Windows\\Temp");
|
||||
// } else {
|
||||
// f = new File("/tmp");
|
||||
// }
|
||||
//
|
||||
// logger.warn("Failed to get the temporary directory; falling back to: {}", f);
|
||||
// return f;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// private static File toDirectory(String path) {
|
||||
// if (path == null) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// File f = new File(path);
|
||||
// f.mkdirs();
|
||||
//
|
||||
// if (!f.isDirectory()) {
|
||||
// return null;
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// return f.getAbsoluteFile();
|
||||
// } catch (Exception ignored) {
|
||||
// return f;
|
||||
// }
|
||||
// }
|
||||
|
||||
// private static int bitMode0() {
|
||||
// // Check user-specified bit mode first.
|
||||
// int bitMode = SystemPropertyUtil.getInt("game.net.bitMode", 0);
|
||||
// if (bitMode > 0) {
|
||||
// logger.debug("-Dgame.net.bitMode: {}", bitMode);
|
||||
// return bitMode;
|
||||
// }
|
||||
//
|
||||
// // And then the vendor specific ones which is probably most reliable.
|
||||
// bitMode = SystemPropertyUtil.getInt("sun.arch.data.model", 0);
|
||||
// if (bitMode > 0) {
|
||||
// logger.debug("-Dgame.net.bitMode: {} (sun.arch.data.model)", bitMode);
|
||||
// return bitMode;
|
||||
// }
|
||||
// bitMode = SystemPropertyUtil.getInt("com.ibm.vm.bitmode", 0);
|
||||
// if (bitMode > 0) {
|
||||
// logger.debug("-Dgame.net.bitMode: {} (com.ibm.vm.bitmode)", bitMode);
|
||||
// return bitMode;
|
||||
// }
|
||||
//
|
||||
// // os.arch also gives us a good hint.
|
||||
// String arch = SystemPropertyUtil.get("os.arch", "").toLowerCase(Locale.US).trim();
|
||||
// if ("amd64".equals(arch) || "x86_64".equals(arch)) {
|
||||
// bitMode = 64;
|
||||
// } else if ("i386".equals(arch) || "i486".equals(arch) || "i586".equals(arch) || "i686".equals(arch)) {
|
||||
// bitMode = 32;
|
||||
// }
|
||||
//
|
||||
// if (bitMode > 0) {
|
||||
// logger.debug("-Dgame.net.bitMode: {} (os.arch: {})", bitMode, arch);
|
||||
// }
|
||||
//
|
||||
// // Last resort: guess from VM name and then fall back to most common 64-bit mode.
|
||||
// String vm = SystemPropertyUtil.get("java.vm.name", "").toLowerCase(Locale.US);
|
||||
// Pattern BIT_PATTERN = Pattern.compile("([1-9][0-9]+)-?bit");
|
||||
// Matcher m = BIT_PATTERN.matcher(vm);
|
||||
// if (m.find()) {
|
||||
// return Integer.parseInt(m.group(1));
|
||||
// } else {
|
||||
// return 64;
|
||||
// }
|
||||
// }
|
||||
|
||||
// private static int addressSize0() {
|
||||
// if (!hasUnsafe()) {
|
||||
// return -1;
|
||||
// }
|
||||
// return PlatformDependent0.addressSize();
|
||||
// }
|
||||
|
||||
private PlatformDependent() {
|
||||
// only static method supported
|
||||
}
|
||||
|
|
|
@ -1,383 +0,0 @@
|
|||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package common.net.util.internal;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.Buffer;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
||||
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
|
||||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
||||
|
||||
import common.net.util.internal.logging.InternalLogger;
|
||||
import common.net.util.internal.logging.InternalLoggerFactory;
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
/**
|
||||
* The {@link PlatformDependent} operations which requires access to {@code sun.misc.*}.
|
||||
*/
|
||||
final class PlatformDependent0 {
|
||||
|
||||
private static final InternalLogger logger = InternalLoggerFactory.getInstance(PlatformDependent0.class);
|
||||
private static final Unsafe UNSAFE;
|
||||
private static final boolean BIG_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
|
||||
private static final long ADDRESS_FIELD_OFFSET;
|
||||
|
||||
/**
|
||||
* Limits the number of bytes to copy per {@link Unsafe#copyMemory(long, long, long)} to allow safepoint polling
|
||||
* during a large copy.
|
||||
*/
|
||||
private static final long UNSAFE_COPY_THRESHOLD = 1024L * 1024L;
|
||||
|
||||
/**
|
||||
* {@code true} if and only if the platform supports unaligned access.
|
||||
*
|
||||
* @see <a href="http://en.wikipedia.org/wiki/Segmentation_fault#Bus_error">Wikipedia on segfault</a>
|
||||
*/
|
||||
private static final boolean UNALIGNED;
|
||||
|
||||
static {
|
||||
ByteBuffer direct = ByteBuffer.allocateDirect(1);
|
||||
Field addressField;
|
||||
try {
|
||||
addressField = Buffer.class.getDeclaredField("address");
|
||||
addressField.setAccessible(true);
|
||||
if (addressField.getLong(ByteBuffer.allocate(1)) != 0) {
|
||||
// A heap buffer must have 0 address.
|
||||
addressField = null;
|
||||
} else {
|
||||
if (addressField.getLong(direct) == 0) {
|
||||
// A direct buffer must have non-zero address.
|
||||
addressField = null;
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
// Failed to access the address field.
|
||||
addressField = null;
|
||||
}
|
||||
logger.debug("java.nio.Buffer.address: {}", addressField != null? "available" : "unavailable");
|
||||
|
||||
Unsafe unsafe;
|
||||
if (addressField != null) {
|
||||
try {
|
||||
Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
|
||||
unsafeField.setAccessible(true);
|
||||
unsafe = (Unsafe) unsafeField.get(null);
|
||||
logger.debug("sun.misc.Unsafe.theUnsafe: {}", unsafe != null ? "available" : "unavailable");
|
||||
|
||||
// Ensure the unsafe supports all necessary methods to work around the mistake in the latest OpenJDK.
|
||||
// https://github.com/netty/netty/issues/1061
|
||||
// http://www.mail-archive.com/jdk6-dev@openjdk.java.net/msg00698.html
|
||||
try {
|
||||
if (unsafe != null) {
|
||||
unsafe.getClass().getDeclaredMethod(
|
||||
"copyMemory", Object.class, long.class, Object.class, long.class, long.class);
|
||||
logger.debug("sun.misc.Unsafe.copyMemory: available");
|
||||
}
|
||||
} catch (NoSuchMethodError t) {
|
||||
logger.debug("sun.misc.Unsafe.copyMemory: unavailable");
|
||||
throw t;
|
||||
} catch (NoSuchMethodException e) {
|
||||
logger.debug("sun.misc.Unsafe.copyMemory: unavailable");
|
||||
throw e;
|
||||
}
|
||||
} catch (Throwable cause) {
|
||||
// Unsafe.copyMemory(Object, long, Object, long, long) unavailable.
|
||||
unsafe = null;
|
||||
}
|
||||
} else {
|
||||
// If we cannot access the address of a direct buffer, there's no point of using unsafe.
|
||||
// Let's just pretend unsafe is unavailable for overall simplicity.
|
||||
unsafe = null;
|
||||
}
|
||||
|
||||
UNSAFE = unsafe;
|
||||
|
||||
if (unsafe == null) {
|
||||
ADDRESS_FIELD_OFFSET = -1;
|
||||
UNALIGNED = false;
|
||||
} else {
|
||||
ADDRESS_FIELD_OFFSET = objectFieldOffset(addressField);
|
||||
boolean unaligned;
|
||||
try {
|
||||
Class<?> bitsClass = Class.forName("java.nio.Bits", false, ClassLoader.getSystemClassLoader());
|
||||
Method unalignedMethod = bitsClass.getDeclaredMethod("unaligned");
|
||||
unalignedMethod.setAccessible(true);
|
||||
unaligned = Boolean.TRUE.equals(unalignedMethod.invoke(null));
|
||||
} catch (Throwable t) {
|
||||
// We at least know x86 and x64 support unaligned access.
|
||||
String arch = SystemPropertyUtil.get("os.arch", "");
|
||||
//noinspection DynamicRegexReplaceableByCompiledPattern
|
||||
unaligned = arch.matches("^(i[3-6]86|x86(_64)?|x64|amd64)$");
|
||||
}
|
||||
|
||||
UNALIGNED = unaligned;
|
||||
logger.debug("java.nio.Bits.unaligned: {}", UNALIGNED);
|
||||
}
|
||||
}
|
||||
|
||||
static boolean hasUnsafe() {
|
||||
return UNSAFE != null;
|
||||
}
|
||||
|
||||
static void throwException(Throwable t) {
|
||||
UNSAFE.throwException(t);
|
||||
}
|
||||
|
||||
static void freeDirectBuffer(ByteBuffer buffer) {
|
||||
// Delegate to other class to not break on android
|
||||
// See https://github.com/netty/netty/issues/2604
|
||||
Cleaner0.freeDirectBuffer(buffer);
|
||||
}
|
||||
|
||||
static long directBufferAddress(ByteBuffer buffer) {
|
||||
return getLong(buffer, ADDRESS_FIELD_OFFSET);
|
||||
}
|
||||
|
||||
static long arrayBaseOffset() {
|
||||
return UNSAFE.arrayBaseOffset(byte[].class);
|
||||
}
|
||||
|
||||
static Object getObject(Object object, long fieldOffset) {
|
||||
return UNSAFE.getObject(object, fieldOffset);
|
||||
}
|
||||
|
||||
static Object getObjectVolatile(Object object, long fieldOffset) {
|
||||
return UNSAFE.getObjectVolatile(object, fieldOffset);
|
||||
}
|
||||
|
||||
static int getInt(Object object, long fieldOffset) {
|
||||
return UNSAFE.getInt(object, fieldOffset);
|
||||
}
|
||||
|
||||
private static long getLong(Object object, long fieldOffset) {
|
||||
return UNSAFE.getLong(object, fieldOffset);
|
||||
}
|
||||
|
||||
static long objectFieldOffset(Field field) {
|
||||
return UNSAFE.objectFieldOffset(field);
|
||||
}
|
||||
|
||||
static byte getByte(long address) {
|
||||
return UNSAFE.getByte(address);
|
||||
}
|
||||
|
||||
static short getShort(long address) {
|
||||
if (UNALIGNED) {
|
||||
return UNSAFE.getShort(address);
|
||||
} else if (BIG_ENDIAN) {
|
||||
return (short) (getByte(address) << 8 | getByte(address + 1) & 0xff);
|
||||
} else {
|
||||
return (short) (getByte(address + 1) << 8 | getByte(address) & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
static int getInt(long address) {
|
||||
if (UNALIGNED) {
|
||||
return UNSAFE.getInt(address);
|
||||
} else if (BIG_ENDIAN) {
|
||||
return getByte(address) << 24 |
|
||||
(getByte(address + 1) & 0xff) << 16 |
|
||||
(getByte(address + 2) & 0xff) << 8 |
|
||||
getByte(address + 3) & 0xff;
|
||||
} else {
|
||||
return getByte(address + 3) << 24 |
|
||||
(getByte(address + 2) & 0xff) << 16 |
|
||||
(getByte(address + 1) & 0xff) << 8 |
|
||||
getByte(address) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
static long getLong(long address) {
|
||||
if (UNALIGNED) {
|
||||
return UNSAFE.getLong(address);
|
||||
} else if (BIG_ENDIAN) {
|
||||
return (long) getByte(address) << 56 |
|
||||
((long) getByte(address + 1) & 0xff) << 48 |
|
||||
((long) getByte(address + 2) & 0xff) << 40 |
|
||||
((long) getByte(address + 3) & 0xff) << 32 |
|
||||
((long) getByte(address + 4) & 0xff) << 24 |
|
||||
((long) getByte(address + 5) & 0xff) << 16 |
|
||||
((long) getByte(address + 6) & 0xff) << 8 |
|
||||
(long) getByte(address + 7) & 0xff;
|
||||
} else {
|
||||
return (long) getByte(address + 7) << 56 |
|
||||
((long) getByte(address + 6) & 0xff) << 48 |
|
||||
((long) getByte(address + 5) & 0xff) << 40 |
|
||||
((long) getByte(address + 4) & 0xff) << 32 |
|
||||
((long) getByte(address + 3) & 0xff) << 24 |
|
||||
((long) getByte(address + 2) & 0xff) << 16 |
|
||||
((long) getByte(address + 1) & 0xff) << 8 |
|
||||
(long) getByte(address) & 0xff;
|
||||
}
|
||||
}
|
||||
|
||||
static void putOrderedObject(Object object, long address, Object value) {
|
||||
UNSAFE.putOrderedObject(object, address, value);
|
||||
}
|
||||
|
||||
static void putByte(long address, byte value) {
|
||||
UNSAFE.putByte(address, value);
|
||||
}
|
||||
|
||||
static void putShort(long address, short value) {
|
||||
if (UNALIGNED) {
|
||||
UNSAFE.putShort(address, value);
|
||||
} else if (BIG_ENDIAN) {
|
||||
putByte(address, (byte) (value >>> 8));
|
||||
putByte(address + 1, (byte) value);
|
||||
} else {
|
||||
putByte(address + 1, (byte) (value >>> 8));
|
||||
putByte(address, (byte) value);
|
||||
}
|
||||
}
|
||||
|
||||
static void putInt(long address, int value) {
|
||||
if (UNALIGNED) {
|
||||
UNSAFE.putInt(address, value);
|
||||
} else if (BIG_ENDIAN) {
|
||||
putByte(address, (byte) (value >>> 24));
|
||||
putByte(address + 1, (byte) (value >>> 16));
|
||||
putByte(address + 2, (byte) (value >>> 8));
|
||||
putByte(address + 3, (byte) value);
|
||||
} else {
|
||||
putByte(address + 3, (byte) (value >>> 24));
|
||||
putByte(address + 2, (byte) (value >>> 16));
|
||||
putByte(address + 1, (byte) (value >>> 8));
|
||||
putByte(address, (byte) value);
|
||||
}
|
||||
}
|
||||
|
||||
static void putLong(long address, long value) {
|
||||
if (UNALIGNED) {
|
||||
UNSAFE.putLong(address, value);
|
||||
} else if (BIG_ENDIAN) {
|
||||
putByte(address, (byte) (value >>> 56));
|
||||
putByte(address + 1, (byte) (value >>> 48));
|
||||
putByte(address + 2, (byte) (value >>> 40));
|
||||
putByte(address + 3, (byte) (value >>> 32));
|
||||
putByte(address + 4, (byte) (value >>> 24));
|
||||
putByte(address + 5, (byte) (value >>> 16));
|
||||
putByte(address + 6, (byte) (value >>> 8));
|
||||
putByte(address + 7, (byte) value);
|
||||
} else {
|
||||
putByte(address + 7, (byte) (value >>> 56));
|
||||
putByte(address + 6, (byte) (value >>> 48));
|
||||
putByte(address + 5, (byte) (value >>> 40));
|
||||
putByte(address + 4, (byte) (value >>> 32));
|
||||
putByte(address + 3, (byte) (value >>> 24));
|
||||
putByte(address + 2, (byte) (value >>> 16));
|
||||
putByte(address + 1, (byte) (value >>> 8));
|
||||
putByte(address, (byte) value);
|
||||
}
|
||||
}
|
||||
|
||||
static void copyMemory(long srcAddr, long dstAddr, long length) {
|
||||
//UNSAFE.copyMemory(srcAddr, dstAddr, length);
|
||||
while (length > 0) {
|
||||
long size = Math.min(length, UNSAFE_COPY_THRESHOLD);
|
||||
UNSAFE.copyMemory(srcAddr, dstAddr, size);
|
||||
length -= size;
|
||||
srcAddr += size;
|
||||
dstAddr += size;
|
||||
}
|
||||
}
|
||||
|
||||
static void copyMemory(Object src, long srcOffset, Object dst, long dstOffset, long length) {
|
||||
//UNSAFE.copyMemory(src, srcOffset, dst, dstOffset, length);
|
||||
while (length > 0) {
|
||||
long size = Math.min(length, UNSAFE_COPY_THRESHOLD);
|
||||
UNSAFE.copyMemory(src, srcOffset, dst, dstOffset, size);
|
||||
length -= size;
|
||||
srcOffset += size;
|
||||
dstOffset += size;
|
||||
}
|
||||
}
|
||||
|
||||
static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater(
|
||||
Class<U> tclass, String fieldName) throws Exception {
|
||||
return new UnsafeAtomicReferenceFieldUpdater<U, W>(UNSAFE, tclass, fieldName);
|
||||
}
|
||||
|
||||
static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater(
|
||||
Class<?> tclass, String fieldName) throws Exception {
|
||||
return new UnsafeAtomicIntegerFieldUpdater<T>(UNSAFE, tclass, fieldName);
|
||||
}
|
||||
|
||||
static <T> AtomicLongFieldUpdater<T> newAtomicLongFieldUpdater(
|
||||
Class<?> tclass, String fieldName) throws Exception {
|
||||
return new UnsafeAtomicLongFieldUpdater<T>(UNSAFE, tclass, fieldName);
|
||||
}
|
||||
|
||||
static ClassLoader getClassLoader(final Class<?> clazz) {
|
||||
if (System.getSecurityManager() == null) {
|
||||
return clazz.getClassLoader();
|
||||
} else {
|
||||
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
|
||||
@Override
|
||||
public ClassLoader run() {
|
||||
return clazz.getClassLoader();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static ClassLoader getContextClassLoader() {
|
||||
if (System.getSecurityManager() == null) {
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
} else {
|
||||
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
|
||||
@Override
|
||||
public ClassLoader run() {
|
||||
return Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static ClassLoader getSystemClassLoader() {
|
||||
if (System.getSecurityManager() == null) {
|
||||
return ClassLoader.getSystemClassLoader();
|
||||
} else {
|
||||
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
|
||||
@Override
|
||||
public ClassLoader run() {
|
||||
return ClassLoader.getSystemClassLoader();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static int addressSize() {
|
||||
return UNSAFE.addressSize();
|
||||
}
|
||||
|
||||
static long allocateMemory(long size) {
|
||||
return UNSAFE.allocateMemory(size);
|
||||
}
|
||||
|
||||
static void freeMemory(long address) {
|
||||
UNSAFE.freeMemory(address);
|
||||
}
|
||||
|
||||
private PlatformDependent0() {
|
||||
}
|
||||
|
||||
}
|
|
@ -15,8 +15,6 @@
|
|||
*/
|
||||
package common.net.util.internal;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -76,16 +74,7 @@ public final class SystemPropertyUtil {
|
|||
|
||||
String value = null;
|
||||
try {
|
||||
if (System.getSecurityManager() == null) {
|
||||
value = System.getProperty(key);
|
||||
} else {
|
||||
value = AccessController.doPrivileged(new PrivilegedAction<String>() {
|
||||
@Override
|
||||
public String run() {
|
||||
return System.getProperty(key);
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (Exception e) {
|
||||
if (!loggedException) {
|
||||
log("Unable to retrieve a system property '" + key + "'; default values will be used.", e);
|
||||
|
|
|
@ -1,61 +0,0 @@
|
|||
/*
|
||||
* Copyright 2014 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package common.net.util.internal;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
|
||||
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
final class UnsafeAtomicIntegerFieldUpdater<T> extends AtomicIntegerFieldUpdater<T> {
|
||||
private final long offset;
|
||||
private final Unsafe unsafe;
|
||||
|
||||
UnsafeAtomicIntegerFieldUpdater(Unsafe unsafe, Class<?> tClass, String fieldName) throws NoSuchFieldException {
|
||||
Field field = tClass.getDeclaredField(fieldName);
|
||||
if (!Modifier.isVolatile(field.getModifiers())) {
|
||||
throw new IllegalArgumentException("Must be volatile");
|
||||
}
|
||||
this.unsafe = unsafe;
|
||||
offset = unsafe.objectFieldOffset(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean compareAndSet(T obj, int expect, int update) {
|
||||
return unsafe.compareAndSwapInt(obj, offset, expect, update);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean weakCompareAndSet(T obj, int expect, int update) {
|
||||
return unsafe.compareAndSwapInt(obj, offset, expect, update);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(T obj, int newValue) {
|
||||
unsafe.putIntVolatile(obj, offset, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lazySet(T obj, int newValue) {
|
||||
unsafe.putOrderedInt(obj, offset, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int get(T obj) {
|
||||
return unsafe.getIntVolatile(obj, offset);
|
||||
}
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
/*
|
||||
* Copyright 2014 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package common.net.util.internal;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
|
||||
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
final class UnsafeAtomicLongFieldUpdater<T> extends AtomicLongFieldUpdater<T> {
|
||||
private final long offset;
|
||||
private final Unsafe unsafe;
|
||||
|
||||
UnsafeAtomicLongFieldUpdater(Unsafe unsafe, Class<?> tClass, String fieldName) throws NoSuchFieldException {
|
||||
Field field = tClass.getDeclaredField(fieldName);
|
||||
if (!Modifier.isVolatile(field.getModifiers())) {
|
||||
throw new IllegalArgumentException("Must be volatile");
|
||||
}
|
||||
this.unsafe = unsafe;
|
||||
offset = unsafe.objectFieldOffset(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean compareAndSet(T obj, long expect, long update) {
|
||||
return unsafe.compareAndSwapLong(obj, offset, expect, update);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean weakCompareAndSet(T obj, long expect, long update) {
|
||||
return unsafe.compareAndSwapLong(obj, offset, expect, update);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(T obj, long newValue) {
|
||||
unsafe.putLongVolatile(obj, offset, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lazySet(T obj, long newValue) {
|
||||
unsafe.putOrderedLong(obj, offset, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long get(T obj) {
|
||||
return unsafe.getLongVolatile(obj, offset);
|
||||
}
|
||||
}
|
|
@ -1,62 +0,0 @@
|
|||
/*
|
||||
* Copyright 2014 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package common.net.util.internal;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
|
||||
|
||||
import sun.misc.Unsafe;
|
||||
|
||||
final class UnsafeAtomicReferenceFieldUpdater<U, M> extends AtomicReferenceFieldUpdater<U, M> {
|
||||
private final long offset;
|
||||
private final Unsafe unsafe;
|
||||
|
||||
UnsafeAtomicReferenceFieldUpdater(Unsafe unsafe, Class<U> tClass, String fieldName) throws NoSuchFieldException {
|
||||
Field field = tClass.getDeclaredField(fieldName);
|
||||
if (!Modifier.isVolatile(field.getModifiers())) {
|
||||
throw new IllegalArgumentException("Must be volatile");
|
||||
}
|
||||
this.unsafe = unsafe;
|
||||
offset = unsafe.objectFieldOffset(field);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean compareAndSet(U obj, M expect, M update) {
|
||||
return unsafe.compareAndSwapObject(obj, offset, expect, update);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean weakCompareAndSet(U obj, M expect, M update) {
|
||||
return unsafe.compareAndSwapObject(obj, offset, expect, update);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(U obj, M newValue) {
|
||||
unsafe.putObjectVolatile(obj, offset, newValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void lazySet(U obj, M newValue) {
|
||||
unsafe.putOrderedObject(obj, offset, newValue);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public M get(U obj) {
|
||||
return (M) unsafe.getObjectVolatile(obj, offset);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package common.network;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Map;
|
||||
|
||||
import common.collect.BiMap;
|
||||
|
@ -225,9 +226,9 @@ public enum PacketRegistry {
|
|||
return (client ? this.client : this.server).inverse().get(packet.getClass());
|
||||
}
|
||||
|
||||
public Packet getPacket(boolean client, int id) throws InstantiationException, IllegalAccessException {
|
||||
public Packet getPacket(boolean client, int id) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
|
||||
Class<? extends Packet> oclass = (client ? this.client : this.server).get(id);
|
||||
return oclass == null ? null : oclass.newInstance();
|
||||
return oclass == null ? null : oclass.getConstructor().newInstance();
|
||||
}
|
||||
|
||||
public static PacketRegistry getType(Packet packet) {
|
||||
|
@ -241,7 +242,7 @@ public enum PacketRegistry {
|
|||
if(STATES.containsKey(clazz) && STATES.get(clazz) != reg)
|
||||
throw new Error("Paket " + clazz + " ist bereits zu ID " + STATES.get(clazz) + " zugewiesen - kann nicht auf " + reg + " neu zuweisen");
|
||||
try {
|
||||
clazz.newInstance();
|
||||
clazz.getConstructor().newInstance();
|
||||
}
|
||||
catch(Throwable e) {
|
||||
throw new Error("Paket " + clazz + " kann nicht instanziert werden!");
|
||||
|
|
|
@ -88,7 +88,7 @@ public abstract class TileEntity
|
|||
|
||||
if (oclass != null)
|
||||
{
|
||||
tileentity = (TileEntity)oclass.newInstance();
|
||||
tileentity = (TileEntity)oclass.getConstructor().newInstance();
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
|
|
|
@ -2,6 +2,7 @@ package common.util;
|
|||
|
||||
import java.awt.GraphicsEnvironment;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
|
@ -14,7 +15,8 @@ import common.log.Log;
|
|||
|
||||
public abstract class Util {
|
||||
private static final long START = getTime();
|
||||
public static final boolean WINDOWS = System.getProperty("os.name").startsWith("Windows");
|
||||
private static final int MIN_JAVA_FEATURE = 21;
|
||||
public static final boolean WINDOWS = System.getProperty("os.name").toLowerCase(Locale.US).contains("win");
|
||||
public static final int PROTOCOL = Version.MAJOR << 16 | Version.MINOR << 8 | Version.PATCH;
|
||||
public static final String VERSION = "v" + Version.MAJOR + "." + Version.MINOR + "." + Version.PATCH + Version.RELEASE;
|
||||
|
||||
|
@ -306,11 +308,26 @@ int utf_len(const char *str) {
|
|||
(((color & 255) * mul) / 255);
|
||||
}
|
||||
|
||||
public static void checkOs() {
|
||||
public static void checkPlatform() {
|
||||
int feature;
|
||||
try {
|
||||
feature = Runtime.version().feature();
|
||||
}
|
||||
catch(Throwable t) {
|
||||
feature = 0;
|
||||
}
|
||||
String info = null;
|
||||
String msg = null;
|
||||
if(System.getProperty("os.name").startsWith("Mac")) {
|
||||
String info = "Inkompatibles Betriebssystem";
|
||||
String msg = "Linux, *BSD oder Windows ist erforderlich, um dieses Programm\n" +
|
||||
info = "Inkompatibles Betriebssystem";
|
||||
msg = "Linux, *BSD oder Windows ist erforderlich, um dieses Programm\n" +
|
||||
"auszuführen. Alle Versionen von Mac OS (X) sind nicht kompatibel.";
|
||||
}
|
||||
else if(feature < MIN_JAVA_FEATURE) {
|
||||
info = "Inkompatible Java-Version";
|
||||
msg = "Java " + MIN_JAVA_FEATURE + " oder höher ist erforderlich, um dieses Programm auszuführen.";
|
||||
}
|
||||
if(info != null) {
|
||||
System.err.println("#################################################################");
|
||||
System.err.println("*** " + info + " ***");
|
||||
System.err.println(msg);
|
||||
|
|
|
@ -11,7 +11,7 @@ dependencies {
|
|||
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(8)
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -150,7 +150,8 @@ public final class Server implements IThreadListener {
|
|||
|
||||
public static void main(String[] args) {
|
||||
long time = System.currentTimeMillis();
|
||||
Util.checkOs();
|
||||
Util.checkPlatform();
|
||||
Log.init();
|
||||
Registry.setup("Server thread");
|
||||
Log.SYSTEM.info("Starte " + Version.NAME + " Server Version " + Util.VERSION + " (Protokoll #" + Util.PROTOCOL + ")");
|
||||
GenBiome.setAsProvider();
|
||||
|
@ -164,6 +165,7 @@ public final class Server implements IThreadListener {
|
|||
server.stopServer();
|
||||
}
|
||||
});
|
||||
Log.setSync(server);
|
||||
server.run(time);
|
||||
Region.killIO();
|
||||
Log.flushLog();
|
||||
|
|
|
@ -46,7 +46,7 @@ public class MapGenStructureIO
|
|||
|
||||
if (oclass != null)
|
||||
{
|
||||
structurestart = (StructureStart)oclass.newInstance();
|
||||
structurestart = (StructureStart)oclass.getConstructor().newInstance();
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
|
@ -77,7 +77,7 @@ public class MapGenStructureIO
|
|||
|
||||
if (oclass != null)
|
||||
{
|
||||
structurecomponent = (StructureComponent)oclass.newInstance();
|
||||
structurecomponent = (StructureComponent)oclass.getConstructor().newInstance();
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue