Class FastByteBuffer
- java.lang.Object
-
- de.gsi.serializer.spi.FastByteBuffer
-
- All Implemented Interfaces:
IoBuffer,IoBufferHeader
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 java.lang.StringINVALID_UTF_8static intSIZE_OF_BOOLEANstatic intSIZE_OF_BYTEstatic intSIZE_OF_CHARstatic intSIZE_OF_DOUBLEstatic intSIZE_OF_FLOATstatic intSIZE_OF_INTstatic intSIZE_OF_LONGstatic intSIZE_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()voidclear()Clears this buffer.byte[]elements()voidensureAdditionalCapacity(int capacity)voidensureCapacity(int newCapacity)voidflip()Flips this buffer.voidforceCapacity(int length, int preserve)Forces FastByteBuffer to contain the given number of entries, preserving just a part of the array.booleangetBoolean()booleangetBoolean(int position)boolean[]getBooleanArray(boolean[] dst, int length)bytegetByte()bytegetByte(int position)byte[]getByteArray(byte[] dst, int length)chargetChar()chargetChar(int position)char[]getCharArray(char[] dst, int length)doublegetDouble()doublegetDouble(int position)double[]getDoubleArray(double[] dst, int length)floatgetFloat()floatgetFloat(int position)float[]getFloatArray(float[] dst, int length)intgetInt()intgetInt(int position)int[]getIntArray(int[] dst, int length)longgetLong()longgetLong(int position)long[]getLongArray(long[] dst, int length)shortgetShort()shortgetShort(int position)short[]getShortArray(short[] dst, int length)java.lang.StringgetString()java.lang.StringgetString(int position)java.lang.String[]getStringArray(java.lang.String[] dst, int length)java.lang.StringgetStringISO8859()booleanhasRemaining()booleanisEnforceSimpleStringEncoding()booleanisReadOnly()intlimit()voidlimit(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.intposition()voidposition(int newPosition)Sets this buffer's position.voidputBoolean(boolean value)voidputBoolean(int position, boolean value)voidputBooleanArray(boolean[] values, int n)voidputByte(byte value)voidputByte(int position, byte value)voidputByteArray(byte[] values, int n)voidputChar(char value)voidputChar(int position, char value)voidputCharArray(char[] values, int n)voidputDouble(double value)voidputDouble(int position, double value)voidputDoubleArray(double[] values, int n)voidputFloat(float value)voidputFloat(int position, float value)voidputFloatArray(float[] values, int n)voidputInt(int value)voidputInt(int position, int value)voidputIntArray(int[] values, int n)voidputLong(int position, long value)voidputLong(long value)voidputLongArray(long[] values, int n)voidputShort(int position, short value)voidputShort(short value)voidputShortArray(short[] values, int n)voidputString(int position, java.lang.String value)voidputString(java.lang.String string)voidputStringArray(java.lang.String[] values, int n)voidputStringISO8859(java.lang.String string)intremaining()voidreset()resets the buffer read/write position to zerovoidsetEnforceSimpleStringEncoding(boolean state)java.lang.StringtoString()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.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, wait, wait, wait
-
Methods inherited from interface de.gsi.serializer.IoBuffer
getBooleanArray, getBooleanArray, getByteArray, getByteArray, getCharArray, getCharArray, getDoubleArray, getDoubleArray, getFloatArray, getFloatArray, getIntArray, getIntArray, getLongArray, getLongArray, getShortArray, getShortArray, getStringArray, getStringArray
-
-
-
-
Field Detail
-
SIZE_OF_BOOLEAN
public static final int SIZE_OF_BOOLEAN
- See Also:
- Constant Field Values
-
SIZE_OF_BYTE
public static final int SIZE_OF_BYTE
- See Also:
- Constant Field Values
-
SIZE_OF_SHORT
public static final int SIZE_OF_SHORT
- See Also:
- Constant Field Values
-
SIZE_OF_CHAR
public static final int SIZE_OF_CHAR
- See Also:
- Constant Field Values
-
SIZE_OF_INT
public static final int SIZE_OF_INT
- See Also:
- Constant Field Values
-
SIZE_OF_LONG
public static final int SIZE_OF_LONG
- See Also:
- Constant Field Values
-
SIZE_OF_FLOAT
public static final int SIZE_OF_FLOAT
- See Also:
- Constant Field Values
-
SIZE_OF_DOUBLE
public static final int SIZE_OF_DOUBLE
- See Also:
- Constant Field Values
-
INVALID_UTF_8
public static final java.lang.String INVALID_UTF_8
- 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- Returns:
- the capacity of this buffer
-
clear
public void 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
-
elements
public byte[] elements()
-
ensureAdditionalCapacity
public void ensureAdditionalCapacity(int capacity)
- Specified by:
ensureAdditionalCapacityin interfaceIoBufferHeader
-
ensureCapacity
public void ensureCapacity(int newCapacity)
- Specified by:
ensureCapacityin interfaceIoBufferHeader
-
flip
public void flip()
Description copied from interface:IoBufferHeaderFlips this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded.After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations. For example:
buf.put(magic); // Prepend header in.read(buf); // Read data into rest of buffer buf.flip(); // Flip buffer out.write(buf); // Write header + data to channel
- Specified by:
flipin interfaceIoBufferHeader
-
forceCapacity
public void forceCapacity(int length, int preserve)Forces FastByteBuffer to contain the given number of entries, preserving just a part of the array.- Specified by:
forceCapacityin interfaceIoBufferHeader- 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.
-
getBoolean
public boolean getBoolean()
- Specified by:
getBooleanin interfaceIoBuffer
-
getBooleanArray
public boolean[] getBooleanArray(boolean[] dst, int length)- Specified by:
getBooleanArrayin interfaceIoBuffer
-
getByteArray
public byte[] getByteArray(byte[] dst, int length)- Specified by:
getByteArrayin interfaceIoBuffer
-
getCharArray
public char[] getCharArray(char[] dst, int length)- Specified by:
getCharArrayin interfaceIoBuffer
-
getDoubleArray
public double[] getDoubleArray(double[] dst, int length)- Specified by:
getDoubleArrayin interfaceIoBuffer
-
getFloatArray
public float[] getFloatArray(float[] dst, int length)- Specified by:
getFloatArrayin interfaceIoBuffer
-
getIntArray
public int[] getIntArray(int[] dst, int length)- Specified by:
getIntArrayin interfaceIoBuffer
-
getLongArray
public long[] getLongArray(long[] dst, int length)- Specified by:
getLongArrayin interfaceIoBuffer
-
getShortArray
public short[] getShortArray(short[] dst, int length)- Specified by:
getShortArrayin interfaceIoBuffer
-
getStringArray
public java.lang.String[] getStringArray(java.lang.String[] dst, int length)- Specified by:
getStringArrayin interfaceIoBuffer
-
getStringISO8859
public java.lang.String getStringISO8859()
- Specified by:
getStringISO8859in interfaceIoBuffer
-
hasRemaining
public boolean hasRemaining()
- Specified by:
hasRemainingin interfaceIoBufferHeader- Returns:
trueif, and only if, there is at least one element remaining in this buffer
-
isEnforceSimpleStringEncoding
public boolean isEnforceSimpleStringEncoding()
- Specified by:
isEnforceSimpleStringEncodingin interfaceIoBuffer- Returns:
truethe ISO-8859-1 character encoding is being enforced for data fields (better performance), otherwise UTF-8 is being used (more generic encoding)
-
setEnforceSimpleStringEncoding
public void setEnforceSimpleStringEncoding(boolean state)
- Specified by:
setEnforceSimpleStringEncodingin interfaceIoBuffer- Parameters:
state-truethe ISO-8859-1 character encoding is being enforced for data fields (better performance), otherwise UTF-8 is being used (more generic encoding)
-
isReadOnly
public boolean isReadOnly()
- Specified by:
isReadOnlyin interfaceIoBufferHeader- Returns:
trueif, and only if, this buffer is read-only
-
limit
public int limit()
- Specified by:
limitin interfaceIoBufferHeader- Returns:
- the limit of this buffer
-
limit
public void 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- Parameters:
newLimit- the new limit value; must be non-negative and no larger than this buffer's capacity
-
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- Returns:
- the read-write lock
-
position
public int position()
- Specified by:
positionin interfaceIoBufferHeader- Returns:
- the position of this buffer
-
position
public void position(int 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- Parameters:
newPosition- the new position value; must be non-negative and no larger than the current limit
-
putBoolean
public void putBoolean(boolean value)
- Specified by:
putBooleanin interfaceIoBuffer
-
putBooleanArray
public void putBooleanArray(boolean[] values, int n)- Specified by:
putBooleanArrayin interfaceIoBuffer
-
putByteArray
public void putByteArray(byte[] values, int n)- Specified by:
putByteArrayin interfaceIoBuffer
-
putCharArray
public void putCharArray(char[] values, int n)- Specified by:
putCharArrayin interfaceIoBuffer
-
putDoubleArray
public void putDoubleArray(double[] values, int n)- Specified by:
putDoubleArrayin interfaceIoBuffer
-
putFloatArray
public void putFloatArray(float[] values, int n)- Specified by:
putFloatArrayin interfaceIoBuffer
-
putIntArray
public void putIntArray(int[] values, int n)- Specified by:
putIntArrayin interfaceIoBuffer
-
putLongArray
public void putLongArray(long[] values, int n)- Specified by:
putLongArrayin interfaceIoBuffer
-
putShortArray
public void putShortArray(short[] values, int n)- Specified by:
putShortArrayin interfaceIoBuffer
-
putString
public void putString(java.lang.String string)
-
putStringArray
public void putStringArray(java.lang.String[] values, int n)- Specified by:
putStringArrayin interfaceIoBuffer
-
putStringISO8859
public void putStringISO8859(java.lang.String string)
- Specified by:
putStringISO8859in interfaceIoBuffer
-
remaining
public int remaining()
- Specified by:
remainingin interfaceIoBufferHeader- Returns:
- the number of elements remaining in this buffer
-
reset
public void reset()
Description copied from interface:IoBufferHeaderresets the buffer read/write position to zero- Specified by:
resetin interfaceIoBufferHeader
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
trim
public void trim()
Trims the internal buffer array so that the capacity is equal to the size.- Specified by:
trimin interfaceIoBufferHeader- 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 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- Parameters:
requestedCapacity- the threshold for the trimming.
-
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.
-
getBoolean
public boolean getBoolean(int position)
- Specified by:
getBooleanin interfaceIoBuffer
-
getString
public java.lang.String getString(int position)
-
putBoolean
public void putBoolean(int position, boolean value)- Specified by:
putBooleanin interfaceIoBuffer
-
putShort
public void putShort(int position, short value)
-
putFloat
public void putFloat(int position, float value)
-
putDouble
public void putDouble(int position, double value)
-
-