Class FastByteBuffer


  • public class FastByteBuffer
    extends java.lang.Object
    FastByteBuffer implementation based on JVM 'Unsafe' Class. based on: https://mechanical-sympathy.blogspot.com/2012/07/native-cc-like-performance-for-java.html http://java-performance.info/various-methods-of-binary-serialization-in-java/
    Author:
    rstein
    • Constructor Detail

      • FastByteBuffer

        public FastByteBuffer()
        construct new FastByteBuffer
      • FastByteBuffer

        public FastByteBuffer​(int size)
        construct new FastByteBuffer
        Parameters:
        size - initial capacity of the buffer
      • FastByteBuffer

        public FastByteBuffer​(byte[] buffer,
                              int size)
        construct new FastByteBuffer
        Parameters:
        buffer - buffer to initialise/re-use
        size - position until buffer is filled
    • Method Detail

      • copyMemory

        public static void copyMemory​(java.lang.Object srcBase,
                                      long srcOffset,
                                      java.lang.Object destBase,
                                      long destOffset,
                                      long bytes)
      • wrap

        public static FastByteBuffer wrap​(byte[] byteArray,
                                          int length)
        Wraps a given byte array into FastByteBuffer

        Note it is guaranteed that the type of the array returned by elements() will be the same.

        Parameters:
        byteArray - an array to wrap.
        length - the length of the resulting array list.
        Returns:
        a new FastByteBuffer of the given size, wrapping the given array.
      • wrap

        public static FastByteBuffer wrap​(byte[] byteArray)
        Wraps a given byte array into FastByteBuffer

        Note it is guaranteed that the type of the array returned by elements() will be the same.

        Parameters:
        byteArray - an array to wrap.
        Returns:
        a new FastByteBuffer of the given size, wrapping the given array.
      • elements

        public byte[] elements()
      • getPosition

        public long getPosition()
      • getCapacity

        public int getCapacity()
      • reset

        public void reset()
      • verifySize

        public boolean verifySize()
      • forceCapacity

        public void forceCapacity​(int length,
                                  int preserve)
        Forces FastByteBUffer to contain the given number of entries, preserving just a part of the array.
        Parameters:
        length - the new minimum length for this array.
        preserve - the number of elements of the old buffer that shall be preserved in case a new allocation is necessary.
      • ensureCapacity

        public void ensureCapacity​(int capacity)
      • ensureAdditionalCapacity

        public void ensureAdditionalCapacity​(long capacity)
      • trim

        public void trim()
        Trims the internal buffer array so that the capacity is equal to the size.
        See Also:
        ArrayList.trimToSize()
      • trim

        public void trim​(int requestedCapacity)
        Trims the internal buffer array if it is too large. If the current array length is smaller than or equal to n, this method does nothing. Otherwise, it trims the array length to the maximum between requestedCapacity and getCapacity().

        This method is useful when reusing FastBuffers. Clearing a list leaves the array length untouched. If you are reusing a list many times, you can call this method with a typical size to avoid keeping around a very large array just because of a few large transient lists.

        Parameters:
        requestedCapacity - the threshold for the trimming.
      • putBoolean

        public void putBoolean​(boolean value)
      • getBoolean

        public boolean getBoolean()
      • putByte

        public void putByte​(byte value)
      • getByte

        public byte getByte()
      • putShort

        public void putShort​(short value)
      • getShort

        public short getShort()
      • putInt

        public void putInt​(int value)
      • getInt

        public int getInt()
      • putLong

        public void putLong​(long value)
      • getLong

        public long getLong()
      • putFloat

        public void putFloat​(float value)
      • getFloat

        public float getFloat()
      • putDouble

        public void putDouble​(double value)
      • getDouble

        public double getDouble()
      • putBooleanArray

        public void putBooleanArray​(boolean[] values)
      • putBooleanArray

        public void putBooleanArray​(boolean[] values,
                                    int nToCopy)
      • getBooleanArray

        public boolean[] getBooleanArray()
      • putByteArray

        public void putByteArray​(byte[] values)
      • putByteArray

        public void putByteArray​(byte[] values,
                                 int nToCopy)
      • getByteArray

        public byte[] getByteArray()
      • putShortArray

        public void putShortArray​(short[] values)
      • putShortArray

        public void putShortArray​(short[] values,
                                  int nToCopy)
      • getShortArray

        public short[] getShortArray()
      • putIntArray

        public void putIntArray​(int[] values)
      • putIntArray

        public void putIntArray​(int[] values,
                                int nToCopy)
      • getIntArray

        public int[] getIntArray()
      • putLongArray

        public void putLongArray​(long[] values)
      • putLongArray

        public void putLongArray​(long[] values,
                                 int nToCopy)
      • getLongArray

        public long[] getLongArray()
      • putFloatArray

        public void putFloatArray​(float[] values)
      • putFloatArray

        public void putFloatArray​(float[] values,
                                  int nToCopy)
      • getFloatArray

        public float[] getFloatArray()
      • putDoubleArray

        public void putDoubleArray​(double[] values)
      • putDoubleArray

        public void putDoubleArray​(double[] values,
                                   int nToCopy)
      • getDoubleArray

        public double[] getDoubleArray()
      • putString

        public void putString​(java.lang.String string)
      • getString

        public java.lang.String getString()
      • putStringArray

        public void putStringArray​(java.lang.String[] values)
      • putStringArray

        public void putStringArray​(java.lang.String[] values,
                                   int nToCopy)
      • getStringArray

        public java.lang.String[] getStringArray()