PulpCore

pulpcore.util
Class ByteArray

java.lang.Object
  extended by pulpcore.util.ByteArray

public class ByteArray
extends Object

The ByteArray class encapsulates reading data from and writing data to an array of bytes.

The ByteArray class is designed to combine the functionality of ByteArrayInputStream, ByteArrayOutputStream, DataInputStream, and DataOutputStream, and adds functionality like compression and encryption.

Both little endian and big endian byte orders are provided, and can be toggled at any time during the read/write process. The default is big endian (the same byte order as DataInputStream and DataOutputStream).

Note that, when the position is at the end, the length of the underlying data array is increased each time a write method is called. It can often be more economical to first allocate a larger array, perform the writes, and then call truncate():

    ByteArray byteArray = new ByteArray(1000);
    for (int i = 0; i < 100; i++) {
        byteArray.writeInt(i);
    }
    byteArray.truncate();
    


Field Summary
static int BIG_ENDIAN
          Big Endian is the byte order where data is stored Most Significant Byte (MSB) first.
static int LITTLE_ENDIAN
          Little Endian is the byte order where data is stored Least Significant Byte (LSB) first.
 
Constructor Summary
ByteArray()
          Creates a new ByteArray with a length of zero.
ByteArray(byte[] data)
          Creates a new ByteArray of the specified data.
ByteArray(int length)
          Creates a new ByteArray with the specified length.
 
Method Summary
 int available()
          Returns the number of bytes that can be read from the current position to the length of the data.
 void compress()
          Compresses this entire byte array using ZLIB compression.
 void compress(Deflater deflater)
          Compresses this entire byte array using ZLIB compression.
 void crypt(ARC4 cipher)
          Crypts the entire byte array using the specified cipher.
 void decompress()
          Decompresses this entire byte array using ZLIB compression.
 void decompress(Inflater inflater)
          Decompresses this entire byte array using ZLIB compression.
 int getByteOrder()
          Gets the current byte order for reading or writing data.
 byte[] getData()
          Gets the current underlying data array.
 int length()
          Returns the length of the underlying data.
 int position()
          Gets the current position where data is read from and write to.
 int read(byte[] buffer)
          Reads bytes from the current position in this ByteArray into a buffer.
 int read(byte[] buffer, int offset, int length)
          Reads bytes from the current position in this ByteArray into a buffer.
 void read(OutputStream out)
           
 boolean readBoolean()
           
 byte readByte()
           
 double readDouble()
           
 float readFloat()
           
 int readInt()
           
 long readLong()
           
 short readShort()
           
 String readUTF()
          Reads a string in the same modified UTF-8 format used in DataInputStream.
 void reset()
          Sets the byte order to BIG_ENDIAN and the position to zero.
 void setByteOrder(int byteOrder)
          Sets the current byte order for reading or writing data.
 void setLength(int length)
          Sets the length of the underlying data.
 void setPosition(int position)
          Sets the current position where data is read from and write to.
 void truncate()
          Sets the length of the data of this ByteArray to the current position.
 void write(byte[] buffer)
           
 void write(byte[] buffer, int offset, int length)
           
 void write(InputStream in)
           
 void writeBoolean(boolean v)
           
 void writeByte(int v)
           
 void writeDouble(double v)
           
 void writeFloat(float v)
           
 void writeInt(int v)
           
 void writeLong(long v)
           
 void writeShort(int v)
           
 void writeUTF(String s)
          Writes a string in the same modified UTF-8 format used by DataOutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BIG_ENDIAN

public static final int BIG_ENDIAN
Big Endian is the byte order where data is stored Most Significant Byte (MSB) first. Big Endian is Java's internal byte order, common in pre-Intel Macs, and is the common network byte order.

See Also:
Constant Field Values

LITTLE_ENDIAN

public static final int LITTLE_ENDIAN
Little Endian is the byte order where data is stored Least Significant Byte (LSB) first. Little Endian is common on PCs and Intel-based Macs.

See Also:
Constant Field Values
Constructor Detail

ByteArray

public ByteArray()
Creates a new ByteArray with a length of zero.


ByteArray

public ByteArray(int length)
Creates a new ByteArray with the specified length. The bytes are filled with zeros.


ByteArray

public ByteArray(byte[] data)
Creates a new ByteArray of the specified data. The data array is initially shared.

Method Detail

reset

public void reset()
Sets the byte order to BIG_ENDIAN and the position to zero.


getData

public byte[] getData()
Gets the current underlying data array.


getByteOrder

public int getByteOrder()
Gets the current byte order for reading or writing data.

Returns:
BIG_ENDIAN or LITTLE_ENDIAN

setByteOrder

public void setByteOrder(int byteOrder)
Sets the current byte order for reading or writing data.

Parameters:
byteOrder - BIG_ENDIAN or LITTLE_ENDIAN

length

public int length()
Returns the length of the underlying data.


setLength

public void setLength(int length)
Sets the length of the underlying data. If the new length is shorter, the data is truncated and if the position is greater than the new length, it is set to the new length.


position

public int position()
Gets the current position where data is read from and write to.


setPosition

public void setPosition(int position)
                 throws IndexOutOfBoundsException
Sets the current position where data is read from and write to.

Throws:
IndexOutOfBoundsException - If the new position is less than zero or greater than the data length;

truncate

public void truncate()
Sets the length of the data of this ByteArray to the current position.


available

public int available()
Returns the number of bytes that can be read from the current position to the length of the data.


crypt

public void crypt(ARC4 cipher)
Crypts the entire byte array using the specified cipher. The position is not changed.


compress

public void compress()
Compresses this entire byte array using ZLIB compression. The position is set to the end of the compressed data.


compress

public void compress(Deflater deflater)
Compresses this entire byte array using ZLIB compression. The position is set to the end of the compressed data.


decompress

public void decompress()
                throws IOException
Decompresses this entire byte array using ZLIB compression. The position is set to the end of the decompressed data.

Throws:
IOException - if the decompression failed.

decompress

public void decompress(Inflater inflater)
                throws IOException
Decompresses this entire byte array using ZLIB compression. The position is set to 0.

Throws:
IOException - if the decompression failed.

readByte

public byte readByte()
              throws IndexOutOfBoundsException
Throws:
IndexOutOfBoundsException

read

public int read(byte[] buffer)
         throws IndexOutOfBoundsException
Reads bytes from the current position in this ByteArray into a buffer.

Returns:
the number of bytes read (always returns the length of the byte array)
Throws:
IndexOutOfBoundsException - if there aren't enough remaining bytes in this ByteArray to fill the buffer

read

public int read(byte[] buffer,
                int offset,
                int length)
         throws IndexOutOfBoundsException
Reads bytes from the current position in this ByteArray into a buffer.

Returns:
the number of bytes read (always returns the specified length)
Throws:
IndexOutOfBoundsException - if there aren't enough remaining bytes in this ByteArray to read the specified number of bytes.

read

public void read(OutputStream out)
          throws IOException
Throws:
IOException

readBoolean

public boolean readBoolean()
                    throws IndexOutOfBoundsException
Throws:
IndexOutOfBoundsException

readShort

public short readShort()
                throws IndexOutOfBoundsException
Throws:
IndexOutOfBoundsException

readInt

public int readInt()
            throws IndexOutOfBoundsException
Throws:
IndexOutOfBoundsException

readLong

public long readLong()
              throws IndexOutOfBoundsException
Throws:
IndexOutOfBoundsException

readFloat

public float readFloat()
                throws IndexOutOfBoundsException
Throws:
IndexOutOfBoundsException

readDouble

public double readDouble()
                  throws IndexOutOfBoundsException
Throws:
IndexOutOfBoundsException

readUTF

public String readUTF()
               throws IndexOutOfBoundsException,
                      UTFDataFormatException
Reads a string in the same modified UTF-8 format used in DataInputStream.

Throws:
IndexOutOfBoundsException
UTFDataFormatException

writeByte

public void writeByte(int v)

write

public void write(byte[] buffer)

write

public void write(byte[] buffer,
                  int offset,
                  int length)

write

public void write(InputStream in)
           throws IOException
Throws:
IOException

writeBoolean

public void writeBoolean(boolean v)

writeShort

public void writeShort(int v)

writeInt

public void writeInt(int v)

writeLong

public void writeLong(long v)

writeFloat

public void writeFloat(float v)

writeDouble

public void writeDouble(double v)

writeUTF

public void writeUTF(String s)
              throws UTFDataFormatException
Writes a string in the same modified UTF-8 format used by DataOutputStream.

Throws:
UTFDataFormatException - if the encoded string is longer than 65535 bytes.

PulpCore

Copyright © 2007-2009 Interactive Pulp, LLC.