Class FastByteBuffer
- java.lang.Object
-
- de.gsi.dataset.serializer.spi.FastByteBuffer
-
- All Implemented Interfaces:
IoBuffer,IoBufferHeader<IoBuffer>
public class FastByteBuffer extends java.lang.Object implements IoBuffer
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
-
-
Field Summary
Fields Modifier and Type Field Description static longSIZE_OF_BOOLEANstatic longSIZE_OF_BYTEstatic longSIZE_OF_CHARstatic 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 limit)construct new FastByteBufferFastByteBuffer(int size)construct new FastByteBuffer
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intcapacity()IoBufferclear()Clears this buffer.static voidcopyMemory(java.lang.Object srcBase, long srcOffset, java.lang.Object destBase, long destOffset, long nBytes)byte[]elements()protected static intencodeISO8859(java.lang.CharSequence sequence, byte[] bytes, long baseOffset, long offset, int length)IoBufferensureAdditionalCapacity(long capacity)IoBufferensureCapacity(long newCapacity)IoBufferforceCapacity(long length, long preserve)Forces FastByteBUffer to contain the given number of entries, preserving just a part of the array.booleangetBoolean()boolean[]getBooleanArray(boolean[] dst, long offset, int length)bytegetByte()byte[]getByteArray(byte[] dst, long offset, int length)chargetChar()char[]getCharArray(char[] dst, long offset, int length)doublegetDouble()double[]getDoubleArray(double[] dst, long offset, int length)floatgetFloat()float[]getFloatArray(float[] dst, long offset, int length)intgetInt()int[]getIntArray(int[] dst, long offset, int length)longgetLong()long[]getLongArray(long[] dst, long offset, int length)shortgetShort()short[]getShortArray(short[] dst, long offset, int length)java.lang.StringgetString()java.lang.String[]getStringArray(java.lang.String[] dst, long offset, int length)booleanhasRemaining()booleanisReadOnly()longlimit()IoBufferlimit(int newLimit)Sets this buffer's limit.java.util.concurrent.locks.ReadWriteLocklock()For efficiency/performance reasons the buffer implementation is not required to safe-guard each put/get method independently.longposition()IoBufferposition(long newPosition)Sets this buffer's position.IoBufferputBoolean(boolean value)IoBufferputBooleanArray(boolean[] values, long offset, int nToCopy)IoBufferputByte(byte value)IoBufferputByteArray(byte[] values, long offset, int nToCopy)IoBufferputChar(char value)IoBufferputCharArray(char[] values, long offset, int nToCopy)IoBufferputDouble(double value)IoBufferputDoubleArray(double[] values, long offset, int nToCopy)IoBufferputFloat(float value)IoBufferputFloatArray(float[] values, long offset, int nToCopy)IoBufferputInt(int value)IoBufferputIntArray(int[] values, long offset, int nToCopy)IoBufferputLong(long value)IoBufferputLongArray(long[] values, long offset, int nToCopy)IoBufferputShort(short value)IoBufferputShortArray(short[] values, long offset, int nToCopy)IoBufferputString(java.lang.String string)IoBufferputStringArray(java.lang.String[] values, long offset, int nToCopy)longremaining()IoBufferreset()resets the buffer read/write position to zeroIoBuffertrim()Trims the internal buffer array so that the capacity is equal to the size.IoBuffertrim(int requestedCapacity)Trims the internal buffer array if it is too large.static FastByteBufferwrap(byte[] byteArray)Wraps a given byte array into FastByteBufferstatic FastByteBufferwrap(byte[] byteArray, int length)Wraps a given byte array into FastByteBuffer-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface de.gsi.dataset.serializer.IoBuffer
getBooleanArray, getBooleanArray, getByteArray, getByteArray, getCharArray, getCharArray, getDoubleArray, getDoubleArray, getFloatArray, getFloatArray, getIntArray, getIntArray, getLongArray, getLongArray, getShortArray, getShortArray, getStringArray, getStringArray, putBooleanArray, putBooleanArray, putByteArray, putByteArray, putCharArray, putCharArray, putDoubleArray, putDoubleArray, putFloatArray, putFloatArray, putIntArray, putIntArray, putLongArray, putLongArray, putShortArray, putShortArray, putStringArray, putStringArray
-
-
-
-
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_CHAR
public static final long SIZE_OF_CHAR
- 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(byte[] buffer, int limit)construct new FastByteBuffer- Parameters:
buffer- buffer to initialise/re-uselimit- position until buffer is filled
-
FastByteBuffer
public FastByteBuffer(int size)
construct new FastByteBuffer- Parameters:
size- initial capacity of the buffer
-
-
Method Detail
-
capacity
public int capacity()
- Specified by:
capacityin interfaceIoBufferHeader<IoBuffer>- Returns:
- the capacity of this buffer
-
clear
public IoBuffer clear()
Description copied from interface:IoBufferHeaderClears this buffer. The position is set to zero amd the limit is set to the capacity.Invoke this method before using a sequence of channel-read or put operations to fill this buffer. For example:
buf.clear(); // Prepare buffer for reading in.read(buf); // Read data
This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.
- Specified by:
clearin interfaceIoBufferHeader<IoBuffer>- Returns:
- itself (fluent design)
-
elements
public byte[] elements()
-
ensureAdditionalCapacity
public IoBuffer ensureAdditionalCapacity(long capacity)
- Specified by:
ensureAdditionalCapacityin interfaceIoBufferHeader<IoBuffer>
-
ensureCapacity
public IoBuffer ensureCapacity(long newCapacity)
- Specified by:
ensureCapacityin interfaceIoBufferHeader<IoBuffer>
-
forceCapacity
public IoBuffer forceCapacity(long length, long preserve)
Forces FastByteBUffer to contain the given number of entries, preserving just a part of the array.- Specified by:
forceCapacityin interfaceIoBufferHeader<IoBuffer>- 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.- Returns:
- itself (fluent design)
-
getBoolean
public boolean getBoolean()
- Specified by:
getBooleanin interfaceIoBuffer
-
getBooleanArray
public boolean[] getBooleanArray(boolean[] dst, long offset, int length)- Specified by:
getBooleanArrayin interfaceIoBuffer
-
getByteArray
public byte[] getByteArray(byte[] dst, long offset, int length)- Specified by:
getByteArrayin interfaceIoBuffer
-
getCharArray
public char[] getCharArray(char[] dst, long offset, int length)- Specified by:
getCharArrayin interfaceIoBuffer
-
getDoubleArray
public double[] getDoubleArray(double[] dst, long offset, int length)- Specified by:
getDoubleArrayin interfaceIoBuffer
-
getFloatArray
public float[] getFloatArray(float[] dst, long offset, int length)- Specified by:
getFloatArrayin interfaceIoBuffer
-
getIntArray
public int[] getIntArray(int[] dst, long offset, int length)- Specified by:
getIntArrayin interfaceIoBuffer
-
getLongArray
public long[] getLongArray(long[] dst, long offset, int length)- Specified by:
getLongArrayin interfaceIoBuffer
-
getShortArray
public short[] getShortArray(short[] dst, long offset, int length)- Specified by:
getShortArrayin interfaceIoBuffer
-
getStringArray
public java.lang.String[] getStringArray(java.lang.String[] dst, long offset, int length)- Specified by:
getStringArrayin interfaceIoBuffer
-
hasRemaining
public boolean hasRemaining()
- Specified by:
hasRemainingin interfaceIoBufferHeader<IoBuffer>- Returns:
trueif, and only if, there is at least one element remaining in this buffer
-
isReadOnly
public boolean isReadOnly()
- Specified by:
isReadOnlyin interfaceIoBufferHeader<IoBuffer>- Returns:
trueif, and only if, this buffer is read-only
-
limit
public long limit()
- Specified by:
limitin interfaceIoBufferHeader<IoBuffer>- Returns:
- the limit of this buffer
-
limit
public IoBuffer limit(int newLimit)
Description copied from interface:IoBufferHeaderSets this buffer's limit. If the position is larger than the new limit then it is set to the new limit. If the mark is defined and larger than the new limit then it is discarded.- Specified by:
limitin interfaceIoBufferHeader<IoBuffer>- Parameters:
newLimit- the new limit value; must be non-negative and no larger than this buffer's capacity- Returns:
- itself (fluent design)
-
lock
public java.util.concurrent.locks.ReadWriteLock lock()
Description copied from interface:IoBufferHeaderFor efficiency/performance reasons the buffer implementation is not required to safe-guard each put/get method independently. Thus the user-code should acquire the given lock around a set of put/get appropriately.- Specified by:
lockin interfaceIoBufferHeader<IoBuffer>- Returns:
- the read-write lock
-
position
public long position()
- Specified by:
positionin interfaceIoBufferHeader<IoBuffer>- Returns:
- the position of this buffer
-
position
public IoBuffer position(long newPosition)
Description copied from interface:IoBufferHeaderSets this buffer's position. If the mark is defined and larger than the new position then it is discarded.- Specified by:
positionin interfaceIoBufferHeader<IoBuffer>- Parameters:
newPosition- the new position value; must be non-negative and no larger than the current limit- Returns:
- itself (fluent design)
-
putBoolean
public IoBuffer putBoolean(boolean value)
- Specified by:
putBooleanin interfaceIoBuffer
-
putBooleanArray
public IoBuffer putBooleanArray(boolean[] values, long offset, int nToCopy)
- Specified by:
putBooleanArrayin interfaceIoBuffer
-
putByteArray
public IoBuffer putByteArray(byte[] values, long offset, int nToCopy)
- Specified by:
putByteArrayin interfaceIoBuffer
-
putCharArray
public IoBuffer putCharArray(char[] values, long offset, int nToCopy)
- Specified by:
putCharArrayin interfaceIoBuffer
-
putDoubleArray
public IoBuffer putDoubleArray(double[] values, long offset, int nToCopy)
- Specified by:
putDoubleArrayin interfaceIoBuffer
-
putFloatArray
public IoBuffer putFloatArray(float[] values, long offset, int nToCopy)
- Specified by:
putFloatArrayin interfaceIoBuffer
-
putIntArray
public IoBuffer putIntArray(int[] values, long offset, int nToCopy)
- Specified by:
putIntArrayin interfaceIoBuffer
-
putLongArray
public IoBuffer putLongArray(long[] values, long offset, int nToCopy)
- Specified by:
putLongArrayin interfaceIoBuffer
-
putShortArray
public IoBuffer putShortArray(short[] values, long offset, int nToCopy)
- Specified by:
putShortArrayin interfaceIoBuffer
-
putString
public IoBuffer putString(java.lang.String string)
-
putStringArray
public IoBuffer putStringArray(java.lang.String[] values, long offset, int nToCopy)
- Specified by:
putStringArrayin interfaceIoBuffer
-
remaining
public long remaining()
- Specified by:
remainingin interfaceIoBufferHeader<IoBuffer>- Returns:
- the number of elements remaining in this buffer
-
reset
public IoBuffer reset()
Description copied from interface:IoBufferHeaderresets the buffer read/write position to zero- Specified by:
resetin interfaceIoBufferHeader<IoBuffer>- Returns:
- itself (fluent design)
-
trim
public IoBuffer trim()
Trims the internal buffer array so that the capacity is equal to the size.- Specified by:
trimin interfaceIoBufferHeader<IoBuffer>- Returns:
- itself (fluent design)
- See Also:
ArrayList.trimToSize()
-
trim
public IoBuffer 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 betweenrequestedCapacityandcapacity().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.
- Specified by:
trimin interfaceIoBufferHeader<IoBuffer>- Parameters:
requestedCapacity- the threshold for the trimming.- Returns:
- itself (fluent design)
-
copyMemory
public static void copyMemory(java.lang.Object srcBase, long srcOffset, java.lang.Object destBase, long destOffset, long nBytes)
-
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.
-
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.
-
encodeISO8859
protected static int encodeISO8859(java.lang.CharSequence sequence, byte[] bytes, long baseOffset, long offset, int length)
-
-