Package de.gsi.dataset.utils.serializer
Class FastByteBuffer
- java.lang.Object
-
- de.gsi.dataset.utils.serializer.FastByteBuffer
-
public class FastByteBuffer extends java.lang.ObjectFastByteBuffer 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
-
-
Field Summary
Fields Modifier and Type Field Description static longSIZE_OF_BOOLEANstatic longSIZE_OF_BYTEstatic longSIZE_OF_DOUBLEstatic longSIZE_OF_FLOATstatic longSIZE_OF_INTstatic longSIZE_OF_LONGstatic longSIZE_OF_SHORT
-
Constructor Summary
Constructors Constructor Description FastByteBuffer()construct new FastByteBufferFastByteBuffer(byte[] buffer, int size)construct new FastByteBufferFastByteBuffer(int size)construct new FastByteBuffer
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidcopyMemory(java.lang.Object srcBase, long srcOffset, java.lang.Object destBase, long destOffset, long bytes)byte[]elements()voidensureAdditionalCapacity(long capacity)voidensureCapacity(int capacity)voidforceCapacity(int length, int preserve)Forces FastByteBUffer to contain the given number of entries, preserving just a part of the array.booleangetBoolean()boolean[]getBooleanArray()bytegetByte()byte[]getByteArray()intgetCapacity()doublegetDouble()double[]getDoubleArray()floatgetFloat()float[]getFloatArray()intgetInt()int[]getIntArray()longgetLong()long[]getLongArray()longgetPosition()shortgetShort()short[]getShortArray()java.lang.StringgetString()java.lang.String[]getStringArray()voidputBoolean(boolean value)voidputBooleanArray(boolean[] values)voidputBooleanArray(boolean[] values, int nToCopy)voidputByte(byte value)voidputByteArray(byte[] values)voidputByteArray(byte[] values, int nToCopy)voidputDouble(double value)voidputDoubleArray(double[] values)voidputDoubleArray(double[] values, int nToCopy)voidputFloat(float value)voidputFloatArray(float[] values)voidputFloatArray(float[] values, int nToCopy)voidputInt(int value)voidputIntArray(int[] values)voidputIntArray(int[] values, int nToCopy)voidputLong(long value)voidputLongArray(long[] values)voidputLongArray(long[] values, int nToCopy)voidputShort(short value)voidputShortArray(short[] values)voidputShortArray(short[] values, int nToCopy)voidputString(java.lang.String string)voidputStringArray(java.lang.String[] values)voidputStringArray(java.lang.String[] values, int nToCopy)voidreset()voidtrim()Trims the internal buffer array so that the capacity is equal to the size.voidtrim(int requestedCapacity)Trims the internal buffer array if it is too large.booleanverifySize()static FastByteBufferwrap(byte[] byteArray)Wraps a given byte array into FastByteBufferstatic FastByteBufferwrap(byte[] byteArray, int length)Wraps a given byte array into FastByteBuffer
-
-
-
Field Detail
-
SIZE_OF_BOOLEAN
public static final long SIZE_OF_BOOLEAN
- See Also:
- Constant Field Values
-
SIZE_OF_BYTE
public static final long SIZE_OF_BYTE
- See Also:
- Constant Field Values
-
SIZE_OF_SHORT
public static final long SIZE_OF_SHORT
- See Also:
- Constant Field Values
-
SIZE_OF_INT
public static final long SIZE_OF_INT
- See Also:
- Constant Field Values
-
SIZE_OF_LONG
public static final long SIZE_OF_LONG
- See Also:
- Constant Field Values
-
SIZE_OF_FLOAT
public static final long SIZE_OF_FLOAT
- See Also:
- Constant Field Values
-
SIZE_OF_DOUBLE
public static final long SIZE_OF_DOUBLE
- See Also:
- Constant Field Values
-
-
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-usesize- 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 FastByteBufferNote 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 FastByteBufferNote 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 ton, this method does nothing. Otherwise, it trims the array length to the maximum betweenrequestedCapacityandgetCapacity().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()
-
-