public class Buffer<T extends Buffer<T>>
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
static class |
Buffer.BufferException |
static class |
Buffer.PlainBuffer |
| Modifier and Type | Field and Description |
|---|---|
protected byte[] |
data |
static int |
DEFAULT_SIZE
The default size for a
Buffer (256 bytes) |
static int |
MAX_SIZE
The maximum valid size of buffer (i.e.
|
protected int |
rpos |
protected int |
wpos |
| Constructor and Description |
|---|
Buffer(Buffer<?> from) |
Buffer(byte[] data,
Endian endianness) |
Buffer(Endian endiannes) |
Buffer(int size,
Endian endianness) |
| Modifier and Type | Method and Description |
|---|---|
byte[] |
array()
Returns the underlying byte array.
|
java.io.InputStream |
asInputStream() |
int |
available()
Returns the number of bytes still available to read from the buffer.
|
void |
clear()
Resets this buffer.
|
void |
compact()
Compact this buffer by truncating the read bytes from the array.
|
protected void |
ensureAvailable(int a)
Ensure that there are at least
a bytes available for reading from this buffer. |
void |
ensureCapacity(int capacity)
Ensure that there is at least
capacity bytes available in the buffer for writing. |
byte[] |
getCompactData() |
protected static int |
getNextPowerOf2(int i) |
java.lang.String |
printHex()
Gives a readable snapshot of the buffer in hex.
|
Buffer<T> |
putBoolean(boolean b)
Puts a boolean byte
|
Buffer<T> |
putBuffer(Buffer<? extends Buffer<?>> buffer)
Copies the contents of provided buffer into this buffer
|
Buffer<T> |
putByte(byte b)
Writes a single byte into this buffer
|
Buffer<T> |
putLong(long longVal)
Writes a long in the buffer's endianness.
|
Buffer<T> |
putLong(long longVal,
Endian endianness)
Writes a long in the specified endianness.
|
Buffer<T> |
putRawBytes(byte[] buf)
Write the bytes of the passed byte array into this buffer.
|
Buffer<T> |
putRawBytes(byte[] buf,
int offset,
int length)
Write the bytes of the passed byte array into this buffer, starting at offset, and writing length bytes.
|
Buffer<T> |
putString(java.lang.String string,
java.nio.charset.Charset charset)
Write the string in the specified charset.
|
Buffer<T> |
putUInt16(int uint16)
Writes a uint16 integer in the buffer's endianness.
|
Buffer<T> |
putUInt16(int uint16,
Endian endianness)
Writes a uint16 integer in the specified endianness.
|
Buffer<T> |
putUInt24(int uint24)
Writes a uint24 integer in the buffer's endianness.
|
Buffer<T> |
putUInt24(int uint24,
Endian endianness)
Writes a uint24 integer in the specified endianness.
|
Buffer<T> |
putUInt32(long uint32)
Writes a uint32 integer in the buffer's endianness.
|
Buffer<T> |
putUInt32(long uint32,
Endian endianness)
Writes a uint32 integer in the specified endianness.
|
Buffer<T> |
putUInt64(long uint64)
Writes a uint64 integer in the buffer's endianness.
|
Buffer<T> |
putUInt64(long uint64,
Endian endianness)
Writes a uint64 integer in the specified endianness.
|
boolean |
readBoolean()
Read a boolean byte
|
byte |
readByte()
Read a byte from the buffer
|
long |
readLong()
Read a long from the buffer using the buffer's endianness.
|
long |
readLong(Endian endianness)
Read a long from the buffer using the specified endianness.
|
void |
readRawBytes(byte[] buf)
Read a raw byte array from the buffer into the passed byte array.
|
void |
readRawBytes(byte[] buf,
int offset,
int length)
Read a raw byte array from the buffer into the passed byte array starting at offset, and reading exactly length bytes.
|
byte[] |
readRawBytes(int length)
Read
length raw bytes from the buffer into a newly allocated byte array of length length. |
java.lang.String |
readString(java.nio.charset.Charset charset,
int length)
Read a string in the specified encoding.
|
java.lang.String |
readString(java.lang.String encoding,
int length)
Read a string in the specified encoding.
|
int |
readUInt16()
Read a uint16 from the buffer using the buffer's endianness.
|
int |
readUInt16(Endian endianness)
Read a uint16 from the buffer using the specified endianness.
|
int |
readUInt24()
Read a uint24 from the buffer using the buffer's endianness.
|
int |
readUInt24(Endian endianness)
Read a uint24 from the buffer using the specified endianness.
|
long |
readUInt32()
Read a uint32 from the buffer using the buffer's endianness.
|
long |
readUInt32(Endian endianness)
Read a uint32 from the buffer using the specified endianness.
|
int |
readUInt32AsInt()
Read a uint32 from the buffer using the buffer's endianness.
|
long |
readUInt64()
Read a uint64 from the buffer using the buffer's endianness.
|
long |
readUInt64(Endian endianness)
Read a uint64 from the buffer using the specified endianness.
|
int |
rpos()
Returns the current reading position of the buffer.
|
void |
rpos(int rpos)
Set the current reading position.
|
Buffer<T> |
skip(int length)
Skip the specified number of bytes.
|
java.lang.String |
toString() |
int |
wpos()
Returns the current writing position of this buffer.
|
void |
wpos(int wpos)
Set the current writing position.
|
public static final int DEFAULT_SIZE
Buffer (256 bytes)public static final int MAX_SIZE
protected byte[] data
protected int rpos
protected int wpos
public Buffer(Endian endiannes)
DEFAULT_SIZEpublic Buffer(Buffer<?> from)
public Buffer(byte[] data,
Endian endianness)
public Buffer(int size,
Endian endianness)
protected static int getNextPowerOf2(int i)
public byte[] array()
public int available()
public void clear()
public int rpos()
public void rpos(int rpos)
rpos - The new reading positionpublic int wpos()
public void wpos(int wpos)
wpos - The new writing position.protected void ensureAvailable(int a)
throws Buffer.BufferException
a bytes available for reading from this buffer.a - The number of bytes to ensure are at least availableBuffer.BufferException - If there are less than a bytes availablepublic void ensureCapacity(int capacity)
capacity bytes available in the buffer for writing.
This call enlarges the buffer if there is less capacity than requested.capacity - The capacity required/public void compact()
public byte[] getCompactData()
public boolean readBoolean()
throws Buffer.BufferException
true or false value readBuffer.BufferExceptionpublic Buffer<T> putBoolean(boolean b)
b - the valuepublic byte readByte()
throws Buffer.BufferException
Buffer.BufferExceptionpublic Buffer<T> putByte(byte b)
b - public byte[] readRawBytes(int length)
throws Buffer.BufferException
length raw bytes from the buffer into a newly allocated byte array of length length.length - The number of bytes to read.length containing the read bytes.Buffer.BufferException - If the read operation would cause an underflow (less than length bytes available)public void readRawBytes(byte[] buf)
throws Buffer.BufferException
buf - The array to write the read bytes intoBuffer.BufferException - If the read operation would cause an underflow (less bytes available than array size)public void readRawBytes(byte[] buf,
int offset,
int length)
throws Buffer.BufferException
buf - The array to write the read bytes intooffset - The offset at which to start writing into the arraylength - The number of bytes to read from this bufferBuffer.BufferException - If the read operation would cause an underflow (less than length bytes available)public Buffer<T> putRawBytes(byte[] buf)
buf - The array of bytes to write.public Buffer<T> putRawBytes(byte[] buf, int offset, int length)
buf - The array of bytes to writeoffset - The offset at which to start reading from the passed arraylength - The number of bytes to write from the passed arraypublic Buffer<T> putBuffer(Buffer<? extends Buffer<?>> buffer)
buffer - the Buffer to copypublic int readUInt16()
throws Buffer.BufferException
Buffer.BufferException - If this would cause an underflow (less than 2 bytes available)public int readUInt16(Endian endianness) throws Buffer.BufferException
endianness - The endian (Big or Little) to useBuffer.BufferException - If this would cause an underflow (less than 2 bytes available)public Buffer<T> putUInt16(int uint16)
uint16 - public Buffer<T> putUInt16(int uint16, Endian endianness)
uint16 - endianness - The endian (Big or Little) to usepublic int readUInt24()
throws Buffer.BufferException
Buffer.BufferException - If this would cause an underflow (less than 3 bytes available)public int readUInt24(Endian endianness) throws Buffer.BufferException
endianness - The endian (Big or Little) to useBuffer.BufferException - If this would cause an underflow (less than 3 bytes available)public Buffer<T> putUInt24(int uint24)
uint24 - public Buffer<T> putUInt24(int uint24, Endian endianness)
uint24 - endianness - The endian (Big or Little) to usepublic int readUInt32AsInt()
throws Buffer.BufferException
Buffer.BufferException - If this would cause an underflow (less than 4 bytes available)public long readUInt32()
throws Buffer.BufferException
Buffer.BufferException - If this would cause an underflow (less than 4 bytes available)public long readUInt32(Endian endianness) throws Buffer.BufferException
endianness - The endian (Big or Little) to useBuffer.BufferException - If this would cause an underflow (less than 4 bytes available)public Buffer<T> putUInt32(long uint32)
uint32 - public Buffer<T> putUInt32(long uint32, Endian endianness)
uint32 - endianness - The endian (Big or Little) to usepublic long readUInt64()
throws Buffer.BufferException
Buffer.BufferException - If this would cause an underflow (less than 8 bytes available)public long readUInt64(Endian endianness) throws Buffer.BufferException
endianness - The endian (Big or Little) to useBuffer.BufferException - If this would cause an underflow (less than 8 bytes available)public Buffer<T> putUInt64(long uint64)
uint64 - public Buffer<T> putUInt64(long uint64, Endian endianness)
uint64 - endianness - The endian (Big or Little) to usepublic Buffer<T> putLong(long longVal)
longVal - public Buffer<T> putLong(long longVal, Endian endianness)
longVal - public long readLong()
throws Buffer.BufferException
Buffer.BufferException - If this would cause an underflow (less than 8 bytes available)public long readLong(Endian endianness) throws Buffer.BufferException
endianness - The endian (Big or Little) to useBuffer.BufferException - If this would cause an underflow (less than 8 bytes available)public java.lang.String readString(java.lang.String encoding,
int length)
throws Buffer.BufferException
encoding - The charset name to use.Buffer.BufferException - If reading this string would cause an underflowjava.nio.charset.UnsupportedCharsetException - If the charset specified is not supported by the buffer.public java.lang.String readString(java.nio.charset.Charset charset,
int length)
throws Buffer.BufferException
charset - The charset to use.Buffer.BufferException - If reading this string would cause an underflowjava.nio.charset.UnsupportedCharsetException - If the charset specified is not supported by the buffer.public Buffer<T> putString(java.lang.String string, java.nio.charset.Charset charset)
string - The string to writecharset - The charset to usejava.nio.charset.UnsupportedCharsetException - If the charset specified is not supported by the buffer.public Buffer<T> skip(int length) throws Buffer.BufferException
length - The number of bytes to skipBuffer.BufferException - If this would cause an underflow (less than length) bytes available).public java.lang.String printHex()
public java.lang.String toString()
toString in class java.lang.Objectpublic java.io.InputStream asInputStream()