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

java.lang.Object
  extended by net.sf.mmm.util.io.base.AbstractByteArray
      extended by net.sf.mmm.util.io.base.ByteArrayImpl
          extended by net.sf.mmm.util.io.impl.ByteArrayBufferImpl
All Implemented Interfaces:
ByteArray, ByteArrayBuffer, ByteBuffer, ByteIterator, ByteProvider

public class ByteArrayBufferImpl
extends ByteArrayImpl
implements ByteArrayBuffer

This class is similar to ByteBuffer but a lot simpler. However it allows to set the current index so the internal buffer-array can be consumed externally and proceeded very fast.
ATTENTION:
This class is NOT intended to be exposed. It should only be used internally by some class or component.

Since:
1.0.1
Author:
Joerg Hohwiller (hohwille at users.sourceforge.net)
See Also:
ByteBuffer.wrap(byte[], int, int)

Field Summary
private  int currentIndex
           
 
Constructor Summary
ByteArrayBufferImpl(byte[] buffer)
          The constructor.
ByteArrayBufferImpl(byte[] buffer, int currentIndex, int maximumIndex)
          The constructor.
ByteArrayBufferImpl(int capacity)
          The constructor.
 
Method Summary
 ByteArrayImpl createSubArray(int minimum, int maximum)
          This method creates a new ByteArray with the same bytes but the given indices.
 int getCurrentIndex()
          This method gets the offset in the byte array.
 boolean hasNext()
          This method determines if there is a next byte available.
 byte next()
          This method gets the current byte in the iteration.
 byte peek()
          This method gets the current byte in the iteration.
 void setCurrentIndex(int currentIndex)
          This method sets the currentIndex.
 void setMaximumIndex(int maximumIndex)
          This method sets the maximumIndex.
 long skip(long byteCount)
          This method skips the number of bytes given by byteCount.
 
Methods inherited from class net.sf.mmm.util.io.base.ByteArrayImpl
getBytes, getMaximumIndex, getMinimumIndex, toString
 
Methods inherited from class net.sf.mmm.util.io.base.AbstractByteArray
checkSubArray, getBytesAvailable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface net.sf.mmm.util.io.api.ByteArray
getBytes, getBytesAvailable, getMaximumIndex, getMinimumIndex
 

Field Detail

currentIndex

private int currentIndex
See Also:
getCurrentIndex()
Constructor Detail

ByteArrayBufferImpl

public ByteArrayBufferImpl(int capacity)
The constructor.

Parameters:
capacity - is the length of the internal buffer.

ByteArrayBufferImpl

public ByteArrayBufferImpl(byte[] buffer)
The constructor.

Parameters:
buffer - is the internal buffer.

ByteArrayBufferImpl

public ByteArrayBufferImpl(byte[] buffer,
                           int currentIndex,
                           int maximumIndex)
The constructor.

Parameters:
buffer - is the internal buffer.
currentIndex - is the current index.
maximumIndex - is the maximum index.
Method Detail

getCurrentIndex

public int getCurrentIndex()
This method gets the offset in the byte array. It will be in the range from 0 to maximumIndex + 1.

Specified by:
getCurrentIndex in interface ByteArray
Overrides:
getCurrentIndex in class ByteArrayImpl
Returns:
the offset in the byte array.

setCurrentIndex

public void setCurrentIndex(int currentIndex)
This method sets the currentIndex. This can be useful e.g. if data from the buffer has been consumed externally.
ATTENTION:
Be very careful and only use this method if you know what you are doing!

Specified by:
setCurrentIndex in interface ByteArrayBuffer
Parameters:
currentIndex - is the currentIndex to set. It has to be in the range from minimumIndex to maximumIndex + 1. A value of maximumIndex + 1 indicates that the buffer is consumed.

setMaximumIndex

public void setMaximumIndex(int maximumIndex)
This method sets the maximumIndex. This may be useful if the buffer should be reused.
ATTENTION:
Be very careful and only use this method if you know what you are doing!

Overrides:
setMaximumIndex in class ByteArrayImpl
Parameters:
maximumIndex - is the maximumIndex to set. It has to be in the range from 0 ( currentIndex - 1) to ByteArrayImpl.getBytes().length.

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

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

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)

createSubArray

public ByteArrayImpl createSubArray(int minimum,
                                    int maximum)
This method creates a new ByteArray with the same bytes but the given indices.
ATTENTION:
If the implementation is immutable and the given minimum and maximum index are both equal to the current indices of this ByteArray this method may return the instance itself ( this) rather than creating a new one.

Specified by:
createSubArray in interface ByteArray
Overrides:
createSubArray in class ByteArrayImpl
Parameters:
minimum - is the minimumIndex and the currentIndex for the new ByteArray. It has to be greater or equal to the minimumIndex of this ByteArray.
maximum - is the maximumIndex for the new ByteArray.
Returns:
a new ByteArray with the given indices.


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