net.sf.mmm.util.io.impl
Class DetectorStreamBufferImpl

java.lang.Object
  extended by net.sf.mmm.util.io.impl.DetectorStreamBufferImpl
All Implemented Interfaces:
ByteBuffer, ByteIterator, ByteProvider, ComposedByteBuffer, DetectorStreamBuffer

public class DetectorStreamBufferImpl
extends Object
implements DetectorStreamBuffer

This is the implementation of the DetectorStreamBuffer interface.
It is based on the idea that each DetectorStreamProcessor in the chain has its own DetectorStreamBuffer instance. Therefore it holds the according DetectorStreamProcessor building a pair of buffer+processor. Further it holds an instance of the predecessor and thereby represents the chain itself.

Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)

Nested Class Summary
protected  class DetectorStreamBufferImpl.CurrentByteArray
          This inner class is a view on the current ByteArray.
protected static class DetectorStreamBufferImpl.SeekMode
          Enum with available modes for a seek.
 
Field Summary
private  LinkedList<ByteArray> arrayQueue
          The Queue of available DetectorStreamBufferImpl.CurrentByteArrays that have NOT yet been processed.
private  ByteArrayPool byteArrayPool
          The ByteArrayPool.
private  DetectorStreamBufferImpl chainSuccessor
          The successor in the chain or null if this is the last.
private  byte[] currentArray
          The current DetectorStreamBufferImpl.CurrentByteArray to work on.
private  int currentArrayIndex
          The index in currentArray.
private  int currentArrayMax
          The maximum index in currentArray.
private  int currentArrayMin
          The start-index in currentArray.
private  ByteArray currentArrayView
           
private  ByteArray currentByteArray
           
private  DetectorStreamProcessor processor
          The actual processor served by this buffer.
private  long seekCount
           
private  DetectorStreamBufferImpl.SeekMode seekMode
           
private  long streamPosition
           
 
Constructor Summary
DetectorStreamBufferImpl(DetectorStreamProcessor processor, DetectorStreamBufferImpl successor, ByteArrayPool byteArrayPool)
          The constructor.
 
Method Summary
protected  void append(ByteArray nextArray)
          This method queues the given DetectorStreamBufferImpl.CurrentByteArray at the end of this buffer.
 int fill(byte[] buffer, int offset, int length)
          This method fills the given buffer starting at offset with the next bytes from this ComposedByteBuffer.
 ByteArray getByteArray(int index)
          This method gets the ByteArray at the given index.
 int getByteArrayCount()
          This method gets the number of ByteArrays currently available via ComposedByteBuffer.getByteArray(int).
 int getBytesAvailable()
          This method gets the number of bytes available.
 long getStreamPosition()
          This method gets the absolute position of the current pointer in the logical stream (the data provided by this interface that origins from the previous member of the chain).
 boolean hasNext()
          This method determines if there is a next byte available.
 void insert(byte... data)
          This method inserts the given bytes at the current position into the stream.
 void insert(ByteArray data)
          This method inserts the given data at the current position into the stream.
 byte next()
          This method gets the current byte in the iteration.
private  boolean nextArray()
          This method switches over to the next internal byte-array.
 byte peek()
          This method gets the current byte in the iteration.
 void process(Map<String,Object> metadata, boolean eos)
           
protected  void release(ByteArray byteArray)
          This method is called when a DetectorStreamBufferImpl.CurrentByteArray is wiped out of the chain.
 void remove(long byteCount)
          This method removes the number of bytes given by byteCount from the stream starting at the current position.
protected  void seek(long byteCount, DetectorStreamBufferImpl.SeekMode mode)
          This method removes or skips the given number of bytes.
 long skip()
          This method skips all bytes available in this buffer.
 long skip(long byteCount)
          This method skips the number of bytes given by byteCount in the stream starting at the current position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

processor

private DetectorStreamProcessor processor
The actual processor served by this buffer.


chainSuccessor

private DetectorStreamBufferImpl chainSuccessor
The successor in the chain or null if this is the last.


streamPosition

private long streamPosition
See Also:
getStreamPosition()

seekCount

private long seekCount
See Also:
seek(long, SeekMode)

seekMode

private DetectorStreamBufferImpl.SeekMode seekMode
See Also:
seek(long, SeekMode)

currentByteArray

private ByteArray currentByteArray
See Also:
getByteArray(int)

currentArray

private byte[] currentArray
The current DetectorStreamBufferImpl.CurrentByteArray to work on.


currentArrayMin

private int currentArrayMin
The start-index in currentArray.


currentArrayIndex

private int currentArrayIndex
The index in currentArray.


currentArrayMax

private int currentArrayMax
The maximum index in currentArray.


arrayQueue

private final LinkedList<ByteArray> arrayQueue
The Queue of available DetectorStreamBufferImpl.CurrentByteArrays that have NOT yet been processed.


currentArrayView

private final ByteArray currentArrayView
See Also:
getByteArray(int)

byteArrayPool

private ByteArrayPool byteArrayPool
The ByteArrayPool.

Constructor Detail

DetectorStreamBufferImpl

public DetectorStreamBufferImpl(DetectorStreamProcessor processor,
                                DetectorStreamBufferImpl successor,
                                ByteArrayPool byteArrayPool)
The constructor.

Parameters:
processor - is the DetectorStreamProcessor served by this buffer.
successor - is the successor in the chain or null if this is the last buffer/processor pair in the chain.
byteArrayPool - is the ByteArrayPool to use.
Method Detail

skip

public long skip(long byteCount)
This method skips the number of bytes given by byteCount in the stream starting at the current position. The given number of bytes will be untouched in stream (queued for the next processor in the chain).

Specified by:
skip in interface ByteIterator
Specified by:
skip in interface DetectorStreamBuffer
Parameters:
byteCount - is the number of bytes to ignore. This value can be greater than the currently available bytes. You may supply Long.MAX_VALUE to ignore to the end of the stream.
Returns:
the given byteCount.
See Also:
InputStream.skip(long)

skip

public long skip()
This method skips all bytes available in this buffer. This is logically the same as skip(ByteProvider.getBytesAvailable()) but more efficient.

Specified by:
skip in interface DetectorStreamBuffer
Returns:
the number of bytes that have been skipped.

getByteArray

public ByteArray getByteArray(int index)
This method gets the ByteArray at the given index. The DetectorStreamBuffer is composed out of ByteArrays. This method allows efficient processing of bytes from byte[] rather then calling ByteIterator.hasNext() and ByteIterator.next() and repetitive. Additionally this way allows full lookahead up to the end of the buffer without consuming the data.
ATTENTION:
Consuming operations such as ByteIterator.next() or ByteIterator.skip(long) will invalidate the returned ByteArray. Please do NOT call these methods while working with ByteArrays.

Specified by:
getByteArray in interface ComposedByteBuffer
Parameters:
index - is the index of the requested ByteArray. It has to be in the range from 0 to ComposedByteBuffer.getByteArrayCount() - 1. A value of 0 indicates the current ByteArray this buffer is currently pointing to. For that current buffer ByteArray.getBytes()[ByteArray.getCurrentIndex()] will have the same result as ByteIterator.peek().
Returns:
the requested ByteArray.
See Also:
ComposedByteBuffer.getByteArrayCount()

getByteArrayCount

public int getByteArrayCount()
This method gets the number of ByteArrays currently available via ComposedByteBuffer.getByteArray(int).
ATTENTION:
Consuming operations such as ByteIterator.next() or ByteIterator.skip(long) will invalidate the returned ByteArray. Please do NOT call these methods while working with ByteArrays.

Specified by:
getByteArrayCount in interface ComposedByteBuffer
Returns:
the number of currently available ByteArrays.

getBytesAvailable

public int getBytesAvailable()
This method gets the number of bytes available.

Specified by:
getBytesAvailable in interface ByteProvider
Returns:
the bytes left.

hasNext

public boolean hasNext()
This method determines if there is a next byte available.

Specified by:
hasNext in interface ByteIterator
Returns:
true if there is a next byte available, false otherwise (if the end of this buffer has been reached).

release

protected void release(ByteArray byteArray)
This method is called when a DetectorStreamBufferImpl.CurrentByteArray is wiped out of the chain.

Parameters:
byteArray - is the array to release.

nextArray

private boolean nextArray()
This method switches over to the next internal byte-array.

Returns:
true if a new buffer is available, false if the buffer queue is empty.

next

public byte next()
          throws NoSuchElementException
This method gets the current byte in the iteration. After the call of this method this ByteIterator points to the next byte in the iteration or to the end if there is no such byte available.
ATTENTION:
You should only call this method if ByteIterator.hasNext() returns true.

Specified by:
next in interface ByteIterator
Returns:
the current byte in the iteration.
Throws:
NoSuchElementException - if there is no such byte available.
See Also:
Iterator.next()

peek

public byte peek()
          throws NoSuchElementException
This method gets the current byte in the iteration. Unlike ByteIterator.next() this method does NOT modify the state of this ByteIterator. Therefore the peeked byte does NOT get consumed and repetitive calls will return the same value.
ATTENTION:
You should only call this method if ByteIterator.hasNext() returns true.

Specified by:
peek in interface ByteIterator
Returns:
the current byte in the iteration.
Throws:
NoSuchElementException - if there is no such byte available.
See Also:
ByteIterator.next()

insert

public void insert(byte... data)
This method inserts the given bytes at the current position into the stream.
It is only a convenience-method for DetectorStreamBuffer.insert(ByteArray).

Specified by:
insert in interface DetectorStreamBuffer
Parameters:
data - is a ByteArray with the data to insert.

insert

public void insert(ByteArray data)
This method inserts the given data at the current position into the stream.

Specified by:
insert in interface DetectorStreamBuffer
Parameters:
data - is a ByteArray with the data to insert.

seek

protected void seek(long byteCount,
                    DetectorStreamBufferImpl.SeekMode mode)
This method removes or skips the given number of bytes.

Parameters:
byteCount - is the number of bytes to seek.
mode - is the DetectorStreamBufferImpl.SeekMode.

remove

public void remove(long byteCount)
This method removes the number of bytes given by byteCount from the stream starting at the current position.

Specified by:
remove in interface DetectorStreamBuffer
Parameters:
byteCount - is the number of bytes to remove. This value can be greater than the currently available bytes. You may supply Long.MAX_VALUE to remove the rest of the stream.
See Also:
DetectorStreamBuffer.skip(long)

getStreamPosition

public long getStreamPosition()
This method gets the absolute position of the current pointer in the logical stream (the data provided by this interface that origins from the previous member of the chain). The value represents the number of bytes that have been consumed. It excludes the number of bytes that have been inserted.

Specified by:
getStreamPosition in interface DetectorStreamBuffer
Returns:
the current position in the stream.

append

protected void append(ByteArray nextArray)
This method queues the given DetectorStreamBufferImpl.CurrentByteArray at the end of this buffer.

Parameters:
nextArray - is the DetectorStreamBufferImpl.CurrentByteArray to append.

process

public void process(Map<String,Object> metadata,
                    boolean eos)
             throws IOException
Parameters:
metadata - is the Map with the meta-data.
eos - - true if the end of the stream has been reached and the given buffer has to be
Throws:
IOException - in case of an Input/Output error. Should only be used internally.
See Also:
DetectorStreamProcessor.process(DetectorStreamBuffer, Map, boolean)

fill

public int fill(byte[] buffer,
                int offset,
                int length)
This method fills the given buffer starting at offset with the next bytes from this ComposedByteBuffer. The bytes that are filled into the given buffer will therefore be consumed. This method can be used to write data from this buffer to an OutputStream or supply it to the consumer of an InputStream.

Specified by:
fill in interface ComposedByteBuffer
Parameters:
buffer - is the buffer to fill.
offset - is the index in the given buffer where to fill in the first byte. See ByteArray.getCurrentIndex().
length - is the expected number of bytes to fill into buffer. Has to be positive and less or equal to buffer.length - offset. However at most the number of available bytes can be filled even if length is greater.
Returns:
the number of bytes that have actually been filled into the buffer. Will be positive and less or equal to the given length. Should be only less than maxLength if NOT enough bytes are available.


Copyright © 2001-2010 mmm-Team. All Rights Reserved.