Package org.apache.mina.util.byteaccess
Class CompositeByteArray
- java.lang.Object
-
- org.apache.mina.util.byteaccess.CompositeByteArray
-
- All Implemented Interfaces:
ByteArray
,IoAbsoluteReader
,IoAbsoluteWriter
public final class CompositeByteArray extends Object
A ByteArray composed of other ByteArrays. Optimized for fast relative access via cursors. Absolute access methods are provided, but may perform poorly. TODO: Write about laziness of cursor implementation - how movement doesn't happen until actual get/put.- Author:
- Apache MINA Project
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
CompositeByteArray.CursorListener
Allows for efficient detection of component boundaries when using a cursor.-
Nested classes/interfaces inherited from interface org.apache.mina.util.byteaccess.ByteArray
ByteArray.Cursor
-
-
Constructor Summary
Constructors Constructor Description CompositeByteArray()
Creates a new instance of CompositeByteArray.CompositeByteArray(ByteArrayFactory byteArrayFactory)
Creates a new instance of CompositeByteArray.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFirst(ByteArray ba)
Adds the specifiedByteArray
to the first position in the listvoid
addLast(ByteArray ba)
Adds the specifiedByteArray
to the end of the listByteArray.Cursor
cursor()
ByteArray.Cursor
cursor(int index)
ByteArray.Cursor
cursor(int index, CompositeByteArray.CursorListener listener)
Get a cursor starting at the given index and with the given listener.ByteArray.Cursor
cursor(CompositeByteArray.CursorListener listener)
Get a cursor starting at index 0 (which may not be the start of the array) and with the given listener.boolean
equals(Object other)
A ByteArray is equal to another ByteArray if they start and end at the same index, have the same byte order, and contain the same bytes at each index.int
first()
void
free()
Remove any resources associated with this object.byte
get(int index)
void
get(int index, IoBuffer bb)
Gets enough bytes to fill theIoBuffer
from the given index.char
getChar(int index)
double
getDouble(int index)
ByteArray
getFirst()
float
getFloat(int index)
int
getInt(int index)
Iterable<IoBuffer>
getIoBuffers()
long
getLong(int index)
short
getShort(int index)
IoBuffer
getSingleIoBuffer()
int
hashCode()
int
last()
int
length()
ByteOrder
order()
void
order(ByteOrder order)
Set the byte order of the array.void
put(int index, byte b)
Puts abyte
at the given index.void
put(int index, IoBuffer bb)
Puts bytes from theIoBuffer
at the given index.void
putChar(int index, char c)
Puts achar
at the given index.void
putDouble(int index, double d)
Puts adouble
at the given index.void
putFloat(int index, float f)
Puts afloat
at the given index.void
putInt(int index, int i)
Puts anint
at the given index.void
putLong(int index, long l)
Puts along
at the given index.void
putShort(int index, short s)
Puts ashort
at the given index.ByteArray
removeFirst()
Remove the firstByteArray
in the listByteArray
removeLast()
Removes the lastByteArray
in the listByteArray
removeTo(int index)
Remove componentByteArray
s to the given index (splitting them if necessary) and returning them in a singleByteArray
.ByteArray
slice(int index, int length)
Creates an array with a view of part of this array.
-
-
-
Constructor Detail
-
CompositeByteArray
public CompositeByteArray()
Creates a new instance of CompositeByteArray.
-
CompositeByteArray
public CompositeByteArray(ByteArrayFactory byteArrayFactory)
Creates a new instance of CompositeByteArray.- Parameters:
byteArrayFactory
- The factory used to create the ByteArray objects
-
-
Method Detail
-
addFirst
public void addFirst(ByteArray ba)
Adds the specifiedByteArray
to the first position in the list- Parameters:
ba
- The ByteArray to add to the list
-
removeFirst
public ByteArray removeFirst()
Remove the firstByteArray
in the list- Returns:
- The first ByteArray in the list
-
removeTo
public ByteArray removeTo(int index)
Remove componentByteArray
s to the given index (splitting them if necessary) and returning them in a singleByteArray
. The caller is responsible for freeing the returned object. TODO: Document free behaviour more thoroughly.- Parameters:
index
- The index from where we will remove bytes- Returns:
- The resulting byte aaay
-
addLast
public void addLast(ByteArray ba)
Adds the specifiedByteArray
to the end of the list- Parameters:
ba
- The ByteArray to add to the end of the list
-
removeLast
public ByteArray removeLast()
Removes the lastByteArray
in the list- Returns:
- The ByteArray that was removed
-
free
public void free()
Remove any resources associated with this object. Using the object after this method is called may result in undefined behaviour.
-
getIoBuffers
public Iterable<IoBuffer> getIoBuffers()
- Returns:
- the sequence of
IoBuffer
s that back this array. Compared togetSingleIoBuffer()
, this method should be relatively efficient for all implementations.
-
getSingleIoBuffer
public IoBuffer getSingleIoBuffer()
- Returns:
- a single
IoBuffer
that backs this array. Some implementations may initially have data split across multiple buffers, so calling this method may require a new buffer to be allocated and populated.
-
cursor
public ByteArray.Cursor cursor()
- Returns:
- a cursor starting at index 0 (which may not be the start of the array).
-
cursor
public ByteArray.Cursor cursor(int index)
- Parameters:
index
- The starting point- Returns:
- a cursor starting at the given index.
-
cursor
public ByteArray.Cursor cursor(CompositeByteArray.CursorListener listener)
Get a cursor starting at index 0 (which may not be the start of the array) and with the given listener.- Parameters:
listener
- The listener to use- Returns:
- a new
ByteArray.Cursor
instance
-
cursor
public ByteArray.Cursor cursor(int index, CompositeByteArray.CursorListener listener)
Get a cursor starting at the given index and with the given listener.- Parameters:
index
- The position of the array to start the Cursor atlistener
- The listener for the Cursor that is returned- Returns:
- The created Cursor
-
slice
public ByteArray slice(int index, int length)
Creates an array with a view of part of this array.- Parameters:
index
- The starting positionlength
- The number of bytes to copy- Returns:
- The ByteArray that is a view on the original array
-
get
public byte get(int index)
- Parameters:
index
- The starting position- Returns:
- a
byte
from the given index.
-
put
public void put(int index, byte b)
Puts abyte
at the given index.- Parameters:
index
- The positionb
- The byte to put
-
get
public void get(int index, IoBuffer bb)
Gets enough bytes to fill theIoBuffer
from the given index.- Parameters:
index
- The starting positionbb
- The IoBuffer that will be filled with the bytes
-
put
public void put(int index, IoBuffer bb)
Puts bytes from theIoBuffer
at the given index.- Parameters:
index
- The positionbb
- The bytes to put
-
first
public int first()
- Returns:
- the index of the first byte that can be accessed.
-
last
public int last()
- Returns:
- the index after the last byte that can be accessed.
-
order
public ByteOrder order()
- Returns:
- the order of the bytes.
-
order
public void order(ByteOrder order)
Set the byte order of the array.- Parameters:
order
- The ByteOrder to use
-
getShort
public short getShort(int index)
- Parameters:
index
- The starting position- Returns:
- a
short
from the given index.
-
putShort
public void putShort(int index, short s)
Puts ashort
at the given index.- Parameters:
index
- The positions
- The short to put
-
getInt
public int getInt(int index)
- Parameters:
index
- The starting position- Returns:
- an
int
from the given index.
-
putInt
public void putInt(int index, int i)
Puts anint
at the given index.- Parameters:
index
- The positioni
- The int to put
-
getLong
public long getLong(int index)
- Parameters:
index
- The starting position- Returns:
- a
long
from the given index.
-
putLong
public void putLong(int index, long l)
Puts along
at the given index.- Parameters:
index
- The positionl
- The long to put
-
getFloat
public float getFloat(int index)
- Parameters:
index
- The starting position- Returns:
- a
float
from the given index.
-
putFloat
public void putFloat(int index, float f)
Puts afloat
at the given index.- Parameters:
index
- The positionf
- The float to put
-
getDouble
public double getDouble(int index)
- Parameters:
index
- The starting position- Returns:
- a
double
from the given index.
-
putDouble
public void putDouble(int index, double d)
Puts adouble
at the given index.- Parameters:
index
- The positiond
- The doubvle to put
-
getChar
public char getChar(int index)
- Parameters:
index
- The starting position- Returns:
- a
char
from the given index.
-
putChar
public void putChar(int index, char c)
Puts achar
at the given index.- Parameters:
index
- The positionc
- The char to put
-
hashCode
public int hashCode()
-
length
public final int length()
- Specified by:
length
in interfaceIoAbsoluteReader
- Returns:
- the total number of bytes that can be accessed.
-
equals
public final boolean equals(Object other)
A ByteArray is equal to another ByteArray if they start and end at the same index, have the same byte order, and contain the same bytes at each index.
-
-