- java.lang.Object
-
- io.netty5.util.internal.PlatformDependent
-
public final class PlatformDependent extends Object
Utility that detects various properties specific to the current runtime environment, such as Java version and the availability of thesun.misc.Unsafeobject.You can disable the use of
sun.misc.Unsafeif you specify the system property io.netty5.noUnsafe.
-
-
Field Summary
Fields Modifier and Type Field Description static booleanBIG_ENDIAN_NATIVE_ORDER
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intaddressSize()Return the address size of the OS. 4 (for 32 bits systems ) and 8 (for 64 bits systems).static longalign(long value, int alignment)static ByteBufferalignDirectBuffer(ByteBuffer buffer, int alignment)static ByteBufferallocateDirectNoCleaner(int capacity)Allocate a newByteBufferwith the givencapacity.static longallocateMemory(long size)static byte[]allocateUninitializedArray(int size)static intbitMode()Returns the bit mode of the current VM (usually 32 or 64.)static longbyteArrayBaseOffset()static booleancanEnableTcpNoDelayByDefault()Returnstrueif and only if it is fine to enable TCP_NODELAY socket option by default.static voidcopyMemory(byte[] src, int srcIndex, byte[] dst, int dstIndex, long length)static voidcopyMemory(byte[] src, int srcIndex, long dstAddr, long length)static voidcopyMemory(long srcAddr, byte[] dst, int dstIndex, long length)static voidcopyMemory(long srcAddr, long dstAddr, long length)static voidcopyMemory(Object src, long srcOffset, Object dst, long dstOffset, long length)static FilecreateTempFile(String prefix, String suffix, File directory)static ByteBufferdirectBuffer(long memoryAddress, int size, Object attachment)static longdirectBufferAddress(ByteBuffer buffer)static booleandirectBufferPreferred()Returnstrueif the platform has reliable low-level direct buffer access API and a user has not specified-Dio.netty5.noPreferDirectoption.static booleanequals(byte[] bytes1, int startPos1, byte[] bytes2, int startPos2, int length)Compare twobytearrays for equality.static intequalsConstantTime(byte[] bytes1, int startPos1, byte[] bytes2, int startPos2, int length)Compare twobytearrays for equality without leaking timing information.static longestimateMaxDirectMemory()Compute an estimate of the maximum amount of direct memory available to this JVM.static voidfreeDirectBuffer(ByteBuffer buffer)Try to deallocate the specified directByteBuffer.static voidfreeDirectNoCleaner(ByteBuffer buffer)This method MUST only be called forByteBuffers that were allocated viaallocateDirectNoCleaner(int).static voidfreeMemory(long address)static bytegetByte(byte[] data, int index)static bytegetByte(byte[] data, long index)static bytegetByte(long address)static bytegetByte(Object object, long fieldOffset)static chargetChar(Object object, long fieldOffset)static ClassLoadergetClassLoader(Class<?> clazz)Return theClassLoaderfor the givenClass.static ClassLoadergetContextClassLoader()Return the contextClassLoaderfor the currentThread.static doublegetDouble(Object object, long fieldOffset)static floatgetFloat(Object object, long fieldOffset)static intgetInt(byte[] data, int index)static intgetInt(int[] data, long index)static intgetInt(long address)static intgetInt(Object object, long fieldOffset)static intgetIntVolatile(long address)static longgetLong(byte[] data, int index)static longgetLong(long address)static longgetLong(long[] data, long index)static longgetLong(Object object, long fieldOffset)static ObjectgetObject(Object object, long fieldOffset)static shortgetShort(byte[] data, int index)static shortgetShort(long address)static shortgetShort(Object object, long fieldOffset)static ClassLoadergetSystemClassLoader()Return the systemClassLoader.static ThrowablegetUnsafeUnavailabilityCause()Return the reason (if any) whysun.misc.Unsafewas not available.static booleanhasAlignDirectByteBuffer()static booleanhasDirectBufferNoCleanerConstructor()static inthashCodeAscii(byte[] bytes, int startPos, int length)Calculate a hash code of a byte array assuming ASCII character encoding.static inthashCodeAscii(CharSequence bytes)Calculate a hash code of a byte array assuming ASCII character encoding.static booleanhasUnsafe()Returntrueifsun.misc.Unsafewas found on the classpath and can be used for accelerated direct memory access.static booleanisAndroid()Returnstrueif and only if the current platform is Androidstatic booleanisIkvmDotNet()static booleanisJ9Jvm()static booleanisOsx()Returntrueif the JVM is running on OSX / MacOSstatic booleanisUnaligned()trueif and only if the platform supports unaligned access.static booleanisWindows()Returntrueif the JVM is running on Windowsstatic booleanisZero(byte[] bytes, int startPos, int length)Determine if a subsection of an array is zero.static intjavaVersion()Return the version of Java under which this library is used.static longmaxDirectMemory()Returns the maximum memory reserved for direct buffer allocation.static booleanmaybeSuperUser()Returntrueif the current user may be a super-user.static <C> Deque<C>newConcurrentDeque()Returns a new concurrentDeque.static <T> Queue<T>newFixedMpscQueue(int capacity)Create a newQueuewhich is safe to use for multiple producers (different threads) and a single consumer (one thread!)static <T> Queue<T>newMpscQueue()Create a newQueuewhich is safe to use for multiple producers (different threads) and a single consumer (one thread!).static <T> Queue<T>newMpscQueue(int maxCapacity)Create a newQueuewhich is safe to use for multiple producers (different threads) and a single consumer (one thread!).static <T> Queue<T>newMpscQueue(int chunkSize, int maxCapacity)Create a newQueuewhich is safe to use for multiple producers (different threads) and a single consumer (one thread!).static <T> Queue<T>newSpscQueue()Create a newQueuewhich is safe to use for single producer (one thread!)static StringnormalizedArch()static Set<String>normalizedLinuxClassifiers()static StringnormalizedOs()static longobjectFieldOffset(Field field)static voidputByte(byte[] data, int index, byte value)static voidputByte(long address, byte value)static voidputByte(Object data, long offset, byte value)static voidputChar(Object data, long offset, char value)static voidputDouble(Object data, long offset, double value)static voidputFloat(Object data, long offset, float value)static voidputInt(byte[] data, int index, int value)static voidputInt(long address, int value)static voidputInt(Object data, long offset, int value)static voidputIntOrdered(long adddress, int newValue)static voidputLong(byte[] data, int index, long value)static voidputLong(long address, long value)static voidputLong(Object data, long offset, long value)static voidputObject(Object o, long offset, Object x)static voidputShort(byte[] data, int index, short value)static voidputShort(long address, short value)static voidputShort(Object data, long offset, short value)static ByteBufferreallocateDirectNoCleaner(ByteBuffer buffer, int capacity)Reallocate a newByteBufferwith the givencapacity.static longreallocateMemory(long address, long newSize)static introundToPowerOfTwo(int value)static voidsetMemory(byte[] dst, int dstIndex, long bytes, byte value)static voidsetMemory(long address, long bytes, byte value)static voidsetMemory(Object base, long offset, long length, byte value)static voidthrowException(Throwable t)Raises an exception bypassing compiler checks for checked exceptions.static Filetmpdir()Returns the temporary directory.static ObjectunwrapUnsafeOrNull()static longusedDirectMemory()Returns the current memory reserved for direct buffer allocation.static booleanuseDirectBufferNoCleaner()
-
-
-
Method Detail
-
byteArrayBaseOffset
public static long byteArrayBaseOffset()
-
hasDirectBufferNoCleanerConstructor
public static boolean hasDirectBufferNoCleanerConstructor()
-
allocateUninitializedArray
public static byte[] allocateUninitializedArray(int size)
-
isAndroid
public static boolean isAndroid()
Returnstrueif and only if the current platform is Android
-
isWindows
public static boolean isWindows()
Returntrueif the JVM is running on Windows
-
isOsx
public static boolean isOsx()
Returntrueif the JVM is running on OSX / MacOS
-
maybeSuperUser
public static boolean maybeSuperUser()
Returntrueif the current user may be a super-user. Be aware that this is just an hint and so it may return false-positives.
-
javaVersion
public static int javaVersion()
Return the version of Java under which this library is used.
-
canEnableTcpNoDelayByDefault
public static boolean canEnableTcpNoDelayByDefault()
Returnstrueif and only if it is fine to enable TCP_NODELAY socket option by default.
-
hasUnsafe
public static boolean hasUnsafe()
Returntrueifsun.misc.Unsafewas found on the classpath and can be used for accelerated direct memory access.
-
getUnsafeUnavailabilityCause
public static Throwable getUnsafeUnavailabilityCause()
Return the reason (if any) whysun.misc.Unsafewas not available.
-
isUnaligned
public static boolean isUnaligned()
trueif and only if the platform supports unaligned access.- See Also:
- Wikipedia on segfault
-
directBufferPreferred
public static boolean directBufferPreferred()
Returnstrueif the platform has reliable low-level direct buffer access API and a user has not specified-Dio.netty5.noPreferDirectoption.
-
maxDirectMemory
public static long maxDirectMemory()
Returns the maximum memory reserved for direct buffer allocation.
-
usedDirectMemory
public static long usedDirectMemory()
Returns the current memory reserved for direct buffer allocation. This method returns -1 in case that a value is not available.- See Also:
maxDirectMemory()
-
tmpdir
public static File tmpdir()
Returns the temporary directory.
-
bitMode
public static int bitMode()
Returns the bit mode of the current VM (usually 32 or 64.)
-
addressSize
public static int addressSize()
Return the address size of the OS. 4 (for 32 bits systems ) and 8 (for 64 bits systems).
-
allocateMemory
public static long allocateMemory(long size)
-
freeMemory
public static void freeMemory(long address)
-
reallocateMemory
public static long reallocateMemory(long address, long newSize)
-
throwException
public static void throwException(Throwable t)
Raises an exception bypassing compiler checks for checked exceptions.
-
freeDirectBuffer
public static void freeDirectBuffer(ByteBuffer buffer)
Try to deallocate the specified directByteBuffer. Please note this method does nothing if the current platform does not support this operation or the specified buffer is not a direct buffer.
-
directBufferAddress
public static long directBufferAddress(ByteBuffer buffer)
-
directBuffer
public static ByteBuffer directBuffer(long memoryAddress, int size, Object attachment)
-
getByte
public static byte getByte(Object object, long fieldOffset)
-
getShort
public static short getShort(Object object, long fieldOffset)
-
getChar
public static char getChar(Object object, long fieldOffset)
-
getInt
public static int getInt(Object object, long fieldOffset)
-
getFloat
public static float getFloat(Object object, long fieldOffset)
-
getLong
public static long getLong(Object object, long fieldOffset)
-
getDouble
public static double getDouble(Object object, long fieldOffset)
-
getIntVolatile
public static int getIntVolatile(long address)
-
putIntOrdered
public static void putIntOrdered(long adddress, int newValue)
-
getByte
public static byte getByte(long address)
-
getShort
public static short getShort(long address)
-
getInt
public static int getInt(long address)
-
getLong
public static long getLong(long address)
-
getByte
public static byte getByte(byte[] data, int index)
-
getByte
public static byte getByte(byte[] data, long index)
-
getShort
public static short getShort(byte[] data, int index)
-
getInt
public static int getInt(byte[] data, int index)
-
getInt
public static int getInt(int[] data, long index)
-
getLong
public static long getLong(byte[] data, int index)
-
getLong
public static long getLong(long[] data, long index)
-
putByte
public static void putByte(long address, byte value)
-
putShort
public static void putShort(long address, short value)
-
putInt
public static void putInt(long address, int value)
-
putLong
public static void putLong(long address, long value)
-
putByte
public static void putByte(byte[] data, int index, byte value)
-
putByte
public static void putByte(Object data, long offset, byte value)
-
putShort
public static void putShort(Object data, long offset, short value)
-
putChar
public static void putChar(Object data, long offset, char value)
-
putInt
public static void putInt(Object data, long offset, int value)
-
putFloat
public static void putFloat(Object data, long offset, float value)
-
putLong
public static void putLong(Object data, long offset, long value)
-
putDouble
public static void putDouble(Object data, long offset, double value)
-
putShort
public static void putShort(byte[] data, int index, short value)
-
putInt
public static void putInt(byte[] data, int index, int value)
-
putLong
public static void putLong(byte[] data, int index, long value)
-
objectFieldOffset
public static long objectFieldOffset(Field field)
-
copyMemory
public static void copyMemory(long srcAddr, long dstAddr, long length)
-
copyMemory
public static void copyMemory(byte[] src, int srcIndex, long dstAddr, long length)
-
copyMemory
public static void copyMemory(byte[] src, int srcIndex, byte[] dst, int dstIndex, long length)
-
copyMemory
public static void copyMemory(Object src, long srcOffset, Object dst, long dstOffset, long length)
-
copyMemory
public static void copyMemory(long srcAddr, byte[] dst, int dstIndex, long length)
-
setMemory
public static void setMemory(byte[] dst, int dstIndex, long bytes, byte value)
-
setMemory
public static void setMemory(Object base, long offset, long length, byte value)
-
setMemory
public static void setMemory(long address, long bytes, byte value)
-
allocateDirectNoCleaner
public static ByteBuffer allocateDirectNoCleaner(int capacity)
Allocate a newByteBufferwith the givencapacity.ByteBuffers allocated with this method MUST be deallocated viafreeDirectNoCleaner(ByteBuffer).
-
reallocateDirectNoCleaner
public static ByteBuffer reallocateDirectNoCleaner(ByteBuffer buffer, int capacity)
Reallocate a newByteBufferwith the givencapacity.ByteBuffers reallocated with this method MUST be deallocated viafreeDirectNoCleaner(ByteBuffer).
-
freeDirectNoCleaner
public static void freeDirectNoCleaner(ByteBuffer buffer)
This method MUST only be called forByteBuffers that were allocated viaallocateDirectNoCleaner(int).
-
hasAlignDirectByteBuffer
public static boolean hasAlignDirectByteBuffer()
-
alignDirectBuffer
public static ByteBuffer alignDirectBuffer(ByteBuffer buffer, int alignment)
-
align
public static long align(long value, int alignment)
-
roundToPowerOfTwo
public static int roundToPowerOfTwo(int value)
-
useDirectBufferNoCleaner
public static boolean useDirectBufferNoCleaner()
-
equals
public static boolean equals(byte[] bytes1, int startPos1, byte[] bytes2, int startPos2, int length)Compare twobytearrays for equality. For performance reasons no bounds checking on the parameters is performed.- Parameters:
bytes1- the first byte array.startPos1- the position (inclusive) to start comparing inbytes1.bytes2- the second byte array.startPos2- the position (inclusive) to start comparing inbytes2.length- the amount of bytes to compare. This is assumed to be validated as not going out of bounds by the caller.
-
isZero
public static boolean isZero(byte[] bytes, int startPos, int length)Determine if a subsection of an array is zero.- Parameters:
bytes- The byte array.startPos- The starting index (inclusive) inbytes.length- The amount of bytes to check for zero.- Returns:
falseifbytes[startPos:startsPos+length)contains a value other than zero.
-
equalsConstantTime
public static int equalsConstantTime(byte[] bytes1, int startPos1, byte[] bytes2, int startPos2, int length)Compare twobytearrays for equality without leaking timing information. For performance reasons no bounds checking on the parameters is performed.The
intreturn type is intentional and is designed to allow cascading of constant time operations:byte[] s1 = new {1, 2, 3}; byte[] s2 = new {1, 2, 3}; byte[] s3 = new {1, 2, 3}; byte[] s4 = new {4, 5, 6}; boolean equals = (equalsConstantTime(s1, 0, s2, 0, s1.length) & equalsConstantTime(s3, 0, s4, 0, s3.length)) != 0;- Parameters:
bytes1- the first byte array.startPos1- the position (inclusive) to start comparing inbytes1.bytes2- the second byte array.startPos2- the position (inclusive) to start comparing inbytes2.length- the amount of bytes to compare. This is assumed to be validated as not going out of bounds by the caller.- Returns:
0if not equal.1if equal.
-
hashCodeAscii
public static int hashCodeAscii(byte[] bytes, int startPos, int length)Calculate a hash code of a byte array assuming ASCII character encoding. The resulting hash code will be case insensitive.- Parameters:
bytes- The array which contains the data to hash.startPos- What index to start generating a hash code inbyteslength- The amount of bytes that should be accounted for in the computation.- Returns:
- The hash code of
bytesassuming ASCII character encoding. The resulting hash code will be case insensitive.
-
hashCodeAscii
public static int hashCodeAscii(CharSequence bytes)
Calculate a hash code of a byte array assuming ASCII character encoding. The resulting hash code will be case insensitive.This method assumes that
bytesis equivalent to abyte[]but just usingCharSequencefor storage. The upper most byte of eachcharfrombytesis ignored.- Parameters:
bytes- The array which contains the data to hash (assumed to be equivalent to abyte[]).- Returns:
- The hash code of
bytesassuming ASCII character encoding. The resulting hash code will be case insensitive.
-
newMpscQueue
public static <T> Queue<T> newMpscQueue()
Create a newQueuewhich is safe to use for multiple producers (different threads) and a single consumer (one thread!).- Returns:
- A MPSC queue which may be unbounded.
-
newMpscQueue
public static <T> Queue<T> newMpscQueue(int maxCapacity)
Create a newQueuewhich is safe to use for multiple producers (different threads) and a single consumer (one thread!).
-
newMpscQueue
public static <T> Queue<T> newMpscQueue(int chunkSize, int maxCapacity)
Create a newQueuewhich is safe to use for multiple producers (different threads) and a single consumer (one thread!). The queue will grow and shrink its capacity in units of the given chunk size.
-
newSpscQueue
public static <T> Queue<T> newSpscQueue()
Create a newQueuewhich is safe to use for single producer (one thread!) and a single consumer (one thread!).
-
newFixedMpscQueue
public static <T> Queue<T> newFixedMpscQueue(int capacity)
Create a newQueuewhich is safe to use for multiple producers (different threads) and a single consumer (one thread!) with the given fixescapacity.
-
getClassLoader
public static ClassLoader getClassLoader(Class<?> clazz)
Return theClassLoaderfor the givenClass.
-
getContextClassLoader
public static ClassLoader getContextClassLoader()
Return the contextClassLoaderfor the currentThread.
-
getSystemClassLoader
public static ClassLoader getSystemClassLoader()
Return the systemClassLoader.
-
unwrapUnsafeOrNull
public static Object unwrapUnsafeOrNull()
-
isJ9Jvm
public static boolean isJ9Jvm()
-
isIkvmDotNet
public static boolean isIkvmDotNet()
-
estimateMaxDirectMemory
public static long estimateMaxDirectMemory()
Compute an estimate of the maximum amount of direct memory available to this JVM.The computation is not cached, so you probably want to use
maxDirectMemory()instead.This will produce debug log output when called.
- Returns:
- The estimated max direct memory, in bytes.
-
normalizedArch
public static String normalizedArch()
-
normalizedOs
public static String normalizedOs()
-
createTempFile
public static File createTempFile(String prefix, String suffix, File directory) throws IOException
- Throws:
IOException
-
-