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 Object
  • Field Details

    • logger

      private static final org.slf4j.Logger logger
    • DEFAULT_SIZE

      public static final int DEFAULT_SIZE
      The default size for a Buffer (256 bytes)
      See Also:
    • 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:
    • data

      private byte[] data
    • endianness

      private Endian endianness
    • rpos

      protected int rpos
    • wpos

      protected int wpos
  • Constructor Details

    • Buffer

      public Buffer(Endian endiannes)
      See Also:
    • Buffer

      public Buffer(Buffer<?> from)
    • Buffer

      public Buffer(byte[] data, Endian endianness)
    • Buffer

      public Buffer(int size, Endian endianness)
    • Buffer

      private Buffer(byte[] data, boolean read, Endian endianness)
  • 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

      protected void ensureAvailable(int a) throws Buffer.BufferException
      Ensure that there are at least a 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 than a bytes available
    • ensureCapacity

      public void ensureCapacity(int capacity)
      Ensure that there is at least capacity 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 or false 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
      Read length raw bytes from the buffer into a newly allocated byte array of length length.
      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 than length 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 into
      offset - The offset at which to start writing into the array
      length - 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 write
      offset - The offset at which to start reading from the passed array
      length - 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 - the Buffer 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 String readString(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 underflow
      UnsupportedCharsetException - If the charset specified is not supported by the buffer.
    • readString

      public String readString(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 underflow
      UnsupportedCharsetException - 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

      public String readNullTerminatedString(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 underflow
      UnsupportedCharsetException - 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

      public Buffer<T> putString(String string, 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 write
      charset - The charset to use
      Returns:
      this
      Throws:
      UnsupportedCharsetException - If the charset specified is not supported by the buffer.
    • putString

      private Buffer<T> putString(String string, Charset charset, Endian endianness)
    • putNullTerminatedString

      public Buffer<T> putNullTerminatedString(String string, 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 write
      charset - The charset to use
      Returns:
      this
      Throws:
      UnsupportedCharsetException - If the charset specified is not supported by the buffer.
    • putNullTerminatedString

      private Buffer<T> putNullTerminatedString(String string, Charset charset, Endian endianness)
    • 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 than length) bytes available).
    • printHex

      public 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 String toString()
      Overrides:
      toString in class Object
    • asInputStream

      public InputStream asInputStream()