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

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

public abstract class AbstractByteArrayBufferBuffer
extends Object
implements ProcessableByteArrayBuffer

This is the abstract base implementation of the ProcessableByteArrayBuffer interface for a byte[]-Buffer that represents the concatenation of multiple ByteArrayBuffers. 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.

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

Constructor Summary
protected AbstractByteArrayBufferBuffer(AbstractByteArrayBufferBuffer template)
          The constructor used to copy from the given template.
  AbstractByteArrayBufferBuffer(ByteArrayBuffer... buffers)
          The constructor.
 
Method Summary
protected  boolean fill(InputStream inputStream)
          This method fills this buffer using the given inputStream.
 int getBytesAvailable()
          This method gets the number of bytes currently available in this buffer.
protected  ByteArrayBuffer getCurrentBuffer()
          This method gets the current ByteArrayBuffer.
protected  int getCurrentBufferIndex()
          This method gets the current index in the current buffer.
 byte getNext()
          This method gets the next byte to iterate.
 boolean hasNext()
          This method determines if there is a next byte available.
protected  boolean nextBuffer()
          This method switches the current buffer to the next available buffer.
 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
 

Constructor Detail

AbstractByteArrayBufferBuffer

public AbstractByteArrayBufferBuffer(ByteArrayBuffer... 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 ByteArrayBuffer.

Returns:
the current ByteArrayBuffer.

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 currently available in this buffer.

Specified by:
getBytesAvailable in interface ProcessableByteArrayBuffer
Returns:
the bytes left in this buffer.

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

getNext

public byte getNext()
             throws NoSuchElementException
This method gets the next byte to iterate.
ATTENTION:
You should only call this method if ByteIterator.hasNext() returns true.

Specified by:
getNext in interface ByteIterator
Returns:
the next byte.
Throws:
NoSuchElementException - if there is no next byte available.

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


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