Class ResizableByteBuffer

java.lang.Object
org.ghotibeaun.json.util.ResizableByteBuffer

public class ResizableByteBuffer
extends Object
Helper class that mimics similar behavior in a ByteBuffer.
Author:
Jim Earley (xml.jim@gmail.com)
  • Constructor Summary

    Constructors
    Constructor Description
    ResizableByteBuffer()
    Default constructor
    ResizableByteBuffer​(byte[] initialByteArray)
    Constructor that is initialized with a starting array of bytes
    ResizableByteBuffer​(int allocateSize)
    Constructor
  • Method Summary

    Modifier and Type Method Description
    void add​(byte b)
    Add a new byte to the buffer
    void add​(byte[] bytes)
    Append an array of bytes to the existing buffer.
    byte[] array()
    Retrieves the array for this buffer
    void clear()
    Clears the buffer
    byte first()
    Retrieves the first byte in the buffer, without updating the cursor position
    static ResizableByteBuffer fromStream​(InputStream inputStream)  
    static ResizableByteBuffer fromStream​(InputStream inputStream, int allocateSize)  
    byte get()
    Returns the next byte based on the internal cursor position, and moves the cursor to the next item
    int getPosition()
    returns the current cursor position in the buffer
    int getRemaining()
    Returns the count of the remaining items to be accessed relative the current cursor position
    boolean hasRemaining()
    Returns whether there are any remaining items following the current cursor position
    byte last()
    Retrieves the last byte in the buffer, without updating the cursor position
    byte peek()
    Retrieves the next byte following the current cursor's position, without updating the cursor position
    byte peek​(int lookAhead)
    Retrieves the byte at the specified location ahead of the cursor, without moving the cursor position
    byte previous()
    Retrieves the byte at the position behind the current cursor
    byte previous​(int lookBehind)
    Retrieves the byte at the specified number of bytes behind the current cursor, without moving the cursor
    void rewind()
    Sets the internal cursor back to the beginning of the buffer
    int size()
    Return the full size of the buffer
    ByteBuffer toByteBuffer()
    Converts the buffer to a ByteBuffer
    String toString()  

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ResizableByteBuffer

      public ResizableByteBuffer()
      Default constructor
    • ResizableByteBuffer

      public ResizableByteBuffer​(int allocateSize)
      Constructor
      Parameters:
      allocateSize - the size to allocate to the backing array
    • ResizableByteBuffer

      public ResizableByteBuffer​(byte[] initialByteArray)
      Constructor that is initialized with a starting array of bytes
      Parameters:
      initialByteArray - the initial array of bytes
  • Method Details

    • fromStream

      public static ResizableByteBuffer fromStream​(InputStream inputStream)
    • fromStream

      public static ResizableByteBuffer fromStream​(InputStream inputStream, int allocateSize)
    • add

      public void add​(byte b)
      Add a new byte to the buffer
      Parameters:
      b - the byte
    • add

      public void add​(byte[] bytes)
      Append an array of bytes to the existing buffer. The values will be inserted after the last item previously inserted into the buffer.
      Parameters:
      bytes - the array of bytes to append
    • rewind

      public void rewind()
      Sets the internal cursor back to the beginning of the buffer
    • get

      public byte get()
      Returns the next byte based on the internal cursor position, and moves the cursor to the next item
      Returns:
      the next byte
    • size

      public int size()
      Return the full size of the buffer
      Returns:
      the full size of the buffer;
    • getPosition

      public int getPosition()
      returns the current cursor position in the buffer
      Returns:
      the current cursor position in the buffer
    • getRemaining

      public int getRemaining()
      Returns the count of the remaining items to be accessed relative the current cursor position
      Returns:
      the count of the remaining items
    • hasRemaining

      public boolean hasRemaining()
      Returns whether there are any remaining items following the current cursor position
      Returns:
      true if there additional items following; false if the cursor is at the end.
    • peek

      public byte peek()
      Retrieves the next byte following the current cursor's position, without updating the cursor position
      Returns:
      the next byte
    • last

      public byte last()
      Retrieves the last byte in the buffer, without updating the cursor position
      Returns:
      the last byte in the buffer or a null byte (0x0) if the buffer is empty
    • first

      public byte first()
      Retrieves the first byte in the buffer, without updating the cursor position
      Returns:
      the first byte in the buffer or a null byte (0x0) if the buffer is empty
    • peek

      public byte peek​(int lookAhead)
      Retrieves the byte at the specified location ahead of the cursor, without moving the cursor position
      Parameters:
      lookAhead - the number of bytes ahead of the current cursor position to move to
      Returns:
      the byte value at the look ahead position
      Throws:
      IndexOutOfBoundsException - if the lookAhead value exceeds the size of the buffer
    • previous

      public byte previous()
      Retrieves the byte at the position behind the current cursor
      Returns:
      the byte value at position behind the cursor
      Throws:
      IndexOutOfBoundsException - if the buffer is empty, or the cursor is at the start position (0)
    • previous

      public byte previous​(int lookBehind)
      Retrieves the byte at the specified number of bytes behind the current cursor, without moving the cursor
      Parameters:
      lookBehind - the number of bytes behind the cursor position
      Returns:
      the byte value at the specified location behind the cursor
      Throws:
      IndexOutOfBoundsException - if the buffer is empty, or the cursor is at the start position (0)
    • toByteBuffer

      public ByteBuffer toByteBuffer()
      Converts the buffer to a ByteBuffer
      Returns:
      a new ByteBuffer instance
    • clear

      public void clear()
      Clears the buffer
    • array

      public byte[] array()
      Retrieves the array for this buffer
      Returns:
      an array of bytes
    • toString

      public String toString()
      Overrides:
      toString in class Object