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
Modifier and TypeFieldDescriptionstatic final int
static final float
-
Constructor Summary
ConstructorDescriptionGrowableByteBuffer
(int baseSize) GrowableByteBuffer
(int baseSize, float growFactor) GrowableByteBuffer
(ByteBuffer buffer) GrowableByteBuffer
(ByteBuffer buffer, float growFactor) -
Method Summary
Modifier and TypeMethodDescriptionstatic GrowableByteBuffer
allocate
(int capacity) static GrowableByteBuffer
allocate
(int capacity, float growFactor) static GrowableByteBuffer
allocateDirect
(int capacity) static GrowableByteBuffer
allocateDirect
(int capacity, float growFactor) final byte[]
array()
final int
final int
capacity()
final void
clear()
compact()
int
compareTo
(GrowableByteBuffer that) boolean
final void
flip()
byte
get()
get
(byte[] dst) get
(byte[] dst, int offset, int length) byte
get
(int index) char
getChar()
char
getChar
(int index) double
double
getDouble
(int index) float
getFloat()
float
getFloat
(int index) float
int
getInt()
int
getInt
(int index) int
Reads a 30-bit positive integer from the buffer, which was written using 1, 2, or 4 bytes.int
Reads a 31-bit positive integer from the buffer, which was written using 1 or 4 bytes.long
Reads a 62-bit positive integer from the buffer, which was written using 2, 4, or 8 bytes.long
getLong()
long
getLong
(int index) static int
getSerializedSize1_2_4Bytes
(int number) Computes the size used for storing the given integer using 1, 2 or 4 bytes.static int
getSerializedSize1_4Bytes
(int number) Computes the size used for storing the given integer using 1 or 4 bytes.static int
getSerializedSize2_4_8Bytes
(long number) Computes the size used for storing the given integer using 2, 4 or 8 bytes.short
getShort()
short
getShort
(int index) Reads a string from the buffer as a 1_4 encoded length in bytes followed by the utf8 bytesvoid
grow
(int newSize) boolean
hasArray()
int
hashCode()
final boolean
boolean
isDirect()
final boolean
final int
limit()
final void
limit
(int newLimit) final void
mark()
order()
final int
position()
final void
position
(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) void
putInt1_2_4Bytes
(int number) Writes a 30-bit positive integer to the buffer, using 1, 2, or 4 bytes.void
putInt1_2_4BytesAs4
(int number) Writes a 30-bit positive integer to the buffer, using 4 bytes.void
putInt1_4Bytes
(int number) Writes a 31-bit positive integer to the buffer, using 1 or 4 bytes.void
putInt1_4BytesAs4
(int number) Writes a 31-bit positive integer to the buffer, using 4 bytes.void
putInt2_4_8Bytes
(long number) Writes a 62-bit positive integer to the buffer, using 2, 4, or 8 bytes.void
putInt2_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) void
putUtf8String
(String value) Writes this string to the buffer as a 1_4 encoded length in bytes followed by the utf8 bytesfinal int
final void
reset()
final void
rewind()
final void
setGrowFactor
(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 GrowableByteBuffer
wrap
(byte[] array) static GrowableByteBuffer
wrap
(byte[] array, float growFactor) static GrowableByteBuffer
wrap
(byte[] array, int offset, int length) static GrowableByteBuffer
wrap
(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:
compareTo
in 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()
-