Package com.yahoo.io
Class GrowableByteBuffer
- java.lang.Object
-
- com.yahoo.io.GrowableByteBuffer
-
- All Implemented Interfaces:
java.lang.Comparable<GrowableByteBuffer>
public class GrowableByteBuffer extends java.lang.Object implements java.lang.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
Fields Modifier and Type Field Description static int
DEFAULT_BASE_SIZE
static float
DEFAULT_GROW_FACTOR
-
Constructor Summary
Constructors Constructor Description GrowableByteBuffer()
GrowableByteBuffer(int baseSize)
GrowableByteBuffer(int baseSize, float growFactor)
GrowableByteBuffer(java.nio.ByteBuffer buffer)
GrowableByteBuffer(java.nio.ByteBuffer buffer, float growFactor)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static GrowableByteBuffer
allocate(int capacity)
static GrowableByteBuffer
allocate(int capacity, float growFactor)
static GrowableByteBuffer
allocateDirect(int capacity)
static GrowableByteBuffer
allocateDirect(int capacity, float growFactor)
byte[]
array()
int
arrayOffset()
java.nio.CharBuffer
asCharBuffer()
java.nio.DoubleBuffer
asDoubleBuffer()
java.nio.FloatBuffer
asFloatBuffer()
java.nio.IntBuffer
asIntBuffer()
java.nio.LongBuffer
asLongBuffer()
GrowableByteBuffer
asReadOnlyBuffer()
java.nio.ShortBuffer
asShortBuffer()
int
capacity()
void
clear()
GrowableByteBuffer
compact()
int
compareTo(GrowableByteBuffer that)
GrowableByteBuffer
duplicate()
boolean
equals(java.lang.Object obj)
void
flip()
byte
get()
GrowableByteBuffer
get(byte[] dst)
GrowableByteBuffer
get(byte[] dst, int offset, int length)
byte
get(int index)
java.nio.ByteBuffer
getByteBuffer()
char
getChar()
char
getChar(int index)
double
getDouble()
double
getDouble(int index)
float
getFloat()
float
getFloat(int index)
float
getGrowFactor()
int
getInt()
int
getInt(int index)
int
getInt1_2_4Bytes()
Reads a 30-bit positive integer from the buffer, which was written using 1, 2, or 4 bytes.int
getInt1_4Bytes()
Reads a 31-bit positive integer from the buffer, which was written using 1 or 4 bytes.long
getInt2_4_8Bytes()
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)
java.lang.String
getUtf8String()
Reads a string from the buffer as a 1_4 encoded length in bytes followed by the utf8 bytesprotected void
grow(int newSize)
boolean
hasArray()
int
hashCode()
boolean
hasRemaining()
boolean
isDirect()
boolean
isReadOnly()
int
limit()
void
limit(int newLimit)
void
mark()
java.nio.ByteOrder
order()
GrowableByteBuffer
order(java.nio.ByteOrder bo)
int
position()
void
position(int newPosition)
GrowableByteBuffer
put(byte b)
GrowableByteBuffer
put(byte[] src)
GrowableByteBuffer
put(byte[] src, int offset, int length)
GrowableByteBuffer
put(int index, byte b)
GrowableByteBuffer
put(GrowableByteBuffer src)
GrowableByteBuffer
put(java.nio.ByteBuffer src)
GrowableByteBuffer
putChar(char value)
GrowableByteBuffer
putChar(int index, char value)
GrowableByteBuffer
putDouble(double value)
GrowableByteBuffer
putDouble(int index, double value)
GrowableByteBuffer
putFloat(float value)
GrowableByteBuffer
putFloat(int index, float value)
GrowableByteBuffer
putInt(int value)
GrowableByteBuffer
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.GrowableByteBuffer
putLong(int index, long value)
GrowableByteBuffer
putLong(long value)
GrowableByteBuffer
putShort(int index, short value)
GrowableByteBuffer
putShort(short value)
void
putUtf8String(java.lang.String value)
Writes this string to the buffer as a 1_4 encoded length in bytes followed by the utf8 bytesint
remaining()
void
reset()
void
rewind()
void
setGrowFactor(float growFactor)
GrowableByteBuffer
slice()
Behaves as ByteBuffer slicing, but the internal buffer will no longer be shared if one of the buffers is forced to grow.java.lang.String
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 Detail
-
DEFAULT_BASE_SIZE
public static final int DEFAULT_BASE_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_GROW_FACTOR
public static final float DEFAULT_GROW_FACTOR
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
GrowableByteBuffer
public GrowableByteBuffer()
-
GrowableByteBuffer
public GrowableByteBuffer(int baseSize, float growFactor)
-
GrowableByteBuffer
public GrowableByteBuffer(int baseSize)
-
GrowableByteBuffer
public GrowableByteBuffer(java.nio.ByteBuffer buffer)
-
GrowableByteBuffer
public GrowableByteBuffer(java.nio.ByteBuffer buffer, float growFactor)
-
-
Method Detail
-
getGrowFactor
public float getGrowFactor()
-
setGrowFactor
public void setGrowFactor(float growFactor)
-
getByteBuffer
public java.nio.ByteBuffer getByteBuffer()
-
grow
protected 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
public void putUtf8String(java.lang.String value)
Writes this string to the buffer as a 1_4 encoded length in bytes followed by the utf8 bytes
-
getUtf8String
public java.lang.String 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
public static GrowableByteBuffer allocate(int capacity)
-
allocate
public static GrowableByteBuffer allocate(int capacity, float growFactor)
-
allocateDirect
public static GrowableByteBuffer allocateDirect(int capacity)
-
allocateDirect
public static GrowableByteBuffer allocateDirect(int capacity, float growFactor)
-
array
public final byte[] array()
-
arrayOffset
public final int arrayOffset()
-
asCharBuffer
public java.nio.CharBuffer asCharBuffer()
-
asDoubleBuffer
public java.nio.DoubleBuffer asDoubleBuffer()
-
asFloatBuffer
public java.nio.FloatBuffer asFloatBuffer()
-
asIntBuffer
public java.nio.IntBuffer asIntBuffer()
-
asLongBuffer
public java.nio.LongBuffer asLongBuffer()
-
asReadOnlyBuffer
public GrowableByteBuffer asReadOnlyBuffer()
-
asShortBuffer
public java.nio.ShortBuffer asShortBuffer()
-
compact
public GrowableByteBuffer compact()
-
compareTo
public int compareTo(GrowableByteBuffer that)
- Specified by:
compareTo
in interfacejava.lang.Comparable<GrowableByteBuffer>
-
duplicate
public GrowableByteBuffer duplicate()
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
get
public byte get()
-
get
public GrowableByteBuffer get(byte[] dst)
-
get
public GrowableByteBuffer get(byte[] dst, int offset, int length)
-
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()
- Overrides:
hashCode
in classjava.lang.Object
-
isDirect
public boolean isDirect()
-
order
public java.nio.ByteOrder order()
-
order
public GrowableByteBuffer order(java.nio.ByteOrder bo)
-
put
public GrowableByteBuffer put(byte b)
-
put
public GrowableByteBuffer put(byte[] src)
-
put
public GrowableByteBuffer put(byte[] src, int offset, int length)
-
put
public GrowableByteBuffer put(java.nio.ByteBuffer src)
-
put
public GrowableByteBuffer put(GrowableByteBuffer src)
-
put
public GrowableByteBuffer put(int index, byte b)
-
putChar
public GrowableByteBuffer putChar(char value)
-
putChar
public GrowableByteBuffer putChar(int index, char value)
-
putDouble
public GrowableByteBuffer putDouble(double value)
-
putDouble
public GrowableByteBuffer putDouble(int index, double value)
-
putFloat
public GrowableByteBuffer putFloat(float value)
-
putFloat
public GrowableByteBuffer putFloat(int index, float value)
-
putInt
public GrowableByteBuffer putInt(int value)
-
putInt
public GrowableByteBuffer putInt(int index, int value)
-
putLong
public GrowableByteBuffer putLong(int index, long value)
-
putLong
public GrowableByteBuffer putLong(long value)
-
putShort
public GrowableByteBuffer putShort(int index, short value)
-
putShort
public GrowableByteBuffer putShort(short value)
-
slice
public GrowableByteBuffer 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:
ByteBuffer.slice()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
wrap
public static GrowableByteBuffer wrap(byte[] array)
-
wrap
public static GrowableByteBuffer wrap(byte[] array, float growFactor)
-
wrap
public static GrowableByteBuffer wrap(byte[] array, int offset, int length)
-
wrap
public static GrowableByteBuffer wrap(byte[] array, int offset, int length, float growFactor)
-
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()
-
-