Class Buffer<T extends Buffer<T>>
java.lang.Object
com.hierynomus.protocol.commons.buffer.Buffer<T>
- Direct Known Subclasses:
Buffer.PlainBuffer
,SMBBuffer
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
static class
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]
static final int
The default size for aBuffer
(256 bytes)private Endian
private static final org.slf4j.Logger
static final int
The maximum valid size of buffer (i.e.protected int
protected int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbyte[]
array()
Returns the underlying byte array.int
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[]
protected static int
getNextPowerOf2
(int i) printHex()
Gives a readable snapshot of the buffer in hex.putBoolean
(boolean b) Puts a boolean byteCopies the contents of provided buffer into this buffer.putByte
(byte b) Writes a single byte into this bufferputLong
(long longVal) Writes a long in the buffer's endianness.Writes a long in the specified endianness.putNullTerminatedString
(String string, Charset charset) Write the string with an additional null-terminator in the specified charset.putNullTerminatedString
(String string, Charset charset, Endian endianness) putRawBytes
(byte[] buf) Write the bytes of the passed byte array into this buffer.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.Write the string in the specified charset.putUInt16
(int uint16) Writes a uint16 integer in the buffer's endianness.Writes a uint16 integer in the specified endianness.putUInt24
(int uint24) Writes a uint24 integer in the buffer's endianness.Writes a uint24 integer in the specified endianness.putUInt32
(long uint32) Writes a uint32 integer in the buffer's endianness.Writes a uint32 integer in the specified endianness.putUInt64
(long uint64) Writes a uint64 integer in the buffer's endianness.Writes a uint64 integer in the specified endianness.boolean
Read a boolean bytebyte
readByte()
Read a byte from the bufferlong
readLong()
Read a long from the buffer using the buffer's endianness.long
Read a long from the buffer using the specified endianness.readNullTerminatedString
(Charset charset) Read a null-terminated string in the specified encoding.private String
readNullTerminatedString
(Charset charset, Endian 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) Readlength
raw bytes from the buffer into a newly allocated byte array of lengthlength
.readString
(String encoding, int length) Read a string in the specified encoding.readString
(Charset charset, int length) Read a string in the specified encoding.private String
readString
(Charset charset, int length, Endian endianness) int
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
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
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
Read a uint32 from the buffer using the buffer's endianness.long
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.skip
(int length) Skip the specified number of bytes.toString()
int
wpos()
Returns the current writing position of this buffer.void
wpos
(int wpos) Set the current writing position.
-
Field Details
-
logger
private static final org.slf4j.Logger logger -
DEFAULT_SIZE
public static final int DEFAULT_SIZEThe default size for aBuffer
(256 bytes)- See Also:
-
MAX_SIZE
public static final int MAX_SIZEThe maximum valid size of buffer (i.e. biggest power of two that can be represented as an int - 2^30)- See Also:
-
data
private byte[] data -
endianness
-
rpos
protected int rpos -
wpos
protected int wpos
-
-
Constructor Details
-
Buffer
- See Also:
-
Buffer
-
Buffer
-
Buffer
-
Buffer
-
-
Method Details
-
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
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
Read a boolean byte- Returns:
- the
true
orfalse
value read - Throws:
Buffer.BufferException
-
putBoolean
Puts a boolean byte- Parameters:
b
- the value- Returns:
- this
-
readByte
Read a byte from the buffer- Returns:
- the byte read
- Throws:
Buffer.BufferException
-
putByte
Writes a single byte into this buffer- Parameters:
b
-- Returns:
- this
-
readRawBytes
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
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
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
Write the bytes of the passed byte array into this buffer.- Parameters:
buf
- The array of bytes to write.- Returns:
- this.
-
putRawBytes
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
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
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
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
Writes a uint16 integer in the buffer's endianness.- Parameters:
uint16
-- Returns:
- this
-
putUInt16
Writes a uint16 integer in the specified endianness.- Parameters:
uint16
-endianness
- The endian (Big or Little) to use- Returns:
- this
-
readUInt24
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
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
Writes a uint24 integer in the buffer's endianness.- Parameters:
uint24
-- Returns:
- this
-
putUInt24
Writes a uint24 integer in the specified endianness.- Parameters:
uint24
-endianness
- The endian (Big or Little) to use- Returns:
- this
-
readUInt32AsInt
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
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
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
Writes a uint32 integer in the buffer's endianness.- Parameters:
uint32
-- Returns:
- this
-
putUInt32
Writes a uint32 integer in the specified endianness.- Parameters:
uint32
-endianness
- The endian (Big or Little) to use- Returns:
- this
-
readUInt64
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
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
Writes a uint64 integer in the buffer's endianness.- Parameters:
uint64
-- Returns:
- this
-
putUInt64
Writes a uint64 integer in the specified endianness.- Parameters:
uint64
-endianness
- The endian (Big or Little) to use- Returns:
- this
-
putLong
Writes a long in the buffer's endianness.Note: unlike a uint64, a long can be negative.
- Parameters:
longVal
-- Returns:
- this
-
putLong
Writes a long in the specified endianness.Note: unlike a uint64, a long can be negative or overflowed.
- Parameters:
longVal
-- Returns:
- this
-
readLong
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
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
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 underflowUnsupportedCharsetException
- If the charset specified is not supported by the buffer.
-
readString
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 underflowUnsupportedCharsetException
- If the charset specified is not supported by the buffer.
-
readString
private String readString(Charset charset, int length, Endian endianness) throws Buffer.BufferException - Throws:
Buffer.BufferException
-
readNullTerminatedString
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 underflowUnsupportedCharsetException
- If the charset specified is not supported by the buffer.
-
readNullTerminatedString
private String readNullTerminatedString(Charset charset, Endian endianness) throws Buffer.BufferException - Throws:
Buffer.BufferException
-
putString
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:
UnsupportedCharsetException
- If the charset specified is not supported by the buffer.
-
putString
-
putNullTerminatedString
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:
UnsupportedCharsetException
- If the charset specified is not supported by the buffer.
-
putNullTerminatedString
-
skip
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
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
-
asInputStream
-