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 classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate byte[]static final intThe default size for aBuffer(256 bytes)private Endianprivate static final org.slf4j.Loggerstatic final intThe maximum valid size of buffer (i.e.protected intprotected int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbyte[]array()Returns the underlying byte array.intReturns the number of bytes still available to read from the buffer.voidclear()Resets this buffer.voidcompact()Compact this buffer by truncating the read bytes from the array.protected voidensureAvailable(int a) Ensure that there are at leastabytes available for reading from this buffer.voidensureCapacity(int capacity) Ensure that there is at leastcapacitybytes available in the buffer for writing.byte[]protected static intgetNextPowerOf2(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.booleanRead a boolean bytebytereadByte()Read a byte from the bufferlongreadLong()Read a long from the buffer using the buffer's endianness.longRead a long from the buffer using the specified endianness.readNullTerminatedString(Charset charset) Read a null-terminated string in the specified encoding.private StringreadNullTerminatedString(Charset charset, Endian endianness) voidreadRawBytes(byte[] buf) Read a raw byte array from the buffer into the passed byte array.voidreadRawBytes(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) Readlengthraw 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 StringreadString(Charset charset, int length, Endian endianness) intRead a uint16 from the buffer using the buffer's endianness.intreadUInt16(Endian endianness) Read a uint16 from the buffer using the specified endianness.intRead a uint24 from the buffer using the buffer's endianness.intreadUInt24(Endian endianness) Read a uint24 from the buffer using the specified endianness.longRead a uint32 from the buffer using the buffer's endianness.longreadUInt32(Endian endianness) Read a uint32 from the buffer using the specified endianness.intRead a uint32 from the buffer using the buffer's endianness.longRead a uint64 from the buffer using the buffer's endianness.longreadUInt64(Endian endianness) Read a uint64 from the buffer using the specified endianness.intrpos()Returns the current reading position of the buffer.voidrpos(int rpos) Set the current reading position.skip(int length) Skip the specified number of bytes.toString()intwpos()Returns the current writing position of this buffer.voidwpos(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 leastabytes 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 thanabytes available
-
ensureCapacity
public void ensureCapacity(int capacity) Ensure that there is at leastcapacitybytes 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
trueorfalsevalue read - Throws:
Buffer.BufferException
-
putBoolean
-
readByte
Read a byte from the buffer- Returns:
- the byte read
- Throws:
Buffer.BufferException
-
putByte
-
readRawBytes
Readlengthraw 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
lengthcontaining the read bytes. - Throws:
Buffer.BufferException- If the read operation would cause an underflow (less thanlengthbytes 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
-
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
-
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
-
putUInt16
-
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
-
putUInt24
-
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
-
putUInt32
-
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
-
putUInt64
-
putLong
-
putLong
-
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
-