Package com.pnfsoftware.jeb.util.collect
Class BytePipe
java.lang.Object
com.pnfsoftware.jeb.util.collect.BytePipe
A dynamic byte array (pipe). Bytes are written/appended at the end; bytes are read from the
beginning. Reading methods raise if the number of requested bytes is more than what is available
in the array.
This class is thread-safe for a single producer thread, single consumer thread. If there are multiple producers and/or multiple consumers, client code is responsible for making sure that writer threads coordinate among themselves, and that reader threads do the same as well. Ideally (and most likely), client code should have a single writer thread, and one reader thread (same or different as the writer's).
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
append
(byte b) Append a single byte to this dynamic array.void
append
(byte[] data) Append bytes to this array.void
append
(byte[] data, int offset, int length) Append bytes to this array.void
append
(int b) Append a single byte to this dynamic array.int
Get the number of bytes available for reading.int
blockUntilAvailable
(int n, long timeout) Wait until enough data is available for reading.int
capacity()
Get the current capacity of the array.int
get()
Retrieve a single byte.void
get
(byte[] data) Get a specified amount of bytes.void
get
(byte[] data, int offset, int size) Get a specified amount of bytes.byte[]
getAll()
Read all available bytes.int
limit()
Get the number of bytes in the pipe.byte
peek()
Peek a single byte within this array.void
peek
(byte[] data) Peek into this array.void
peek
(byte[] data, int where, int size) Peek into this array for a specified amount of data.int
position()
Get the current position in the pipe.int
readWait
(long timeout) Read a single byte of data.void
reset()
Reset this array: the number of bytes available for reading becomes 0.void
skip
(int n) Skip bytes.
-
Constructor Details
-
BytePipe
public BytePipe()Create an array whose initial capacity if 4,096 bytes. -
BytePipe
public BytePipe(int initialCapacity) Create an array with the provided initial capacity.- Parameters:
initialCapacity
- strictly positive number
-
-
Method Details
-
capacity
public int capacity()Get the current capacity of the array. Note: the capacity varies as bytes are written/read to the array.- Returns:
- the current capacity in bytes
-
available
public int available()Get the number of bytes available for reading.- Returns:
- the number of bytes available for reading
-
position
public int position()Get the current position in the pipe.- Returns:
-
limit
public int limit()Get the number of bytes in the pipe.- Returns:
-
reset
public void reset()Reset this array: the number of bytes available for reading becomes 0. -
append
public void append(byte[] data, int offset, int length) Append bytes to this array.- Parameters:
data
-offset
-length
-
-
append
public void append(byte[] data) Append bytes to this array.- Parameters:
data
-
-
append
public void append(byte b) Append a single byte to this dynamic array.- Parameters:
b
-
-
append
public void append(int b) Append a single byte to this dynamic array.- Parameters:
b
-
-
get
public void get(byte[] data, int offset, int size) Get a specified amount of bytes. Throws if not enough bytes are available.- Parameters:
data
-offset
-size
-
-
get
public void get(byte[] data) Get a specified amount of bytes. Throws if not enough bytes are available.- Parameters:
data
-
-
get
public int get()Retrieve a single byte. Throws if there is nothing available.- Returns:
-
getAll
public byte[] getAll()Read all available bytes.- Returns:
-
peek
public void peek(byte[] data, int where, int size) Peek into this array for a specified amount of data. Throws if not enough bytes are available.- Parameters:
data
-where
-size
-
-
peek
public void peek(byte[] data) Peek into this array. Throws if not enough bytes are available.- Parameters:
data
-
-
peek
public byte peek()Peek a single byte within this array. Throws if there a byte is not available.- Returns:
-
skip
public void skip(int n) Skip bytes. Throws if not enough bytes are available.- Parameters:
n
-
-
readWait
public int readWait(long timeout) Read a single byte of data. This method is blocking.- Parameters:
timeout
- in milliseconds; 0 means infinite- Returns:
- -1 on error (timeout elapsed and no data is available)
-
blockUntilAvailable
public int blockUntilAvailable(int n, long timeout) Wait until enough data is available for reading.- Parameters:
n
- size in bytestimeout
- in milliseconds (0 means infinite)- Returns:
- the amount of bytes in the pipe available for reading
-