|
Sea 0.2.2 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object gov.lbl.dsd.sea.nio.util.ByteArrayList
Highly efficient resizable list holding byte
elements;
implemented with arrays; Convenient to assemble variable-sized byte messages
if message lengths are a priori unknown.
Manipulating primitive values other than bytes and ints is not directly supported. However, this can be done along the following lines:
// append double at end list = ... double elemToAdd = 1234.0; int s = list.size(); list.setSize(s + 8); list.asByteBuffer().putDouble(s, elemToAdd); // or insertion at beginning can be done this way list = ... double elemToInsert = 1234.0; list.replace(0, 0, 0, 8); // insert 8 bytes at beginning list.asByteBuffer().putDouble(0, elemToInsert);
Constructor Summary | |
ByteArrayList()
Constructs an empty list. |
|
ByteArrayList(byte[] elems)
Constructs a list SHARING the specified elements. |
|
ByteArrayList(ByteBuffer elems)
Constructs a list containing a copy of the remaining buffer elements. |
|
ByteArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity. |
|
ByteArrayList(String str,
Charset charset)
Constructs a list containing the encoded representation of the given string. |
Method Summary | |
void |
add(byte elem)
Appends the specified element to the end of this list. |
void |
add(byte[] elems)
Appends the specified elements to the end of this list. |
void |
add(ByteArrayList elems)
Appends the specified elements to the end of this list. |
void |
add(ByteBuffer elems)
Appends the remaining buffer elements to the end of this list. |
void |
add(String str,
Charset charset)
Appends the encoded representation of the given string. |
byte[] |
asArray()
Returns the elements currently stored, including invalid elements between size and capacity, if any. |
ByteBuffer |
asByteBuffer()
Returns a byte buffer SHARING elements with the receiver; the buffer will have the default NIO byte order, which is ByteOrder.BIG_ENDIAN. |
void |
clear()
Removes all elements from the receiver. |
ByteArrayList |
copy()
Returns a deep copy of the receiver. |
void |
ensureCapacity(int minCapacity)
Ensures that the receiver can hold at least the specified number of elements without needing to allocate new internal memory. |
boolean |
equals(Object otherObj)
Compares the specified Object with the receiver. |
byte |
get(int index)
Returns the element at the specified position in the receiver. |
int |
getInt(int index)
Returns the 4 byte integer at the specified position in the receiver; using the NIO default byte order, which is ByteOrder.BIG_ENDIAN. |
String |
getString(int from,
int to,
Charset charset)
Returns a decoded string representation of the receiver's bytes in the given range. |
int |
indexOf(byte elem,
int from,
int to)
Returns the index of the first occurrence of the specified element. |
void |
insert(int index,
byte elem)
Inserts the specified element before the specified position into the receiver. |
void |
insert(int index,
byte[] elems)
Inserts the specified elements before the specified position into the receiver. |
void |
insert(int index,
ByteArrayList elems)
Inserts the specified elements before the specified position into the receiver. |
void |
insert(int index,
ByteBuffer elems)
Inserts the remaining buffer elements before the specified position into the receiver. |
int |
lastIndexOf(byte elem,
int from,
int to)
Returns the index of the last occurrence of the specified element. |
void |
remove(int index)
Removes the element at the specified position from the receiver. |
void |
remove(int from,
int to)
Removes the elements in the given range from the receiver. |
boolean |
removeAll(ByteArrayList other)
Removes from the receiver all elements that are contained in the specified list. |
void |
replace(int from,
int length,
byte[] replacement)
Replaces all elements[from..from+length-1] with the given replacement. |
void |
replace(int from,
int length,
ByteArrayList replacement)
Replaces all elements[from..from+length-1] with the given replacement. |
void |
replace(int from,
int length,
ByteBuffer replacement)
Replaces all elements[from..from+length-1] with the given replacement. |
void |
replace(int from,
int length,
byte replacement,
int replacementSize)
Replaces all elements[from..from+length-1] with the given replacement. |
boolean |
retainAll(ByteArrayList other)
Retains (keeps) only the elements in the receiver that are contained in the specified other list. |
void |
reverse()
Reverses the elements of the receiver. |
void |
set(int index,
byte element)
Replaces the element at the specified position in the receiver with the specified element. |
void |
setInt(int index,
int elem)
Replaces the 4 bytes at the specified position in the receiver with the specified element; using the NIO default byte order, which is ByteOrder.BIG_ENDIAN. |
void |
setSize(int newSize)
Sets the size to the given new size; expanding the list capacity if necessary. |
int |
size()
Returns the number of elements contained in the receiver. |
void |
sort()
Sorts the receiver into ascending numerical order. |
ByteArrayList |
subList(int from,
int to)
Returns a new list of the copied part of the receiver between from , inclusive, and to , exclusive. |
byte[] |
toArray()
Returns a copied array of bytes containing all the elements in the receiver; the returned array has length = this.size(). |
ArrayList |
toList()
Returns a java.util.ArrayList containing all the elements
in the receiver. |
String |
toString()
Returns a string representation of the receiver, containing the numeric String representation of each element. |
String |
toString(Charset charset)
Returns a decoded string representation of the receiver. |
void |
trimToSize()
Trims the capacity of the receiver to be the receiver's current size. |
Methods inherited from class java.lang.Object |
getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public ByteArrayList()
public ByteArrayList(int initialCapacity)
initialCapacity
- the number of elements the receiver can hold without
auto-expanding itself by allocating new internal memory.public ByteArrayList(byte[] elems)
elems
- the array to be backed by the the constructed listpublic ByteArrayList(ByteBuffer elems)
elements.remaining()
.
elems
- the elements initially to be added to the listpublic ByteArrayList(String str, Charset charset)
str
- the string to convert.charset
- the requested charset to convert with (e.g.
Charset.forName("US-ASCII"), Charset.forName("UTF-8"))Method Detail |
public void add(byte elem)
elem
- element to be appended to this list.public void add(byte[] elems)
elems
- elements to be appended.public void add(ByteArrayList elems)
elems
- elements to be appended.public void add(ByteBuffer elems)
elems
- elements to be appended.public void add(String str, Charset charset)
str
- the string to convert.charset
- the requested charset to convert with (e.g.
Charset.forName("US-ASCII"), Charset.forName("UTF-8"))public byte[] asArray()
WARNING: For efficiency reasons and to keep memory usage low, the array is not copied . So if subsequently you modify the returned array directly via the [] operator, be sure you know what you're doing.
public ByteBuffer asByteBuffer()
WARNING: For efficiency reasons and to keep memory usage low, the array is not copied . So if subsequently you modify the returned bytebuffer, be sure you know what you're doing.
public void clear()
public ByteArrayList copy()
public boolean equals(Object otherObj)
otherObj
- the Object to be compared for equality with the receiver.
public void ensureCapacity(int minCapacity)
minCapacity
- the desired minimum capacity.public final byte get(int index)
index
- index of element to return.
IndexOutOfBoundsException
- if index is out of range.public int getInt(int index)
index
- index of first byte of the integer.
IndexOutOfBoundsException
- if index is out of range.public String getString(int from, int to, Charset charset)
from
- the index of the first element (inclusive).to
- the index of the last element (exclusive).charset
- the requested charset to convert with (e.g.
Charset.forName("US-ASCII"), Charset.forName("UTF-8"))
IndexOutOfBoundsException
- if indexes are out of rangepublic int indexOf(byte elem, int from, int to)
-1
if the receiver does not contain this element.
Searches between from
, inclusive and to
,
exclusive. Tests for identity.
elem
- element to search for.from
- the leftmost search position, inclusive.to
- the rightmost search position, exclusive.
-1
if the element is not found.
IndexOutOfBoundsException
- if indexes are out of rangepublic void insert(int index, byte elem)
index
- index before which the specified element is to be insertedelem
- element to insert.public void insert(int index, byte[] elems)
index
- index before which the specified elements are to be insertedelems
- elements to insert.public void insert(int index, ByteArrayList elems)
index
- index before which the specified elements are to be insertedelems
- elements to insert.public void insert(int index, ByteBuffer elems)
index
- index before which the specified elements are to be insertedelems
- elements to insert.public int lastIndexOf(byte elem, int from, int to)
-1
if the receiver does not contain this element.
Searches beginning at to
, inclusive until
from
, exclusive. Tests for identity.
elem
- element to search for.from
- the leftmost search position, inclusive.to
- the rightmost search position, exclusive.
-1
if the element is not found.
IndexOutOfBoundsException
- if indexes are out of range.public void remove(int index)
index
- the index of the element to removed.
IndexOutOfBoundsException
- if index is out of rangepublic void remove(int from, int to)
from
- the index of the first element to removed (inclusive).to
- the index of the last element to removed (exclusive).
IndexOutOfBoundsException
- if indexes are out of rangepublic boolean removeAll(ByteArrayList other)
other
- the other list.
true
if the receiver changed as a result of the
call.public void replace(int from, int length, byte[] replacement)
[a,b,c,d,e].replace(1,3, [x,y]) --> [a,x,y,e]
[a,b].replace(1,0, [w,x,y,z]) --> [a,w,x,y,z,b]
from
- the index of the first element to replace (inclusive)length
- the number of elements to replacereplacement
- the elements to replace the replaced elementspublic void replace(int from, int length, ByteArrayList replacement)
[a,b,c,d,e].replace(1,3, [x,y]) --> [a,x,y,e]
[a,b].replace(1,0, [w,x,y,z]) --> [a,w,x,y,z,b]
from
- the index of the first element to replace (inclusive)length
- the number of elements to replacereplacement
- the elements to replace the replaced elementspublic void replace(int from, int length, ByteBuffer replacement)
[a,b,c,d,e].replace(1,3, [x,y]) --> [a,x,y,e]
[a,b].replace(1,0, [w,x,y,z]) --> [a,w,x,y,z,b]
from
- the index of the first element to replace (inclusive)length
- the number of elements to replacereplacement
- the elements to replace the replaced elementspublic void replace(int from, int length, byte replacement, int replacementSize)
[a,b,c,d,e].replace(1,3,x,4) --> [a,x,x,x,x,e]
from
- the index of the first element to replace (inclusive)length
- the number of elements to replacereplacement
- the elements to replace the replaced elementsreplacementSize
- the number of times replacement
is to replace the replaced elementspublic boolean retainAll(ByteArrayList other)
other
- the other list to test against.
true
if the receiver changed as a result of the
call.public void reverse()
public final void set(int index, byte element)
index
- index of element to replace.element
- element to be stored at the specified position.
IndexOutOfBoundsException
- if index is out of range.public void setInt(int index, int elem)
index
- index of first element to replace.elem
- element value to set.
IndexOutOfBoundsException
- if index is out of range.public void setSize(int newSize)
newSize
- the new size.
IndexOutOfBoundsException
- if index is out of range.public int size()
public void sort()
public ByteArrayList subList(int from, int to)
from
, inclusive, and to
, exclusive.
from
- the index of the first element (inclusive).to
- the index of the last element (exclusive).
IndexOutOfBoundsException
- if indexes are out of rangepublic byte[] toArray()
public ArrayList toList()
java.util.ArrayList
containing all the elements
in the receiver.
public String toString()
public String toString(Charset charset)
charset
- the requested charset to convert with
(e.g. Charset.forName("US-ASCII"), Charset.forName("UTF-8"))public void trimToSize()
|
Sea 0.2.2 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |