ewe.util
Class ByteArray

java.lang.Object
  extended byewe.util.ByteArray
All Implemented Interfaces:
Copyable, Encodable
Direct Known Subclasses:
ImageBytes

public class ByteArray
extends Object
implements Copyable, Encodable

A ByteArray is a utility class that represents a number of data bytes. It is generally used as a buffer for data storage.

Associated with the ByteArray is a byte [] value called "data" which holds the data bytes (starting from index 0) and the "length" variable specifies how many of those bytes are considered valid (which may be less than the actual length of the data variable).


Field Summary
 byte[] data
          The data bytes.
 int length
          The number of valid bytes in the data.
 
Constructor Summary
ByteArray()
           
ByteArray(byte[] data)
          This will actually use the "data" byte array as the data for the byte array.
 
Method Summary
 void append(byte data)
          Append a single byte of data.
 void append(byte[] src, int start, int length)
          Add bytes to the end of the ByteArray.
 void appendInt(int value)
          Append a four-byte integer value.
 void appendInt(int value, int numBytes)
          Append an integer value as up to four bytes in big-endian format.
 void appendLong(long value)
          Append a long value as eight bytes in big-endian format.
 void clear()
          This sets the length of the array back to zero without releasing the data bytes.
 void copyFrom(byte[] data)
          Copy the bytes from the supplied array, re-using the interal byte array if possible.
 void copyFrom(byte[] data, int offset, int length)
          Copy the bytes from the supplied array, re-using the interal byte array if possible.
 boolean delete(int where, int numBytes)
          This removes bytes from within the data.
 Object getCopy()
          Get a copy of this ByteArray with its own copy of this data.
 void insert(int where, byte[] src, int start, int length)
          Insert bytes into the ByteArray.
 void insertInt(int where, int value, int numBytes)
          Insert an integer value as up to four bytes in big-endian format.
 void insertLong(int where, long value)
          Insert a long value as eight bytes in big-endian format.
 boolean makeSpace(int where, int numBytes)
          This adds space to the ByteArray at the specified index, increasing the length value by numBytes.
 void setLength(int len)
          This sets the length of the valid data within the array, but does NOT extend OR truncate the array.
 byte[] toBytes()
          Return a copy of the data in the ByteArray as a byte array of the exact length of the data.
 void write(int where, byte[] src, int start, int length)
          This writes into the data at the specific location.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

data

public byte[] data
The data bytes.


length

public int length
The number of valid bytes in the data.

Constructor Detail

ByteArray

public ByteArray()

ByteArray

public ByteArray(byte[] data)
This will actually use the "data" byte array as the data for the byte array. It will not copy the data.

Method Detail

getCopy

public Object getCopy()
Get a copy of this ByteArray with its own copy of this data.

Specified by:
getCopy in interface Copyable

makeSpace

public boolean makeSpace(int where,
                         int numBytes)
This adds space to the ByteArray at the specified index, increasing the length value by numBytes. After this is done you can safely write into the data starting at index "where" for a length of numBytes.


write

public void write(int where,
                  byte[] src,
                  int start,
                  int length)
This writes into the data at the specific location. Make sure you have called makeSpace() before if necessary.


insert

public void insert(int where,
                   byte[] src,
                   int start,
                   int length)
Insert bytes into the ByteArray.

Parameters:
where - The index in this ByteArray where the data will go.
src - The source data bytes.
start - The start index in the source data.
length - The number of bytes to copy.

append

public void append(byte[] src,
                   int start,
                   int length)
Add bytes to the end of the ByteArray.

Parameters:
src - The source data bytes.
start - The start index in the source data.
length - The number of bytes to copy.

append

public void append(byte data)
Append a single byte of data.

Parameters:
data - The byte to append.

insertInt

public void insertInt(int where,
                      int value,
                      int numBytes)
Insert an integer value as up to four bytes in big-endian format.

Parameters:
where - the index at which to insert the integer.
value - the value to insert.
numBytes - the number of bytes (starting from the least significant) to insert.

appendInt

public void appendInt(int value,
                      int numBytes)
Append an integer value as up to four bytes in big-endian format.

Parameters:
value - the value to append.
numBytes - the number of bytes (starting from the least significant) to insert.

appendInt

public void appendInt(int value)
Append a four-byte integer value.

Parameters:
value - the value to append.

insertLong

public void insertLong(int where,
                       long value)
Insert a long value as eight bytes in big-endian format.

Parameters:
where - the index at which to insert the integer.
value - the value to insert.

appendLong

public void appendLong(long value)
Append a long value as eight bytes in big-endian format.

Parameters:
value - the value to append.

delete

public boolean delete(int where,
                      int numBytes)
This removes bytes from within the data.


clear

public void clear()
This sets the length of the array back to zero without releasing the data bytes. This means it can be used again without creating any new objects.


setLength

public void setLength(int len)
This sets the length of the valid data within the array, but does NOT extend OR truncate the array. If the supplied len is greater than the length of the byte array, the value will be reduced to the length of the byte array.


toBytes

public byte[] toBytes()
Return a copy of the data in the ByteArray as a byte array of the exact length of the data.


copyFrom

public void copyFrom(byte[] data,
                     int offset,
                     int length)
Copy the bytes from the supplied array, re-using the interal byte array if possible.


copyFrom

public void copyFrom(byte[] data)
Copy the bytes from the supplied array, re-using the interal byte array if possible.