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

java.lang.Object
  extended by net.sf.mmm.util.io.impl.AbstractByteArrayBufferBuffer
All Implemented Interfaces:
ByteBuffer, ByteIterator, ByteProcessable, ByteProvider, ComposedByteBuffer, ProcessableByteArrayBuffer
Direct Known Subclasses:
ByteArrayBufferBuffer, LookaheadByteArrayBufferBuffer

public abstract class AbstractByteArrayBufferBuffer
extends Object
implements ProcessableByteArrayBuffer, ComposedByteBuffer

This is the abstract base implementation of the ProcessableByteArrayBuffer interface for a byte[]-Buffer that represents the concatenation of multiple ByteArrayBufferImpls. It has its own state (read-pointer) and does NOT modify a contained buffer when reading. If one of the underlying buffers has been read to the end this class steps to the next one in a rotating way until the last buffer has been reached, that contains data that has NOT been read before. Further this class allows to be (re)filled.
NOTE:
This class is NOT public visible, because further releases might break it's compatibility. Feel free to review and give feedback on the mailing list if you want to use it directly.

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

Field Summary
private  ByteArrayBufferImpl[] buffers
          The actual buffers.
private  int buffersEndIndex
          The index of the last buffer out of buffers.
private  int buffersIndex
          The index of the current buffer out of buffers.
private  int bufferStepCount
          The number of buffers that have been stepped through.
private  byte[] currentBufferBytes
          The value of buffers[buffersIndex].
private  int currentBufferIndex
          The current position in buffers[buffersIndex].
private  int currentBufferMax
          The value of buffers[buffersIndex].
 
Constructor Summary
protected AbstractByteArrayBufferBuffer(AbstractByteArrayBufferBuffer template)
          The constructor used to copy from the given template.
  AbstractByteArrayBufferBuffer(ByteArrayBufferImpl... buffers)
          The constructor.
 
Method Summary
 int fill(byte[] buffer, int offset, int length)
          This method fills the given buffer starting at offset with the next bytes from this ComposedByteBuffer.
protected  boolean fill(InputStream inputStream)
          This method fills this buffer using the given inputStream.
 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.
protected  ByteArrayBuffer getCurrentBuffer()
          This method gets the current ByteArrayBufferImpl.
protected  int getCurrentBufferIndex()
          This method gets the current index in the current buffer.
 boolean hasNext()
          This method determines if there is a next byte available.
 byte next()
          This method gets the current byte in the iteration.
protected  boolean nextBuffer()
          This method switches the current buffer to the next available buffer.
 byte peek()
          This method gets the current byte in the iteration.
 long process(ByteProcessor processor, long byteCount)
          This method processes the number of bytes given by length (as far as available) using the given processor.
 long skip(long byteCount)
          This method skips the number of bytes given by byteCount.
protected  void sync(AbstractByteArrayBufferBuffer master)
          This method synchronizes the buffer with the given master.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

buffers

private final ByteArrayBufferImpl[] buffers
The actual buffers.


buffersIndex

private int buffersIndex
The index of the current buffer out of buffers.


buffersEndIndex

private int buffersEndIndex
The index of the last buffer out of buffers.


bufferStepCount

private int bufferStepCount
The number of buffers that have been stepped through.


currentBufferIndex

private int currentBufferIndex
The current position in buffers[buffersIndex].


currentBufferMax

private int currentBufferMax
The value of buffers[buffersIndex].getMaximumIndex() .


currentBufferBytes

private byte[] currentBufferBytes
The value of buffers[buffersIndex].getBytes() .

Constructor Detail

AbstractByteArrayBufferBuffer

public AbstractByteArrayBufferBuffer(ByteArrayBufferImpl... buffers)
The constructor.

Parameters:
buffers - are the buffers to concat.

AbstractByteArrayBufferBuffer

protected AbstractByteArrayBufferBuffer(AbstractByteArrayBufferBuffer template)
The constructor used to copy from the given template.

Parameters:
template - is the buffer to copy.
Method Detail

getCurrentBuffer

protected ByteArrayBuffer getCurrentBuffer()
This method gets the current ByteArrayBufferImpl.

Returns:
the current ByteArrayBufferImpl.

getCurrentBufferIndex

protected int getCurrentBufferIndex()
This method gets the current index in the current buffer.

Returns:
the position in the current buffer.

nextBuffer

protected boolean nextBuffer()
This method switches the current buffer to the next available buffer. If this method is called when the last buffer has already been reached, the index will be set to getCurrentBuffer().getMaximumIndex()+1 so the end of this buffer is reached and hasNext() will return false.

Returns:
true if there was a next buffer to switch to, false if the current buffer is already the last one.

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

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

skip

public long skip(long byteCount)
This method skips the number of bytes given by byteCount.

Specified by:
skip in interface ByteIterator
Parameters:
byteCount - is the expected number of bytes to skip.
Returns:
the number of bytes that have actually been skipped. This will typically be equal to byteCount. However the value may be less if the end of this iterator has been reached before the according number of bytes have been skipped. The value will always be greater or equal to 0.
See Also:
InputStream.skip(long)

process

public long process(ByteProcessor processor,
                    long byteCount)
This method processes the number of bytes given by length (as far as available) using the given processor.

Specified by:
process in interface ByteProcessable
Parameters:
processor - is the ByteProcessor called to process the bytes. It may be called multiple types if the data is sliced into multiple byte-arrays.
byteCount - is the desired number of bytes to process. The value has to be greater or equal to 0. A value of 0 will have no effect. If you want to process all available data to the end of stream or buffer you may use Long.MAX_VALUE.
Returns:
the number of bytes that have actually been processed. For a sufficient length this will typically be equal to length. However if the end of the data has been reached, a smaller value is returned. The value will always be greater or equal to 0.

sync

protected void sync(AbstractByteArrayBufferBuffer master)
This method synchronizes the buffer with the given master.

Parameters:
master - is the buffer this buffer was created from.

fill

protected boolean fill(InputStream inputStream)
                throws IOException
This method fills this buffer using the given inputStream. If the buffer is already filled, this method will have no effect and return false.

Parameters:
inputStream - is the InputStream providing the data to fill this buffer with.
Returns:
true if the end of the stream was encountered while (re)filling this buffer, false otherwise.
Throws:
IOException - if caused by the inputStream whilst reading.

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.

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.


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