Class ByteBufferUtil

java.lang.Object
com.pnfsoftware.jeb.util.io.ByteBufferUtil

public class ByteBufferUtil extends Object
Utility methods for ByteBuffers.
  • Constructor Details

    • ByteBufferUtil

      public ByteBufferUtil()
  • Method Details

    • equalsByteArray

      public static boolean equalsByteArray(ByteBuffer b, int bufferOffset, byte[] a, int arrayOffset, int size)
      Compare bytes of a buffer against the bytes of an array.
      Parameters:
      b - source buffer
      bufferOffset - offset in buffer
      a - source array
      arrayOffset - offset in array
      size - number to bytes to be compared
      Returns:
      true on equality
    • getBytes

      public static byte[] getBytes(ByteBuffer b, int offset, int size)
      Extract a sequence of bytes from a buffer into a newly-created byte array.
      Parameters:
      b -
      offset - offset in `b` (!)
      size - number of bytes to read, from `b.position()`
      Returns:
    • getBytes

      public static byte[] getBytes(ByteBuffer b)
      Read the remaining bytes of a buffer.
      Parameters:
      b -
      Returns:
    • skip

      public static void skip(ByteBuffer b, int n)
      Skip bytes, including "backward skipping". An exception is raised if the exact amount of bytes could not be skipped.
      Parameters:
      b - buffer
      n - number of bytes to skip (move forward); can be negative to partially rewind the pointer
    • skipAttempt

      public static int skipAttempt(ByteBuffer b, int n)
      Attempt to skip bytes, including "backward skipping".
      Parameters:
      b - buffer
      n - number of bytes to skip
      Returns:
      the number of bytes skipped.
    • skipToEnd

      public static void skipToEnd(ByteBuffer b)
      Go to the end of the buffer.
      Parameters:
      b - buffer
    • getUnsignedInt

      public static long getUnsignedInt(ByteBuffer b)
      Unpack a 4-byte int and zero-extend it to a long.
      Parameters:
      b -
      Returns:
    • getUnsignedInt

      public static long getUnsignedInt(ByteBuffer b, int index)
      Unpack a 4-byte int and zero-extend it to a long.
      Parameters:
      b -
      index -
      Returns:
    • wrapLE

      public static ByteBuffer wrapLE(byte[] array)
      Wrap the provided bytes in a little-endian buffer.
      Parameters:
      array -
      Returns:
    • wrapBE

      public static ByteBuffer wrapBE(byte[] array)
      Wrap the provided bytes in a big-endian buffer.
      Parameters:
      array -
      Returns:
    • wrapLE

      public static ByteBuffer wrapLE(byte[] array, int offset, int length)
      Wrap the provided bytes in a little-endian buffer.
      Parameters:
      array -
      offset -
      length -
      Returns:
    • wrapBE

      public static ByteBuffer wrapBE(byte[] array, int offset, int length)
      Wrap the provided bytes in a big-endian buffer.
      Parameters:
      array -
      offset -
      length -
      Returns:
    • align

      public static int align(ByteBuffer b, int align)
      Align the buffer using the provided byte alignment. The position will be increased from anything from 0 to ALIGN-1 bytes so that the new position is congurent to 0 (modulo N).
      Parameters:
      b -
      align - must be >= 1
      Returns:
      the buffer's new position