net.sf.mmm.util.io.base
Class ByteArrayImpl

java.lang.Object
  extended by net.sf.mmm.util.io.base.AbstractByteArray
      extended by net.sf.mmm.util.io.base.ByteArrayImpl
All Implemented Interfaces:
ByteArray, ByteProvider
Direct Known Subclasses:
ByteArrayBufferImpl, PooledByteArray

public class ByteArrayImpl
extends AbstractByteArray

This class is similar to ByteBuffer but a lot simpler.

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

Field Summary
private  byte[] buffer
           
private  int maximumIndex
           
private  int minimumIndex
           
 
Constructor Summary
ByteArrayImpl(byte[] buffer)
          The constructor.
ByteArrayImpl(byte[] buffer, int startIndex, int maximumIndex)
          The constructor.
ByteArrayImpl(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.
 byte[] getBytes()
          This method gets the underlying byte-array of this buffer.
 int getCurrentIndex()
          This method gets the offset in the byte array.
 int getMaximumIndex()
          This method gets the maximum index in the buffer.
 int getMinimumIndex()
          This method gets the minimum index where to start reading in the byte array.
protected  void setMaximumIndex(int maximumIndex)
          This method sets the maximumIndex.
 String 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
 

Field Detail

buffer

private final byte[] buffer
See Also:
getBytes()

minimumIndex

private int minimumIndex
See Also:
getMinimumIndex()

maximumIndex

private int maximumIndex
See Also:
getMaximumIndex()
Constructor Detail

ByteArrayImpl

public ByteArrayImpl(int capacity)
The constructor.

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

ByteArrayImpl

public ByteArrayImpl(byte[] buffer)
The constructor.

Parameters:
buffer - is the internal buffer.

ByteArrayImpl

public ByteArrayImpl(byte[] buffer,
                     int startIndex,
                     int maximumIndex)
The constructor.

Parameters:
buffer - is the internal buffer.
startIndex - is the current index as well as the minimum index.
maximumIndex - is the maximum index.
Method Detail

getBytes

public byte[] getBytes()
This method gets the underlying byte-array of this buffer. You are only permitted to read the content from minimumIndex (typically currentIndex to maximumIndex. Only the creator of this object may modify this array.

Returns:
the buffer
See Also:
ByteArray.getCurrentIndex(), ByteArray.getMaximumIndex()

getCurrentIndex

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

Returns:
the offset in the byte array.

getMinimumIndex

public int getMinimumIndex()
This method gets the minimum index where to start reading in the byte array. It will be in the range from 0 to maximumIndex + 1. This is typically the same as the current index. However a mutable variant of a ByteArray may allow to modify (increase) the current-index. The value returned by this method can NOT be modified.

Returns:
the minimum index.

getMaximumIndex

public int getMaximumIndex()
This method gets the maximum index in the buffer. It will be in the range from -1 to ByteArray.getBytes().length - 1.
A negative value (-1) indicates that the buffer does NOT contain data (payload).

Returns:
the maximumIndex

setMaximumIndex

protected 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!

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

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

toString

public String toString()

Overrides:
toString in class Object


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