Package com.yahoo.io

Class 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
    • 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,
                                                  float growFactor)
      • 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()
      • asShortBuffer

        public java.nio.ShortBuffer asShortBuffer()
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • get

        public byte 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()
        Overrides:
        hashCode in class java.lang.Object
      • isDirect

        public boolean isDirect()
      • order

        public java.nio.ByteOrder order()
      • 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 class java.lang.Object
      • 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()