Class PlatformDependent

java.lang.Object
io.netty.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 the sun.misc.Unsafe object.

You can disable the use of sun.misc.Unsafe if you specify the system property io.netty.noUnsafe.

  • Field Details

    • BIG_ENDIAN_NATIVE_ORDER

      public static final boolean BIG_ENDIAN_NATIVE_ORDER
  • Method Details

    • byteArrayBaseOffset

      public static long byteArrayBaseOffset()
    • hasDirectBufferNoCleanerConstructor

      public static boolean hasDirectBufferNoCleanerConstructor()
    • allocateUninitializedArray

      public static byte[] allocateUninitializedArray(int size)
    • isAndroid

      public static boolean isAndroid()
      Returns true if and only if the current platform is Android
    • isWindows

      public static boolean isWindows()
      Return true if the JVM is running on Windows
    • isOsx

      public static boolean isOsx()
      Return true if the JVM is running on OSX / MacOS
    • maybeSuperUser

      public static boolean maybeSuperUser()
      Return true if 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.
    • isVirtualThread

      public static boolean isVirtualThread(Thread thread)
      Parameters:
      thread - The thread to be checked.
      Returns:
      true if this Thread is a virtual thread, false otherwise.
    • canEnableTcpNoDelayByDefault

      public static boolean canEnableTcpNoDelayByDefault()
      Returns true if and only if it is fine to enable TCP_NODELAY socket option by default.
    • hasUnsafe

      public static boolean hasUnsafe()
      Return true if sun.misc.Unsafe was found on the classpath and can be used for accelerated direct memory access.
    • getUnsafeUnavailabilityCause

      public static Throwable getUnsafeUnavailabilityCause()
      Return the reason (if any) why sun.misc.Unsafe was not available.
    • isUnaligned

      public static boolean isUnaligned()
      true if and only if the platform supports unaligned access.
      See Also:
    • directBufferPreferred

      public static boolean directBufferPreferred()
      Returns true if the platform has reliable low-level direct buffer access API and a user has not specified -Dio.netty.noPreferDirect option.
    • isExplicitNoPreferDirect

      public static boolean isExplicitNoPreferDirect()
      Returns true if user has specified -Dio.netty.noPreferDirect=true option.
    • canReliabilyFreeDirectBuffers

      public static boolean canReliabilyFreeDirectBuffers()
      Return true if the selected cleaner can free direct buffers in a controlled way. This guarantee only applies for buffers allocated via allocateDirect(int) and when using the clean method of the returned CleanableDirectBuffer.
    • 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:
    • 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.
    • newConcurrentHashMap

      @Deprecated public static <K,V> ConcurrentMap<K,V> newConcurrentHashMap()
      Deprecated.
      please use new ConcurrentHashMapinvalid input: '<'K, V>() directly.
      Creates a new fastest ConcurrentMap implementation for the current platform.
    • newLongCounter

      @Deprecated public static LongCounter newLongCounter()
      Deprecated.
      please use LongAdder instead.
      Creates a new fastest LongCounter implementation for the current platform.
    • newConcurrentHashMap

      @Deprecated public static <K,V> ConcurrentMap<K,V> newConcurrentHashMap(int initialCapacity)
      Deprecated.
      please use new ConcurrentHashMapinvalid input: '<'K, V>() directly.
      Creates a new fastest ConcurrentMap implementation for the current platform.
    • newConcurrentHashMap

      @Deprecated public static <K,V> ConcurrentMap<K,V> newConcurrentHashMap(int initialCapacity, float loadFactor)
      Deprecated.
      please use new ConcurrentHashMapinvalid input: '<'K, V>() directly.
      Creates a new fastest ConcurrentMap implementation for the current platform.
    • newConcurrentHashMap

      @Deprecated public static <K,V> ConcurrentMap<K,V> newConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel)
      Deprecated.
      please use new ConcurrentHashMapinvalid input: '<'K, V>() directly.
      Creates a new fastest ConcurrentMap implementation for the current platform.
    • newConcurrentHashMap

      @Deprecated public static <K,V> ConcurrentMap<K,V> newConcurrentHashMap(Map<? extends K, ? extends V> map)
      Deprecated.
      please use new ConcurrentHashMapinvalid input: '<'K, V>() directly.
      Creates a new fastest ConcurrentMap implementation for the current platform.
    • allocateDirect

      public static CleanableDirectBuffer allocateDirect(int capacity)
      Allocate a direct ByteBuffer of the given capacity, and return it alongside its deallocation mechanism.
      Parameters:
      capacity - The desired capacity of the direct byte buffer.
      Returns:
      The CleanableDirectBuffer instance that contain the buffer and its deallocation mechanism.
    • freeDirectBuffer

      @Deprecated public static void freeDirectBuffer(ByteBuffer buffer)
      Deprecated.
      Try to deallocate the specified direct ByteBuffer. 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)
    • hasVarHandle

      public static boolean hasVarHandle()
    • findVarHandleOfIntField

      public static VarHandle findVarHandleOfIntField(MethodHandles.Lookup lookup, Class<?> type, String fieldName)
    • intBeArrayView

      public static VarHandle intBeArrayView()
    • intLeArrayView

      public static VarHandle intLeArrayView()
    • longBeArrayView

      public static VarHandle longBeArrayView()
    • longLeArrayView

      public static VarHandle longLeArrayView()
    • shortBeArrayView

      public static VarHandle shortBeArrayView()
    • shortLeArrayView

      public static VarHandle shortLeArrayView()
    • longBeByteBufferView

      public static VarHandle longBeByteBufferView()
    • longLeByteBufferView

      public static VarHandle longLeByteBufferView()
    • intBeByteBufferView

      public static VarHandle intBeByteBufferView()
    • intLeByteBufferView

      public static VarHandle intLeByteBufferView()
    • shortBeByteBufferView

      public static VarHandle shortBeByteBufferView()
    • shortLeByteBufferView

      public static VarHandle shortLeByteBufferView()
    • getObject

      public static Object getObject(Object object, long fieldOffset)
    • getVolatileInt

      public static int getVolatileInt(Object object, long fieldOffset)
    • getInt

      public static int getInt(Object object, long fieldOffset)
    • putOrderedInt

      public static void putOrderedInt(Object object, long fieldOffset, int value)
    • getAndAddInt

      public static int getAndAddInt(Object object, long fieldOffset, int delta)
    • compareAndSwapInt

      public static boolean compareAndSwapInt(Object object, long fieldOffset, int expected, int value)
    • 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(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)
    • putObject

      public static void putObject(Object o, long offset, Object x)
    • 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(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(long address, long bytes, byte value)
    • allocateDirectNoCleaner

      public static ByteBuffer allocateDirectNoCleaner(int capacity)
      Allocate a new ByteBuffer with the given capacity. ByteBuffers allocated with this method MUST be deallocated via freeDirectNoCleaner(ByteBuffer).
    • allocateDirectBufferNoCleaner

      public static CleanableDirectBuffer allocateDirectBufferNoCleaner(int capacity)
      Allocate a new ByteBuffer with the given capacity, inside a CleanableDirectBuffer. The ByteBuffer MUST be deallocated via the CleanableDirectBuffer.clean() of the returned CleanableDirectBuffer object.
    • reallocateDirectNoCleaner

      public static ByteBuffer reallocateDirectNoCleaner(ByteBuffer buffer, int capacity)
      Reallocate a new ByteBuffer with the given capacity. ByteBuffers reallocated with this method MUST be deallocated via freeDirectNoCleaner(ByteBuffer).
    • reallocateDirectBufferNoCleaner

      public static CleanableDirectBuffer reallocateDirectBufferNoCleaner(CleanableDirectBuffer buffer, int capacity)
      Reallocate a new ByteBuffer with the given capacity. The ByteBuffer is given as wrapped in its associated CleanableDirectBuffer, and a new CleanableDirectBuffer instance will be returned. The ByteBuffers reallocated with this method MUST be deallocated via the CleanableDirectBuffer.clean() method on the returned object.
    • freeDirectNoCleaner

      public static void freeDirectNoCleaner(ByteBuffer buffer)
      This method MUST only be called for ByteBuffers that were allocated via allocateDirectNoCleaner(int).
    • hasAlignDirectByteBuffer

      public static boolean hasAlignDirectByteBuffer()
    • alignDirectBuffer

      public static ByteBuffer alignDirectBuffer(ByteBuffer buffer, int alignment)
    • align

      public static long align(long value, int alignment)
    • offsetSlice

      public static ByteBuffer offsetSlice(ByteBuffer buffer, int index, int length)
    • absolutePut

      public static ByteBuffer absolutePut(ByteBuffer dst, int dstOffset, byte[] src, int srcOffset, int length)
    • absolutePut

      public static ByteBuffer absolutePut(ByteBuffer dst, int dstOffset, ByteBuffer src, int srcOffset, int length)
    • useDirectBufferNoCleaner

      public static boolean useDirectBufferNoCleaner()
    • equals

      public static boolean equals(byte[] bytes1, int startPos1, byte[] bytes2, int startPos2, int length)
      Compare two byte arrays 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 in bytes1.
      bytes2 - the second byte array.
      startPos2 - the position (inclusive) to start comparing in bytes2.
      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) in bytes.
      length - The amount of bytes to check for zero.
      Returns:
      false if bytes[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 two byte arrays for equality without leaking timing information. For performance reasons no bounds checking on the parameters is performed.

      The int return 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) invalid input: '&'
                            equalsConstantTime(s3, 0, s4, 0, s3.length)) != 0;
      
      Parameters:
      bytes1 - the first byte array.
      startPos1 - the position (inclusive) to start comparing in bytes1.
      bytes2 - the second byte array.
      startPos2 - the position (inclusive) to start comparing in bytes2.
      length - the amount of bytes to compare. This is assumed to be validated as not going out of bounds by the caller.
      Returns:
      0 if not equal. 1 if 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 in bytes
      length - The amount of bytes that should be accounted for in the computation.
      Returns:
      The hash code of bytes assuming 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 bytes is equivalent to a byte[] but just using CharSequence for storage. The upper most byte of each char from bytes is ignored.

      Parameters:
      bytes - The array which contains the data to hash (assumed to be equivalent to a byte[]).
      Returns:
      The hash code of bytes assuming ASCII character encoding. The resulting hash code will be case insensitive.
    • newMpscQueue

      public static <T> Queue<T> newMpscQueue()
      Create a new Queue which 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 new Queue which 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 new Queue which 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 new Queue which 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 new Queue which is safe to use for multiple producers (different threads) and a single consumer (one thread!) with the given fixes capacity.
    • newFixedMpscUnpaddedQueue

      public static <T> Queue<T> newFixedMpscUnpaddedQueue(int capacity)
      Create a new un-padded Queue which is safe to use for multiple producers (different threads) and a single consumer (one thread!) with the given fixes capacity.
      This should be preferred to newFixedMpscQueue(int) when the queue is not to be heavily contended.
    • newFixedMpmcQueue

      public static <T> Queue<T> newFixedMpmcQueue(int capacity)
      Create a new Queue which is safe to use for multiple producers (different threads) and multiple consumers with the given fixes capacity.
    • getClassLoader

      public static ClassLoader getClassLoader(Class<?> clazz)
      Return the ClassLoader for the given Class.
    • getContextClassLoader

      public static ClassLoader getContextClassLoader()
      Return the context ClassLoader for the current Thread.
    • getSystemClassLoader

      public static ClassLoader getSystemClassLoader()
      Return the system ClassLoader.
    • newConcurrentDeque

      public static <C> Deque<C> newConcurrentDeque()
      Returns a new concurrent Deque.
    • threadLocalRandom

      @Deprecated public static Random threadLocalRandom()
      Deprecated.
      Use ThreadLocalRandom.current() instead.
      Return a Random which is not-threadsafe and so can only be used from the same thread.
    • isJ9Jvm

      public static boolean isJ9Jvm()
      Returns true if the running JVM is either IBM J9 or Eclipse OpenJ9, false otherwise.
    • isIkvmDotNet

      public static boolean isIkvmDotNet()
      Returns true if the running JVM is IKVM.NET, false otherwise.
    • 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()
    • normalizedLinuxClassifiers

      public static Set<String> normalizedLinuxClassifiers()
    • createTempFile

      public static File createTempFile(String prefix, String suffix, File directory) throws IOException
      Throws:
      IOException
    • isJfrEnabled

      public static boolean isJfrEnabled()
      Check if JFR events are supported on this platform.