public class

BytePipe

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.util.collect.BytePipe

Class Overview

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).

Summary

Public Constructors
BytePipe()
Create an array whose initial capacity if 4,096 bytes.
BytePipe(int initialCapacity)
Create an array with the provided initial capacity.
Public Methods
synchronized void append(byte[] data)
Append bytes to this array.
synchronized void append(byte[] data, int offset, int length)
Append bytes to this array.
synchronized void append(int b)
Append a single byte to this dynamic array.
synchronized void append(byte b)
Append a single byte to this dynamic array.
int available()
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.
synchronized int get()
Retrieve a single byte.
synchronized void get(byte[] data)
Get a specified amount of bytes.
synchronized void get(byte[] data, int offset, int size)
Get a specified amount of bytes.
synchronized byte[] getAll()
Read all available bytes.
int limit()
Get the number of bytes in the pipe.
synchronized byte peek()
Peek a single byte within this array.
synchronized void peek(byte[] data, int where, int size)
Peek into this array for a specified amount of data.
synchronized void peek(byte[] data)
Peek into this array.
int position()
Get the current position in the pipe.
int readWait(long timeout)
Read a single byte of data.
synchronized void reset()
Reset this array: the number of bytes available for reading becomes 0.
synchronized void skip(int n)
Skip bytes.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public BytePipe ()

Create an array whose initial capacity if 4,096 bytes.

public BytePipe (int initialCapacity)

Create an array with the provided initial capacity.

Parameters
initialCapacity strictly positive number

Public Methods

public synchronized void append (byte[] data)

Append bytes to this array.

public synchronized void append (byte[] data, int offset, int length)

Append bytes to this array.

public synchronized void append (int b)

Append a single byte to this dynamic array.

public synchronized void append (byte b)

Append a single byte to this dynamic array.

public int available ()

Get the number of bytes available for reading.

Returns
  • the number of bytes available for reading

public int blockUntilAvailable (int n, long timeout)

Wait until enough data is available for reading.

Parameters
n size in bytes
timeout in milliseconds (0 means infinite)
Returns
  • the amount of bytes in the pipe available for reading

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

public synchronized int get ()

Retrieve a single byte. Throws if there is nothing available.

public synchronized void get (byte[] data)

Get a specified amount of bytes. Throws if not enough bytes are available.

public synchronized void get (byte[] data, int offset, int size)

Get a specified amount of bytes. Throws if not enough bytes are available.

public synchronized byte[] getAll ()

Read all available bytes.

public int limit ()

Get the number of bytes in the pipe.

public synchronized byte peek ()

Peek a single byte within this array. Throws if there a byte is not available.

public synchronized 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.

public synchronized void peek (byte[] data)

Peek into this array. Throws if not enough bytes are available.

public int position ()

Get the current position in the pipe.

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)

public synchronized void reset ()

Reset this array: the number of bytes available for reading becomes 0.

public synchronized void skip (int n)

Skip bytes. Throws if not enough bytes are available.