org.apache.mina.util.byteaccess
Class CompositeByteArray

java.lang.Object
  extended by org.apache.mina.util.byteaccess.CompositeByteArray
All Implemented Interfaces:
ByteArray, IoAbsoluteReader, IoAbsoluteWriter

public final class CompositeByteArray
extends Object

A ByteArray composed of other ByteArrays. Optimised 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.

Version:
$Rev$, $Date$
Author:
The Apache MINA Project (dev@mina.apache.org)

Nested Class Summary
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
CompositeByteArray()
          Creates a new instance of CompositeByteArray.
CompositeByteArray(ByteArrayFactory byteArrayFactory)
          Creates a new instance of CompositeByteArray.
 
Method Summary
 void addFirst(ByteArray ba)
          Adds the specified ByteArray to the first position in the list
 void addLast(ByteArray ba)
          Adds the specified ByteArray to the end of the list
 ByteArray.Cursor cursor()
          Get a cursor starting at index 0 (which may not be the start of the array).
 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.
 ByteArray.Cursor cursor(int index)
          Get a cursor starting at the given index.
 ByteArray.Cursor cursor(int index, CompositeByteArray.CursorListener listener)
          Get a cursor starting at the given index 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()
          Get the index of the first byte that can be accessed.
 void free()
          Remove any resources associated with this object.
 byte get(int index)
          Gets a byte from the given index.
 void get(int index, IoBuffer bb)
          Gets enough bytes to fill the IoBuffer from the given index.
 char getChar(int index)
          Gets a char from the given index.
 double getDouble(int index)
          Gets a double from the given index.
 ByteArray getFirst()
          Returns the first ByteArray in the list
 float getFloat(int index)
          Gets a float from the given index.
 int getInt(int index)
          Gets an int from the given index.
 Iterable<IoBuffer> getIoBuffers()
          Get the sequence of IoBuffers that back this array.
 long getLong(int index)
          Gets a long from the given index.
 short getShort(int index)
          Gets a short from the given index.
 IoBuffer getSingleIoBuffer()
          Gets a single IoBuffer that backs this array.
 int last()
          Gets the index after the last byte that can be accessed.
 int length()
          Gets the total number of bytes that can be accessed.
 ByteOrder order()
          Gets the order of the bytes.
 void order(ByteOrder order)
          Set the byte order of the array.
 void put(int index, byte b)
          Puts a byte at the given index.
 void put(int index, IoBuffer bb)
          Puts bytes from the IoBuffer at the given index.
 void putChar(int index, char c)
          Puts a char at the given index.
 void putDouble(int index, double d)
          Puts a double at the given index.
 void putFloat(int index, float f)
          Puts a float at the given index.
 void putInt(int index, int i)
          Puts an int at the given index.
 void putLong(int index, long l)
          Puts a long at the given index.
 void putShort(int index, short s)
          Puts a short at the given index.
 ByteArray removeFirst()
          Remove the first ByteArray in the list
 ByteArray removeLast()
          Removes the last ByteArray in the list
 ByteArray removeTo(int index)
          Remove component ByteArrays to the given index (splitting them if necessary) and returning them in a single ByteArray.
 ByteArray slice(int index, int length)
          Creates an array with a view of part of this array.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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

getFirst

public ByteArray getFirst()
Returns the first ByteArray in the list

Returns:
The first ByteArray in the list

addFirst

public void addFirst(ByteArray ba)
Adds the specified ByteArray to the first position in the list

Parameters:
ba - The ByteArray to add to the list

removeFirst

public ByteArray removeFirst()
Remove the first ByteArray in the list

Returns:
The first ByteArray in the list

removeTo

public ByteArray removeTo(int index)
Remove component ByteArrays to the given index (splitting them if necessary) and returning them in a single ByteArray. The caller is responsible for freeing the returned object. TODO: Document free behaviour more thoroughly.


addLast

public void addLast(ByteArray ba)
Adds the specified ByteArray to the end of the list

Parameters:
ba - The ByteArray to add to the end of the list

removeLast

public ByteArray removeLast()
Removes the last ByteArray in the list

Returns:
The ByteArray that was removed

free

public void free()
Description copied from interface: ByteArray
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()
Description copied from interface: ByteArray
Get the sequence of IoBuffers that back this array. Compared to getSingleIoBuffer(), this method should be relatively efficient for all implementations.


getSingleIoBuffer

public IoBuffer getSingleIoBuffer()
Description copied from interface: ByteArray
Gets 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()
Description copied from interface: ByteArray
Get a cursor starting at index 0 (which may not be the start of the array).


cursor

public ByteArray.Cursor cursor(int index)
Description copied from interface: ByteArray
Get 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 - Returns a new 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 at
listener - The listener for the Cursor that is returned

slice

public ByteArray slice(int index,
                       int length)
Description copied from interface: IoAbsoluteReader
Creates an array with a view of part of this array.


get

public byte get(int index)
Description copied from interface: IoAbsoluteReader
Gets a byte from the given index.


put

public void put(int index,
                byte b)
Description copied from interface: IoAbsoluteWriter
Puts a byte at the given index.


get

public void get(int index,
                IoBuffer bb)
Description copied from interface: IoAbsoluteReader
Gets enough bytes to fill the IoBuffer from the given index.


put

public void put(int index,
                IoBuffer bb)
Description copied from interface: IoAbsoluteWriter
Puts bytes from the IoBuffer at the given index.


first

public int first()
Description copied from interface: IoAbsoluteReader
Get the index of the first byte that can be accessed.


last

public int last()
Description copied from interface: IoAbsoluteReader
Gets the index after the last byte that can be accessed.


order

public ByteOrder order()
Description copied from interface: IoAbsoluteReader
Gets the order of the bytes.


order

public void order(ByteOrder order)
Description copied from interface: ByteArray
Set the byte order of the array.


getShort

public short getShort(int index)
Description copied from interface: IoAbsoluteReader
Gets a short from the given index.


putShort

public void putShort(int index,
                     short s)
Description copied from interface: IoAbsoluteWriter
Puts a short at the given index.


getInt

public int getInt(int index)
Description copied from interface: IoAbsoluteReader
Gets an int from the given index.


putInt

public void putInt(int index,
                   int i)
Description copied from interface: IoAbsoluteWriter
Puts an int at the given index.


getLong

public long getLong(int index)
Description copied from interface: IoAbsoluteReader
Gets a long from the given index.


putLong

public void putLong(int index,
                    long l)
Description copied from interface: IoAbsoluteWriter
Puts a long at the given index.


getFloat

public float getFloat(int index)
Description copied from interface: IoAbsoluteReader
Gets a float from the given index.


putFloat

public void putFloat(int index,
                     float f)
Description copied from interface: IoAbsoluteWriter
Puts a float at the given index.


getDouble

public double getDouble(int index)
Description copied from interface: IoAbsoluteReader
Gets a double from the given index.


putDouble

public void putDouble(int index,
                      double d)
Description copied from interface: IoAbsoluteWriter
Puts a double at the given index.


getChar

public char getChar(int index)
Description copied from interface: IoAbsoluteReader
Gets a char from the given index.


putChar

public void putChar(int index,
                    char c)
Description copied from interface: IoAbsoluteWriter
Puts a char at the given index.


length

public final int length()
Description copied from interface: IoAbsoluteReader
Gets the total number of bytes that can be accessed.

Specified by:
length in interface IoAbsoluteReader

equals

public final boolean equals(Object other)
Description copied from interface: ByteArray
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.

Specified by:
equals in interface ByteArray
Overrides:
equals in class Object


Copyright © 2004-2009 Apache MINA Project. All Rights Reserved.