Class Buffer<T extends Buffer<T>>
- java.lang.Object
-
- com.hierynomus.protocol.commons.buffer.Buffer<T>
-
- Direct Known Subclasses:
Buffer.PlainBuffer
,SMBBuffer
public class Buffer<T extends Buffer<T>> extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Buffer.BufferException
static class
Buffer.PlainBuffer
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_SIZE
The default size for aBuffer
(256 bytes)static int
MAX_SIZE
The maximum valid size of buffer (i.e.protected int
rpos
protected int
wpos
-
Method Summary
Modifier and Type Method 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 leasta
bytes available for reading from this buffer.void
ensureCapacity(int capacity)
Ensure that there is at leastcapacity
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 byteBuffer<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 bufferBuffer<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>
putNullTerminatedString(java.lang.String string, java.nio.charset.Charset charset)
Write the string with an additional null-terminator in the specified charset.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 bytebyte
readByte()
Read a byte from the bufferlong
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.java.lang.String
readNullTerminatedString(java.nio.charset.Charset charset)
Read a null-terminated string in the specified encoding.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)
Readlength
raw bytes from the buffer into a newly allocated byte array of lengthlength
.java.lang.String
readString(java.lang.String encoding, int length)
Read a string in the specified encoding.java.lang.String
readString(java.nio.charset.Charset charset, 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.
-
-
-
Field Detail
-
DEFAULT_SIZE
public static final int DEFAULT_SIZE
The default size for aBuffer
(256 bytes)- See Also:
- Constant Field Values
-
MAX_SIZE
public static final int MAX_SIZE
The maximum valid size of buffer (i.e. biggest power of two that can be represented as an int - 2^30)- See Also:
- Constant Field Values
-
rpos
protected int rpos
-
wpos
protected int wpos
-
-
Constructor Detail
-
Buffer
public Buffer(Endian endiannes)
- See Also:
DEFAULT_SIZE
-
Buffer
public Buffer(Buffer<?> from)
-
Buffer
public Buffer(byte[] data, Endian endianness)
-
Buffer
public Buffer(int size, Endian endianness)
-
-
Method Detail
-
getNextPowerOf2
protected static int getNextPowerOf2(int i)
-
array
public byte[] array()
Returns the underlying byte array. NOTE: Be careful, the structure is mutable.- Returns:
- The underlying byte array
-
available
public int available()
Returns the number of bytes still available to read from the buffer.- Returns:
- The number of bytes available from the buffer.
-
clear
public void clear()
Resets this buffer. The object becomes ready for reuse. NOTE: This does not erase the underlying byte array for performance reasons.
-
rpos
public int rpos()
Returns the current reading position of the buffer.- Returns:
- The current reading position
-
rpos
public void rpos(int rpos)
Set the current reading position.- Parameters:
rpos
- The new reading position
-
wpos
public int wpos()
Returns the current writing position of this buffer.- Returns:
- The current writing position.
-
wpos
public void wpos(int wpos)
Set the current writing position.- Parameters:
wpos
- The new writing position.
-
ensureAvailable
protected void ensureAvailable(int a) throws Buffer.BufferException
Ensure that there are at leasta
bytes available for reading from this buffer.- Parameters:
a
- The number of bytes to ensure are at least available- Throws:
Buffer.BufferException
- If there are less thana
bytes available
-
ensureCapacity
public void ensureCapacity(int capacity)
Ensure that there is at leastcapacity
bytes available in the buffer for writing. This call enlarges the buffer if there is less capacity than requested.- Parameters:
capacity
- The capacity required/
-
compact
public void compact()
Compact this buffer by truncating the read bytes from the array.
-
getCompactData
public byte[] getCompactData()
-
readBoolean
public boolean readBoolean() throws Buffer.BufferException
Read a boolean byte- Returns:
- the
true
orfalse
value read - Throws:
Buffer.BufferException
-
putBoolean
public Buffer<T> putBoolean(boolean b)
Puts a boolean byte- Parameters:
b
- the value- Returns:
- this
-
readByte
public byte readByte() throws Buffer.BufferException
Read a byte from the buffer- Returns:
- the byte read
- Throws:
Buffer.BufferException
-
putByte
public Buffer<T> putByte(byte b)
Writes a single byte into this buffer- Parameters:
b
-- Returns:
- this
-
readRawBytes
public byte[] readRawBytes(int length) throws Buffer.BufferException
Readlength
raw bytes from the buffer into a newly allocated byte array of lengthlength
.- Parameters:
length
- The number of bytes to read.- Returns:
- a newly allocated byte array of
length
containing the read bytes. - Throws:
Buffer.BufferException
- If the read operation would cause an underflow (less thanlength
bytes available)
-
readRawBytes
public void readRawBytes(byte[] buf) throws Buffer.BufferException
Read a raw byte array from the buffer into the passed byte array. Will try to read exactly the size of array bytes.- Parameters:
buf
- The array to write the read bytes into- Throws:
Buffer.BufferException
- If the read operation would cause an underflow (less bytes available than array size)
-
readRawBytes
public void readRawBytes(byte[] buf, int offset, int length) throws Buffer.BufferException
Read a raw byte array from the buffer into the passed byte array starting at offset, and reading exactly length bytes.- Parameters:
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 buffer- Throws:
Buffer.BufferException
- If the read operation would cause an underflow (less than length bytes available)
-
putRawBytes
public Buffer<T> putRawBytes(byte[] buf)
Write the bytes of the passed byte array into this buffer.- Parameters:
buf
- The array of bytes to write.- Returns:
- this.
-
putRawBytes
public 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.- Parameters:
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 array- Returns:
-
putBuffer
public Buffer<T> putBuffer(Buffer<? extends Buffer<?>> buffer)
Copies the contents of provided buffer into this buffer. NOTE: This does not update the source buffer fields!- Parameters:
buffer
- theBuffer
to copy- Returns:
- this
-
readUInt16
public int readUInt16() throws Buffer.BufferException
Read a uint16 from the buffer using the buffer's endianness.- Returns:
- an int
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 2 bytes available)
-
readUInt16
public int readUInt16(Endian endianness) throws Buffer.BufferException
Read a uint16 from the buffer using the specified endianness.- Parameters:
endianness
- The endian (Big or Little) to use- Returns:
- an int
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 2 bytes available)
-
putUInt16
public Buffer<T> putUInt16(int uint16)
Writes a uint16 integer in the buffer's endianness.- Parameters:
uint16
-- Returns:
- this
-
putUInt16
public Buffer<T> putUInt16(int uint16, Endian endianness)
Writes a uint16 integer in the specified endianness.- Parameters:
uint16
-endianness
- The endian (Big or Little) to use- Returns:
- this
-
readUInt24
public int readUInt24() throws Buffer.BufferException
Read a uint24 from the buffer using the buffer's endianness.- Returns:
- an int
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 3 bytes available)
-
readUInt24
public int readUInt24(Endian endianness) throws Buffer.BufferException
Read a uint24 from the buffer using the specified endianness.- Parameters:
endianness
- The endian (Big or Little) to use- Returns:
- an int
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 3 bytes available)
-
putUInt24
public Buffer<T> putUInt24(int uint24)
Writes a uint24 integer in the buffer's endianness.- Parameters:
uint24
-- Returns:
- this
-
putUInt24
public Buffer<T> putUInt24(int uint24, Endian endianness)
Writes a uint24 integer in the specified endianness.- Parameters:
uint24
-endianness
- The endian (Big or Little) to use- Returns:
- this
-
readUInt32AsInt
public int readUInt32AsInt() throws Buffer.BufferException
Read a uint32 from the buffer using the buffer's endianness.- Returns:
- an int (possibly truncated)
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 4 bytes available)
-
readUInt32
public long readUInt32() throws Buffer.BufferException
Read a uint32 from the buffer using the buffer's endianness.- Returns:
- a long
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 4 bytes available)
-
readUInt32
public long readUInt32(Endian endianness) throws Buffer.BufferException
Read a uint32 from the buffer using the specified endianness.- Parameters:
endianness
- The endian (Big or Little) to use- Returns:
- a long
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 4 bytes available)
-
putUInt32
public Buffer<T> putUInt32(long uint32)
Writes a uint32 integer in the buffer's endianness.- Parameters:
uint32
-- Returns:
- this
-
putUInt32
public Buffer<T> putUInt32(long uint32, Endian endianness)
Writes a uint32 integer in the specified endianness.- Parameters:
uint32
-endianness
- The endian (Big or Little) to use- Returns:
- this
-
readUInt64
public long readUInt64() throws Buffer.BufferException
Read a uint64 from the buffer using the buffer's endianness.- Returns:
- a long
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 8 bytes available)
-
readUInt64
public long readUInt64(Endian endianness) throws Buffer.BufferException
Read a uint64 from the buffer using the specified endianness.- Parameters:
endianness
- The endian (Big or Little) to use- Returns:
- a long
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 8 bytes available)
-
putUInt64
public Buffer<T> putUInt64(long uint64)
Writes a uint64 integer in the buffer's endianness.- Parameters:
uint64
-- Returns:
- this
-
putUInt64
public Buffer<T> putUInt64(long uint64, Endian endianness)
Writes a uint64 integer in the specified endianness.- Parameters:
uint64
-endianness
- The endian (Big or Little) to use- Returns:
- this
-
putLong
public Buffer<T> putLong(long longVal)
Writes a long in the buffer's endianness.Note: unlike a uint64, a long can be negative.
- Parameters:
longVal
-- Returns:
- this
-
putLong
public Buffer<T> putLong(long longVal, Endian endianness)
Writes a long in the specified endianness.Note: unlike a uint64, a long can be negative or overflowed.
- Parameters:
longVal
-- Returns:
- this
-
readLong
public long readLong() throws Buffer.BufferException
Read a long from the buffer using the buffer's endianness.- Returns:
- a long
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 8 bytes available)
-
readLong
public long readLong(Endian endianness) throws Buffer.BufferException
Read a long from the buffer using the specified endianness.- Parameters:
endianness
- The endian (Big or Little) to use- Returns:
- a long
- Throws:
Buffer.BufferException
- If this would cause an underflow (less than 8 bytes available)
-
readString
public java.lang.String readString(java.lang.String encoding, int length) throws Buffer.BufferException
Read a string in the specified encoding. If the encoding is UTF-16, the buffer's endianness is used to determine the correct byte order.- Parameters:
encoding
- The charset name to use.- Throws:
Buffer.BufferException
- If reading this string would cause an underflowjava.nio.charset.UnsupportedCharsetException
- If the charset specified is not supported by the buffer.
-
readString
public java.lang.String readString(java.nio.charset.Charset charset, int length) throws Buffer.BufferException
Read a string in the specified encoding. If the charset is UTF-16, the buffer's endianness is used to determine the correct byte order.- Parameters:
charset
- The charset to use.- Throws:
Buffer.BufferException
- If reading this string would cause an underflowjava.nio.charset.UnsupportedCharsetException
- If the charset specified is not supported by the buffer.
-
readNullTerminatedString
public java.lang.String readNullTerminatedString(java.nio.charset.Charset charset) throws Buffer.BufferException
Read a null-terminated string in the specified encoding. If the charset is UTF-16, the buffer's endianness is used to determine the correct byte order.- Parameters:
charset
- The charset to use.- Throws:
Buffer.BufferException
- If reading this string would cause an underflowjava.nio.charset.UnsupportedCharsetException
- If the charset specified is not supported by the buffer.
-
putString
public Buffer<T> putString(java.lang.String string, java.nio.charset.Charset charset)
Write the string in the specified charset. If the charset is UTF-16, the buffer's endianness is used to determine the correct byte order.- Parameters:
string
- The string to writecharset
- The charset to use- Returns:
- this
- Throws:
java.nio.charset.UnsupportedCharsetException
- If the charset specified is not supported by the buffer.
-
putNullTerminatedString
public Buffer<T> putNullTerminatedString(java.lang.String string, java.nio.charset.Charset charset)
Write the string with an additional null-terminator in the specified charset. If the charset is UTF-16, the buffer's endianness is used to determine the correct byte order.- Parameters:
string
- The string to writecharset
- The charset to use- Returns:
- this
- Throws:
java.nio.charset.UnsupportedCharsetException
- If the charset specified is not supported by the buffer.
-
skip
public Buffer<T> skip(int length) throws Buffer.BufferException
Skip the specified number of bytes.- Parameters:
length
- The number of bytes to skip- Returns:
- this
- Throws:
Buffer.BufferException
- If this would cause an underflow (less thanlength
) bytes available).
-
printHex
public java.lang.String printHex()
Gives a readable snapshot of the buffer in hex. This is useful for debugging.- Returns:
- snapshot of the buffer as a hex string with each octet delimited by a space
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
asInputStream
public java.io.InputStream asInputStream()
-
-