|
PulpCore | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object pulpcore.util.ByteArray
public class ByteArray
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 |
---|
public static final int BIG_ENDIAN
public static final int LITTLE_ENDIAN
Constructor Detail |
---|
public ByteArray()
public ByteArray(int length)
public ByteArray(byte[] data)
Method Detail |
---|
public void reset()
public byte[] getData()
public int getByteOrder()
BIG_ENDIAN
or LITTLE_ENDIAN
public void setByteOrder(int byteOrder)
byteOrder
- BIG_ENDIAN
or LITTLE_ENDIAN
public int length()
public void setLength(int length)
public int position()
public void setPosition(int position) throws IndexOutOfBoundsException
IndexOutOfBoundsException
- If the new position is less than zero or greater than the
data length;public void truncate()
public int available()
public void crypt(ARC4 cipher)
public void compress()
public void compress(Deflater deflater)
public void decompress() throws IOException
IOException
- if the decompression failed.public void decompress(Inflater inflater) throws IOException
IOException
- if the decompression failed.public byte readByte() throws IndexOutOfBoundsException
IndexOutOfBoundsException
public int read(byte[] buffer) throws IndexOutOfBoundsException
IndexOutOfBoundsException
- if there aren't enough remaining bytes in this ByteArray to
fill the bufferpublic int read(byte[] buffer, int offset, int length) throws IndexOutOfBoundsException
IndexOutOfBoundsException
- if there aren't enough remaining bytes in this ByteArray to
read the specified number of bytes.public void read(OutputStream out) throws IOException
IOException
public boolean readBoolean() throws IndexOutOfBoundsException
IndexOutOfBoundsException
public short readShort() throws IndexOutOfBoundsException
IndexOutOfBoundsException
public int readInt() throws IndexOutOfBoundsException
IndexOutOfBoundsException
public long readLong() throws IndexOutOfBoundsException
IndexOutOfBoundsException
public float readFloat() throws IndexOutOfBoundsException
IndexOutOfBoundsException
public double readDouble() throws IndexOutOfBoundsException
IndexOutOfBoundsException
public String readUTF() throws IndexOutOfBoundsException, UTFDataFormatException
IndexOutOfBoundsException
UTFDataFormatException
public void writeByte(int v)
public void write(byte[] buffer)
public void write(byte[] buffer, int offset, int length)
public void write(InputStream in) throws IOException
IOException
public void writeBoolean(boolean v)
public void writeShort(int v)
public void writeInt(int v)
public void writeLong(long v)
public void writeFloat(float v)
public void writeDouble(double v)
public void writeUTF(String s) throws UTFDataFormatException
UTFDataFormatException
- if the encoded string is longer than 65535 bytes.
|
PulpCore | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |