at.spardat.xma.util
Class ByteArray

java.lang.Object
  extended byat.spardat.xma.util.ByteArray

public class ByteArray
extends java.lang.Object

Implements an efficient, growing byte array. The array may have an optional header that consumes the first 5 bytes. The first 4 bytes reserve place for the total length, the 5th byte indicates if this is compressed or not.


Nested Class Summary
 class ByteArray.ByteArrayOS
          Inner nested class to write to the end of the buffer by using an OutputStream interface.
 
Field Summary
static int HEADER_LEN
          Length of the header
 
Constructor Summary
ByteArray(byte[] array)
          Constructor that constructs by providing the array from outside.
ByteArray(byte[] array, int length)
          Constructor that constructs by providing the array from outside.
ByteArray(int initialSize)
          Constructs with an internal buffer of size initialSize.
 
Method Summary
 void addHeader()
          Adds header at the beginning of this.
static void dump(byte[] data)
          Writes hex-dump to System.out.
static void dump(byte[] data, int len, java.io.PrintWriter pw)
          Writes a hex-dump of data to the given printwriter.
 byte get()
          Returns the byte at the current position and increments the position indicator.
 byte[] getBuffer()
          Returns the internally stored byte array.
 byte[] getBytes()
          Returns a copy of the bytes stored in this.
 ByteArray getCompressed()
          Creates a new ByteArray by compressing the contents of this.
 int getLengthInHeader()
          Returns the length stored in the header.
 java.io.OutputStream getOutputStream()
          Returns an OutputStream that allows to append to this.
 int getPosition()
          Returns current position
 byte getRel(int offset)
          Returns the byte at (position+offset) without changing the current position.
 ByteArray getUncompressed()
          Creates a new ByteArray by uncompressing the contents of this.
 boolean hasHeader()
          Indicates if this has a header.
 boolean hasRemaining()
          Returns true if there are bytes from the current position until the end.
 boolean isCompressed()
          Returns if the stream is compressed.
 void readFrom(java.io.InputStream in)
          Reads all bytes from the provided inputstream in until an EOF or an error is detected and stores the bytes at the end of this buffer.
 void readFrom(java.io.InputStream in, int maxSize)
          Reads bytes from the provided inputstream in until either EOF or the size of this is maxSize.
 int remaining()
          Returns the number of bytes from the current position until the end.
 void reset()
          All accumulated output in this is discarded.
 void rewind()
          Current position is set to zero.
 void set(byte[] b, int srcOffset, int srcLen, int dstOffset)
          Extracts srcLen bytes at offset srcOffset from b and copies them to the offset dstOffset in this.
 void setComputeHeaderLength(boolean value)
          Forces the header to indicate the length of this or not.
 void setHeader(boolean what)
          Sets the header-status of this.
 void setPosition(int pos)
          Sets the current position to the provided index.
 int size()
          Returns how many bytes this ByteArray stores.
 void write(byte[] b)
          Appends all bytes from b to this byte array.
 void write(byte[] b, int off, int len)
          Appends len bytes from the specified byte array starting at offset off to this byte array.
 void write(int b)
          Appends the specified byte to the end of this byte array
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HEADER_LEN

public static final int HEADER_LEN
Length of the header

See Also:
Constant Field Values
Constructor Detail

ByteArray

public ByteArray(int initialSize)
Constructs with an internal buffer of size initialSize.


ByteArray

public ByteArray(byte[] array)
Constructor that constructs by providing the array from outside. The array is not copied, instead the provided one is used inside.


ByteArray

public ByteArray(byte[] array,
                 int length)
Constructor that constructs by providing the array from outside. The array is not copied, instead the provided one is used inside. Only the first length bytes in array are treated as data.

Method Detail

size

public int size()
Returns how many bytes this ByteArray stores.


write

public void write(int b)
Appends the specified byte to the end of this byte array

Parameters:
b - the byte to be appended

write

public void write(byte[] b,
                  int off,
                  int len)
Appends len bytes from the specified byte array starting at offset off to this byte array.

Parameters:
b - the data.
off - the start offset in the data.
len - the number of bytes to write.

write

public void write(byte[] b)
Appends all bytes from b to this byte array.


set

public void set(byte[] b,
                int srcOffset,
                int srcLen,
                int dstOffset)
Extracts srcLen bytes at offset srcOffset from b and copies them to the offset dstOffset in this. The size is adjusted if necessary.


reset

public void reset()
All accumulated output in this is discarded. size returns zero after this call if this did not have a header. Otherwise, it contains the bytes for the header. The current position is reset to zero.


getBuffer

public byte[] getBuffer()
Returns the internally stored byte array. The array may be longer then size(), so you may only use the first size() bytes. If you modify the returned byte [], you are modifying this.


getBytes

public byte[] getBytes()
Returns a copy of the bytes stored in this. The length of the returned array is size().


readFrom

public void readFrom(java.io.InputStream in)
              throws java.io.IOException
Reads all bytes from the provided inputstream in until an EOF or an error is detected and stores the bytes at the end of this buffer.

Parameters:
in - input stream to read from
Throws:
java.io.IOException

readFrom

public void readFrom(java.io.InputStream in,
                     int maxSize)
              throws java.io.IOException
Reads bytes from the provided inputstream in until either EOF or the size of this is maxSize. Bytes are stored at the end of this buffer.

Parameters:
in - input stream to read from
maxSize - maximum size of this
Throws:
java.io.IOException

get

public byte get()
Returns the byte at the current position and increments the position indicator.


getRel

public byte getRel(int offset)
Returns the byte at (position+offset) without changing the current position.


setPosition

public void setPosition(int pos)
Sets the current position to the provided index.


rewind

public void rewind()
Current position is set to zero.


getPosition

public int getPosition()
Returns current position


remaining

public int remaining()
Returns the number of bytes from the current position until the end.


hasRemaining

public boolean hasRemaining()
Returns true if there are bytes from the current position until the end.


dump

public static void dump(byte[] data,
                        int len,
                        java.io.PrintWriter pw)
Writes a hex-dump of data to the given printwriter. For debugging.


dump

public static void dump(byte[] data)
Writes hex-dump to System.out.


getOutputStream

public java.io.OutputStream getOutputStream()
Returns an OutputStream that allows to append to this.


hasHeader

public boolean hasHeader()
Indicates if this has a header.


addHeader

public void addHeader()
Adds header at the beginning of this. The size of this must be zero. This must not already have a header.


setHeader

public void setHeader(boolean what)
Sets the header-status of this. If true, it is the responsibility of the caller that this has at least HEADER_LEN bytes stored.


setComputeHeaderLength

public void setComputeHeaderLength(boolean value)
Forces the header to indicate the length of this or not.

Throws:
java.lang.IllegalStateException - is not hasHeader()

getLengthInHeader

public int getLengthInHeader()
Returns the length stored in the header. Warning: The length is not updated if data is appended. If you want to compute the length in the header, call setLengthInHeader().

Returns:
-1 if the length is unknown (first 4 bytes are ASCII-zeros). Otherwise the length as stored in the header.
Throws:
java.lang.IllegalStateException - if this does not have a header

isCompressed

public boolean isCompressed()
Returns if the stream is compressed. This information is stored in the 5th byte.


getCompressed

public ByteArray getCompressed()
Creates a new ByteArray by compressing the contents of this. This (the receiver of this method) is not changed by this operation.


getUncompressed

public ByteArray getUncompressed()
Creates a new ByteArray by uncompressing the contents of this. This (the receiver of this method) is not changed by this operation.