Package com.yahoo.io
Class GrowableByteBuffer
java.lang.Object
com.yahoo.io.GrowableByteBuffer
- All Implemented Interfaces:
Comparable<GrowableByteBuffer>
GrowableByteBuffer encapsulates a ByteBuffer and grows it as needed.
The implementation is safe and simple (and certainly a bit inefficient)
- when growing the buffer a new buffer
is allocated, the old contents are copied into the new buffer,
and the new buffer's position is set to the position of the old
buffer.
It is possible to set a growth factor. The default is 2.0, meaning that
the buffer will double its size when growing.
Note that NO methods are re-implemented (except growing the buffer,
of course), all are delegated to the encapsulated ByteBuffer.
This also includes toString(), hashCode(), equals() and compareTo().
No methods except getByteBuffer() expose the encapsulated
ByteBuffer, which is intentional.
- Author:
- Einar M R Rosenvinge
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final float -
Constructor Summary
ConstructorsConstructorDescriptionGrowableByteBuffer(int baseSize) GrowableByteBuffer(int baseSize, float growFactor) GrowableByteBuffer(ByteBuffer buffer) GrowableByteBuffer(ByteBuffer buffer, float growFactor) -
Method Summary
Modifier and TypeMethodDescriptionstatic GrowableByteBufferallocate(int capacity) static GrowableByteBufferallocate(int capacity, float growFactor) static GrowableByteBufferallocateDirect(int capacity) static GrowableByteBufferallocateDirect(int capacity, float growFactor) final byte[]array()final intfinal intcapacity()final voidclear()compact()intcompareTo(GrowableByteBuffer that) booleanfinal voidflip()byteget()get(byte[] dst) get(byte[] dst, int offset, int length) byteget(int index) chargetChar()chargetChar(int index) doubledoublegetDouble(int index) floatgetFloat()floatgetFloat(int index) floatintgetInt()intgetInt(int index) intReads a 30-bit positive integer from the buffer, which was written using 1, 2, or 4 bytes.intReads a 31-bit positive integer from the buffer, which was written using 1 or 4 bytes.longReads a 62-bit positive integer from the buffer, which was written using 2, 4, or 8 bytes.longgetLong()longgetLong(int index) static intgetSerializedSize1_2_4Bytes(int number) Computes the size used for storing the given integer using 1, 2 or 4 bytes.static intgetSerializedSize1_4Bytes(int number) Computes the size used for storing the given integer using 1 or 4 bytes.static intgetSerializedSize2_4_8Bytes(long number) Computes the size used for storing the given integer using 2, 4 or 8 bytes.shortgetShort()shortgetShort(int index) Reads a string from the buffer as a 1_4 encoded length in bytes followed by the utf8 bytesvoidgrow(int newSize) booleanhasArray()inthashCode()final booleanbooleanisDirect()final booleanfinal intlimit()final voidlimit(int newLimit) final voidmark()order()final intposition()final voidposition(int newPosition) put(byte b) put(byte[] src) put(byte[] src, int offset, int length) put(int index, byte b) put(GrowableByteBuffer src) put(ByteBuffer src) putChar(char value) putChar(int index, char value) putDouble(double value) putDouble(int index, double value) putFloat(float value) putFloat(int index, float value) putInt(int value) putInt(int index, int value) voidputInt1_2_4Bytes(int number) Writes a 30-bit positive integer to the buffer, using 1, 2, or 4 bytes.voidputInt1_2_4BytesAs4(int number) Writes a 30-bit positive integer to the buffer, using 4 bytes.voidputInt1_4Bytes(int number) Writes a 31-bit positive integer to the buffer, using 1 or 4 bytes.voidputInt1_4BytesAs4(int number) Writes a 31-bit positive integer to the buffer, using 4 bytes.voidputInt2_4_8Bytes(long number) Writes a 62-bit positive integer to the buffer, using 2, 4, or 8 bytes.voidputInt2_4_8BytesAs4(long number) Writes a 32 bit positive integer (or 31 bit unsigned) to the buffer, using 4 bytes.putLong(int index, long value) putLong(long value) putShort(int index, short value) putShort(short value) voidputUtf8String(String value) Writes this string to the buffer as a 1_4 encoded length in bytes followed by the utf8 bytesfinal intfinal voidreset()final voidrewind()final voidsetGrowFactor(float growFactor) slice()Behaves as ByteBuffer slicing, but the internal buffer will no longer be shared if one of the buffers is forced to grow.toString()static GrowableByteBufferwrap(byte[] array) static GrowableByteBufferwrap(byte[] array, float growFactor) static GrowableByteBufferwrap(byte[] array, int offset, int length) static GrowableByteBufferwrap(byte[] array, int offset, int length, float growFactor)
-
Field Details
-
DEFAULT_BASE_SIZE
public static final int DEFAULT_BASE_SIZE- See Also:
-
DEFAULT_GROW_FACTOR
public static final float DEFAULT_GROW_FACTOR- See Also:
-
-
Constructor Details
-
GrowableByteBuffer
public GrowableByteBuffer() -
GrowableByteBuffer
public GrowableByteBuffer(int baseSize, float growFactor) -
GrowableByteBuffer
public GrowableByteBuffer(int baseSize) -
GrowableByteBuffer
-
GrowableByteBuffer
-
-
Method Details
-
getGrowFactor
public float getGrowFactor() -
setGrowFactor
public final void setGrowFactor(float growFactor) -
getByteBuffer
-
grow
public void grow(int newSize) -
putInt2_4_8Bytes
public void putInt2_4_8Bytes(long number) Writes a 62-bit positive integer to the buffer, using 2, 4, or 8 bytes.- Parameters:
number- the integer to write
-
putInt2_4_8BytesAs4
public void putInt2_4_8BytesAs4(long number) Writes a 32 bit positive integer (or 31 bit unsigned) to the buffer, using 4 bytes.- Parameters:
number- the integer to write
-
getInt2_4_8Bytes
public long getInt2_4_8Bytes()Reads a 62-bit positive integer from the buffer, which was written using 2, 4, or 8 bytes.- Returns:
- the integer read
-
getSerializedSize2_4_8Bytes
public static int getSerializedSize2_4_8Bytes(long number) Computes the size used for storing the given integer using 2, 4 or 8 bytes.- Parameters:
number- the integer to check length of- Returns:
- the number of bytes used to store it; 2, 4 or 8
-
putInt1_2_4Bytes
public void putInt1_2_4Bytes(int number) Writes a 30-bit positive integer to the buffer, using 1, 2, or 4 bytes.- Parameters:
number- the integer to write
-
putInt1_2_4BytesAs4
public void putInt1_2_4BytesAs4(int number) Writes a 30-bit positive integer to the buffer, using 4 bytes.- Parameters:
number- the integer to write
-
getInt1_2_4Bytes
public int getInt1_2_4Bytes()Reads a 30-bit positive integer from the buffer, which was written using 1, 2, or 4 bytes.- Returns:
- the integer read
-
getSerializedSize1_2_4Bytes
public static int getSerializedSize1_2_4Bytes(int number) Computes the size used for storing the given integer using 1, 2 or 4 bytes.- Parameters:
number- the integer to check length of- Returns:
- the number of bytes used to store it; 1, 2 or 4
-
putInt1_4Bytes
public void putInt1_4Bytes(int number) Writes a 31-bit positive integer to the buffer, using 1 or 4 bytes.- Parameters:
number- the integer to write
-
putInt1_4BytesAs4
public void putInt1_4BytesAs4(int number) Writes a 31-bit positive integer to the buffer, using 4 bytes.- Parameters:
number- the integer to write
-
getInt1_4Bytes
public int getInt1_4Bytes()Reads a 31-bit positive integer from the buffer, which was written using 1 or 4 bytes.- Returns:
- the integer read
-
putUtf8String
Writes this string to the buffer as a 1_4 encoded length in bytes followed by the utf8 bytes -
getUtf8String
Reads a string from the buffer as a 1_4 encoded length in bytes followed by the utf8 bytes -
getSerializedSize1_4Bytes
public static int getSerializedSize1_4Bytes(int number) Computes the size used for storing the given integer using 1 or 4 bytes.- Parameters:
number- the integer to check length of- Returns:
- the number of bytes used to store it; 1 or 4
-
allocate
-
allocate
-
allocateDirect
-
allocateDirect
-
array
public final byte[] array() -
arrayOffset
public final int arrayOffset() -
asCharBuffer
-
asDoubleBuffer
-
asFloatBuffer
-
asIntBuffer
-
asLongBuffer
-
asReadOnlyBuffer
-
asShortBuffer
-
compact
-
compareTo
- Specified by:
compareToin interfaceComparable<GrowableByteBuffer>
-
duplicate
-
equals
-
get
public byte get() -
get
-
get
-
get
public byte get(int index) -
getChar
public char getChar() -
getChar
public char getChar(int index) -
getDouble
public double getDouble() -
getDouble
public double getDouble(int index) -
getFloat
public float getFloat() -
getFloat
public float getFloat(int index) -
getInt
public int getInt() -
getInt
public int getInt(int index) -
getLong
public long getLong() -
getLong
public long getLong(int index) -
getShort
public short getShort() -
getShort
public short getShort(int index) -
hasArray
public boolean hasArray() -
hashCode
public int hashCode() -
isDirect
public boolean isDirect() -
order
-
order
-
put
-
put
-
put
-
put
-
put
-
put
-
putChar
-
putChar
-
putDouble
-
putDouble
-
putFloat
-
putFloat
-
putInt
-
putInt
-
putLong
-
putLong
-
putShort
-
putShort
-
slice
Behaves as ByteBuffer slicing, but the internal buffer will no longer be shared if one of the buffers is forced to grow.- Returns:
- a new buffer with shared contents
- See Also:
-
toString
-
wrap
-
wrap
-
wrap
-
wrap
-
capacity
public final int capacity() -
clear
public final void clear() -
flip
public final void flip() -
hasRemaining
public final boolean hasRemaining() -
isReadOnly
public final boolean isReadOnly() -
limit
public final int limit() -
limit
public final void limit(int newLimit) -
mark
public final void mark() -
position
public final int position() -
position
public final void position(int newPosition) -
remaining
public final int remaining() -
reset
public final void reset() -
rewind
public final void rewind()
-