public final class ParsableByteArray extends Object
Modifier and Type | Field and Description |
---|---|
byte[] |
data |
Constructor and Description |
---|
ParsableByteArray()
Creates a new instance that initially has no backing data.
|
ParsableByteArray(byte[] data)
Creates a new instance wrapping
data , and sets the limit to data.length . |
ParsableByteArray(byte[] data,
int limit)
Creates a new instance that wraps an existing array.
|
ParsableByteArray(int limit)
Creates a new instance with
limit bytes and sets the limit. |
Modifier and Type | Method and Description |
---|---|
int |
bytesLeft()
Returns the number of bytes yet to be read.
|
int |
capacity()
Returns the capacity of the array, which may be larger than the limit.
|
int |
getPosition()
Returns the current offset in the array, in bytes.
|
int |
limit()
Returns the limit.
|
int |
peekUnsignedByte()
Peeks at the next byte as an unsigned value.
|
void |
readBytes(byte[] buffer,
int offset,
int length)
Reads the next
length bytes into buffer at offset . |
void |
readBytes(ByteBuffer buffer,
int length)
Reads the next
length bytes into buffer . |
void |
readBytes(ParsableBitArray bitArray,
int length)
Reads the next
length bytes into bitArray , and resets the position of
bitArray to zero. |
double |
readDouble()
Reads the next eight bytes as a 64-bit floating point value.
|
float |
readFloat()
Reads the next four bytes as a 32-bit floating point value.
|
int |
readInt()
Reads the next four bytes as a signed value
|
String |
readLine()
Reads a line of text.
|
int |
readLittleEndianInt()
Reads the next four bytes as an signed value in little endian order.
|
int |
readLittleEndianInt24()
Reads the next three bytes as a signed value in little endian order.
|
long |
readLittleEndianLong()
Reads the next eight bytes as a signed value in little endian order.
|
short |
readLittleEndianShort()
Reads the next two bytes as a signed value.
|
long |
readLittleEndianUnsignedInt()
Reads the next four bytes as an unsigned value in little endian order.
|
int |
readLittleEndianUnsignedInt24()
Reads the next three bytes as an unsigned value in little endian order.
|
int |
readLittleEndianUnsignedIntToInt()
Reads the next four bytes as a little endian unsigned integer into an integer, if the top bit
is a zero.
|
int |
readLittleEndianUnsignedShort()
Reads the next two bytes as an unsigned value.
|
long |
readLong()
Reads the next eight bytes as a signed value.
|
short |
readShort()
Reads the next two bytes as an signed value.
|
String |
readString(int length)
Reads the next
length bytes as UTF-8 characters. |
String |
readString(int length,
Charset charset)
Reads the next
length bytes as characters in the specified Charset . |
int |
readSynchSafeInt()
Reads a Synchsafe integer.
|
int |
readUnsignedByte()
Reads the next byte as an unsigned value.
|
int |
readUnsignedFixedPoint1616()
Reads the next four bytes, returning the integer portion of the fixed point 16.16 integer.
|
long |
readUnsignedInt()
Reads the next four bytes as an unsigned value.
|
int |
readUnsignedInt24()
Reads the next three bytes as an unsigned value.
|
int |
readUnsignedIntToInt()
Reads the next four bytes as an unsigned integer into an integer, if the top bit is a zero.
|
long |
readUnsignedLongToLong()
Reads the next eight bytes as an unsigned long into a long, if the top bit is a zero.
|
int |
readUnsignedShort()
Reads the next two bytes as an unsigned value.
|
long |
readUtf8EncodedLong()
Reads a long value encoded by UTF-8 encoding
|
void |
reset()
Sets the position and limit to zero.
|
void |
reset(byte[] data,
int limit)
Updates the instance to wrap
data , and resets the position to zero. |
void |
reset(int limit)
Resets the position to zero and the limit to the specified value.
|
void |
setLimit(int limit)
Sets the limit.
|
void |
setPosition(int position)
Sets the reading offset in the array.
|
void |
skipBytes(int bytes)
Moves the reading offset by
bytes . |
public ParsableByteArray()
public ParsableByteArray(int limit)
limit
bytes and sets the limit.limit
- The limit to set.public ParsableByteArray(byte[] data)
data
, and sets the limit to data.length
.data
- The array to wrap.public ParsableByteArray(byte[] data, int limit)
data
- The data to wrap.limit
- The limit to set.public void reset(int limit)
data
is replaced with a new array of sufficient size.limit
- The limit to set.public void reset(byte[] data, int limit)
data
, and resets the position to zero.data
- The array to wrap.limit
- The limit to set.public void reset()
public int bytesLeft()
public int limit()
public void setLimit(int limit)
limit
- The limit to set.public int getPosition()
public int capacity()
public void setPosition(int position)
position
- Byte offset in the array from which to read.IllegalArgumentException
- Thrown if the new position is neither in nor at the end of the
array.public void skipBytes(int bytes)
bytes
.bytes
- The number of bytes to skip.IllegalArgumentException
- Thrown if the new position is neither in nor at the end of the
array.public void readBytes(ParsableBitArray bitArray, int length)
length
bytes into bitArray
, and resets the position of
bitArray
to zero.bitArray
- The ParsableBitArray
into which the bytes should be read.length
- The number of bytes to write.public void readBytes(byte[] buffer, int offset, int length)
length
bytes into buffer
at offset
.buffer
- The array into which the read data should be written.offset
- The offset in buffer
at which the read data should be written.length
- The number of bytes to read.System.arraycopy(Object, int, Object, int, int)
public void readBytes(ByteBuffer buffer, int length)
length
bytes into buffer
.buffer
- The ByteBuffer
into which the read data should be written.length
- The number of bytes to read.ByteBuffer.put(byte[], int, int)
public int peekUnsignedByte()
public int readUnsignedByte()
public int readUnsignedShort()
public int readLittleEndianUnsignedShort()
public short readShort()
public short readLittleEndianShort()
public int readUnsignedInt24()
public int readLittleEndianInt24()
public int readLittleEndianUnsignedInt24()
public long readUnsignedInt()
public long readLittleEndianUnsignedInt()
public int readInt()
public int readLittleEndianInt()
public long readLong()
public long readLittleEndianLong()
public int readUnsignedFixedPoint1616()
public int readSynchSafeInt()
Synchsafe integers keep the highest bit of every byte zeroed. A 32 bit synchsafe integer can store 28 bits of information.
public int readUnsignedIntToInt()
IllegalStateException
- Thrown if the top bit of the input data is set.public int readLittleEndianUnsignedIntToInt()
IllegalStateException
- Thrown if the top bit of the input data is set.public long readUnsignedLongToLong()
IllegalStateException
- Thrown if the top bit of the input data is set.public float readFloat()
public double readDouble()
public String readString(int length)
length
bytes as UTF-8 characters.length
- The number of bytes to read.public String readString(int length, Charset charset)
length
bytes as characters in the specified Charset
.length
- The number of bytes to read.charset
- The character set of the encoded characters.public String readLine()
A line is considered to be terminated by any one of a carriage return ('\r'), a line feed ('\n'), or a carriage return followed immediately by a line feed ('\r\n'). The system's default charset (UTF-8) is used.
public long readUtf8EncodedLong()
NumberFormatException
- if there is a problem with decoding