Package org.ghotibeaun.json.util
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 constructorResizableByteBuffer(byte[] initialByteArray)
Constructor that is initialized with a starting array of bytesResizableByteBuffer(int allocateSize)
Constructor -
Method Summary
Modifier and Type Method Description void
add(byte b)
Add a new byte to the buffervoid
add(byte[] bytes)
Append an array of bytes to the existing buffer.byte[]
array()
Retrieves the array for this buffervoid
clear()
Clears the bufferbyte
first()
Retrieves the first byte in the buffer, without updating the cursor positionstatic 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 itemint
getPosition()
returns the current cursor position in the bufferint
getRemaining()
Returns the count of the remaining items to be accessed relative the current cursor positionboolean
hasRemaining()
Returns whether there are any remaining items following the current cursor positionbyte
last()
Retrieves the last byte in the buffer, without updating the cursor positionbyte
peek()
Retrieves the next byte following the current cursor's position, without updating the cursor positionbyte
peek(int lookAhead)
Retrieves the byte at the specified location ahead of the cursor, without moving the cursor positionbyte
previous()
Retrieves the byte at the position behind the current cursorbyte
previous(int lookBehind)
Retrieves the byte at the specified number of bytes behind the current cursor, without moving the cursorvoid
rewind()
Sets the internal cursor back to the beginning of the bufferint
size()
Return the full size of the bufferByteBuffer
toByteBuffer()
Converts the buffer to aByteBuffer
String
toString()
-
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
-
fromStream
-
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
Converts the buffer to aByteBuffer
- 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
-