Package com.yahoo.io

Class GrowableByteBuffer

java.lang.Object
com.yahoo.io.GrowableByteBuffer
All Implemented Interfaces:
Comparable<GrowableByteBuffer>

public class GrowableByteBuffer extends Object implements 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 Details

  • Constructor Details

    • GrowableByteBuffer

      public GrowableByteBuffer()
    • GrowableByteBuffer

      public GrowableByteBuffer(int baseSize, float growFactor)
    • GrowableByteBuffer

      public GrowableByteBuffer(int baseSize)
    • GrowableByteBuffer

      public GrowableByteBuffer(ByteBuffer buffer)
    • GrowableByteBuffer

      public GrowableByteBuffer(ByteBuffer buffer, float growFactor)
  • Method Details

    • getGrowFactor

      public float getGrowFactor()
    • setGrowFactor

      public void setGrowFactor(float growFactor)
    • getByteBuffer

      public ByteBuffer 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

      public void putUtf8String(String value)
      Writes this string to the buffer as a 1_4 encoded length in bytes followed by the utf8 bytes
    • getUtf8String

      public 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 CharBuffer asCharBuffer()
    • asDoubleBuffer

      public DoubleBuffer asDoubleBuffer()
    • asFloatBuffer

      public FloatBuffer asFloatBuffer()
    • asIntBuffer

      public IntBuffer asIntBuffer()
    • asLongBuffer

      public LongBuffer asLongBuffer()
    • asReadOnlyBuffer

      public GrowableByteBuffer asReadOnlyBuffer()
    • asShortBuffer

      public ShortBuffer asShortBuffer()
    • compact

      public GrowableByteBuffer compact()
    • compareTo

      public int compareTo(GrowableByteBuffer that)
      Specified by:
      compareTo in interface Comparable<GrowableByteBuffer>
    • duplicate

      public GrowableByteBuffer duplicate()
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class 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 class Object
    • isDirect

      public boolean isDirect()
    • order

      public ByteOrder order()
    • order

      public GrowableByteBuffer order(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(ByteBuffer src)
    • put

    • 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:
    • toString

      public String toString()
      Overrides:
      toString in class 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()