public abstract class ArrayConsumer extends Object implements ByteConsumer
ArrayConsumer
object is a consumer that consumes
bytes in to an internal array before processing. This consumes
all bytes read in to an internal array. Each read is met with an
invocation of the scan
method, which searches for
the terminal token within the read chunk. Once the terminal token
has been read the excess bytes are reset and the data can be
processed by the subclass implementation. The internal array is
expanded if the number of consumed bytes exceeds its capacity.Modifier and Type | Field and Description |
---|---|
protected byte[] |
array
This is the array that is used to contain the read bytes.
|
protected int |
chunk
This is the size of the chunk of bytes to read each time.
|
protected int |
count
This is the number of bytes that have been consumed so far.
|
protected boolean |
done
This determines whether the terminal token has been read.
|
Constructor and Description |
---|
ArrayConsumer()
Constructor for the
ArrayConsumer object. |
ArrayConsumer(int size)
Constructor for the
ArrayConsumer object. |
ArrayConsumer(int size,
int chunk)
Constructor for the
ArrayConsumer object. |
Modifier and Type | Method and Description |
---|---|
void |
consume(ByteCursor cursor)
This method is used to consume bytes from the provided cursor.
|
boolean |
isFinished()
When the terminal token is read from the cursor this will be
true.
|
protected abstract void |
process()
This method is invoked after the terminal token has been read.
|
protected void |
resize(int size)
This method is used to add an additional chunk size to the
internal array.
|
protected abstract int |
scan()
This method is used to scan for the terminal token.
|
protected byte[] array
protected int count
protected int chunk
protected boolean done
public ArrayConsumer()
ArrayConsumer
object. This is
used to create a consumer that will consume all bytes in to an
internal array until a terminal token has been read. If excess
bytes are read by this consumer they are reset in the cursor.public ArrayConsumer(int size)
ArrayConsumer
object. This is
used to create a consumer that will consume all bytes in to an
internal array until a terminal token has been read. If excess
bytes are read by this consumer they are reset in the cursor.size
- this is the initial array and chunk size to usepublic ArrayConsumer(int size, int chunk)
ArrayConsumer
object. This is
used to create a consumer that will consume all bytes in to an
internal array until a terminal token has been read. If excess
bytes are read by this consumer they are reset in the cursor.size
- this is the initial array size that is to be usedchunk
- this is the chunk size to read bytes aspublic void consume(ByteCursor cursor) throws IOException
ByteCursor
object. After each read the byte
array is scanned for the terminal token. When the terminal
token is found the bytes are processed by the implementation.consume
in interface ByteConsumer
cursor
- this is the cursor to consume the bytes fromIOException
protected void resize(int size) throws IOException
size
- this is the minimum size to expand the array toIOException
public boolean isFinished()
scan
method is used to determine the
terminal token. It is invoked after each read, when the scan
method returns a non-zero value then excess bytes are reset
and the consumer has finished.isFinished
in interface ByteConsumer
protected abstract void process() throws IOException
IOException
protected abstract int scan() throws IOException
IOException
Copyright © 2015. All rights reserved.