org.simpleframework.http.message
Class ArrayConsumer

java.lang.Object
  extended by org.simpleframework.http.message.ArrayConsumer
All Implemented Interfaces:
ByteConsumer
Direct Known Subclasses:
SegmentConsumer

public abstract class ArrayConsumer
extends Object
implements ByteConsumer

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

Author:
Niall Gallagher

Field Summary
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 Summary
ArrayConsumer()
          Constructor for the ArrayConsumer object.
ArrayConsumer(int size)
          Constructor for the ArrayConsumer object.
ArrayConsumer(int size, int chunk)
          Constructor for the ArrayConsumer object.
 
Method Summary
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

array

protected byte[] array
This is the array that is used to contain the read bytes.


count

protected int count
This is the number of bytes that have been consumed so far.


chunk

protected int chunk
This is the size of the chunk of bytes to read each time.


done

protected boolean done
This determines whether the terminal token has been read.

Constructor Detail

ArrayConsumer

public ArrayConsumer()
Constructor for the 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.


ArrayConsumer

public ArrayConsumer(int size)
Constructor for the 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.

Parameters:
size - this is the initial array and chunk size to use

ArrayConsumer

public ArrayConsumer(int size,
                     int chunk)
Constructor for the 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.

Parameters:
size - this is the initial array size that is to be used
chunk - this is the chunk size to read bytes as
Method Detail

consume

public void consume(ByteCursor cursor)
             throws IOException
This method is used to consume bytes from the provided cursor. Each read performed is done in a specific chunk size to ensure that a sufficiently large or small amount of data is read from the 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.

Specified by:
consume in interface ByteConsumer
Parameters:
cursor - this is the cursor to consume the bytes from
Throws:
IOException

resize

protected void resize(int size)
               throws IOException
This method is used to add an additional chunk size to the internal array. Resizing of the internal array is required as the consumed bytes may exceed the initial size of the array. In such a scenario the array is expanded the chunk size.

Parameters:
size - this is the minimum size to expand the array to
Throws:
IOException

isFinished

public boolean isFinished()
When the terminal token is read from the cursor this will be true. The 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.

Specified by:
isFinished in interface ByteConsumer
Returns:
this returns true when the terminal token is read

process

protected abstract void process()
                         throws IOException
This method is invoked after the terminal token has been read. It is used to process the consumed data and is typically used to parse the input such that it can be used by the subclass for some useful purpose. This is called only once by the consumer.

Throws:
IOException

scan

protected abstract int scan()
                     throws IOException
This method is used to scan for the terminal token. It searches for the token and returns the number of bytes in the buffer after the terminal token. Returning the excess bytes allows the consumer to reset the bytes within the consumer object.

Returns:
this returns the number of excess bytes consumed
Throws:
IOException


Copyright © 2014. All Rights Reserved.