Class ByteArray

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

public class ByteArray extends Object
Primitive reader for little-endian byte arrays. Support for fixed-length primitive and variable-length LEB128-encoded primitives.

A max bound (see maxPosition()) can be set on the array. It can be adjusted at any time after object creation. When a max bound is set, reading past that bound will generate a runtime exception. The bound is always relative to the full array length.

  • Constructor Summary

    Constructors
    Constructor
    Description
    ByteArray(byte[] bytes)
    Create a little-endian byte[] reader with an initial index of 0.
    ByteArray(byte[] bytes, int pos)
    Create a little-endian byte[] reader with the provided initial index.
    ByteArray(byte[] bytes, int pos, int maxpos)
    Create a little-endian byte[] reader with the provided initial and maximum positions.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
     
    byte[]
     
    byte
    get()
    Read one byte.
    byte[]
    get(int cnt)
    Read bytes from the current position.
    int
    i16()
    Read a signed little-endian 16-bit integer.
    int
    i32()
    Read a signed little-endian 32-bit integer.
    long
    i64()
    Read a signed little-endian 64-bit integer.
    int
    i8()
    Read a signed 8-bit integer.
    int
    Get the current maximum readable position.
    int
    maxPosition(int maxpos)
     
    int
    Get the current read position.
    int
    position(int pos)
     
    void
    skip(int cnt)
    Advance or rewind the current position.
    sub(int len)
    Create a view of this object.
     
    int
    u16()
    Read an unsigned little-endian 16-bit integer.
    int
    u31()
    Read a non-negative little-endian 32-bit integer.
    long
    u32()
    Read an unsigned little-endian 32-bit integer.
    long
    u63()
    Read a non-negative little-endian 64-bit integer.
    int
    u8()
    Read an unsigned 8-bit integer.
    int
    Read a signed LEB128-encoded 32-bit integer.
    long
    Read a signed LEB128-encoded 64-bit integer.
    int
    Read an unsigned LEB128-encoded 16-bit integer.
    long
    Read an unsigned LEB128-encoded 32-bit integer.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ByteArray

      public ByteArray(byte[] bytes)
      Create a little-endian byte[] reader with an initial index of 0.
      Parameters:
      bytes - backing byte array
    • ByteArray

      public ByteArray(byte[] bytes, int pos)
      Create a little-endian byte[] reader with the provided initial index.
      Parameters:
      bytes - backing byte array
      pos - initial index
    • ByteArray

      public ByteArray(byte[] bytes, int pos, int maxpos)
      Create a little-endian byte[] reader with the provided initial and maximum positions.
      Parameters:
      bytes - backing byte array
      pos - initial index
      maxpos - maximum index
  • Method Details

    • sub

      public ByteArray sub(int len)
      Create a view of this object.
      Parameters:
      len - count of bytes from the current position
      Returns:
      a new byte array
    • bytes

      public byte[] bytes()
      Returns:
      the underlying byte array provided to the constructor
    • maxPosition

      public int maxPosition(int maxpos)
      Parameters:
      maxpos - the new max position (which cannot be less than the current position)
      Returns:
      the previous max position
    • maxPosition

      public int maxPosition()
      Get the current maximum readable position.
      Returns:
      maximum position
    • position

      public int position(int pos)
      Parameters:
      pos - the new position (which cannot be more than the current max position)
      Returns:
      the previous position
    • position

      public int position()
      Get the current read position.
      Returns:
      current position
    • available

      public int available()
      Returns:
      the number of bytes available from the current position to the max position
    • skip

      public void skip(int cnt)
      Advance or rewind the current position.
      Parameters:
      cnt - signed byte count to skip
    • get

      public byte get()
      Read one byte.
      Returns:
      byte at the current position
    • get

      public byte[] get(int cnt)
      Read bytes from the current position.
      Parameters:
      cnt - number of bytes to read
      Returns:
      newly allocated byte array
    • i8

      public int i8()
      Read a signed 8-bit integer.
      Returns:
      decoded value
    • u8

      public int u8()
      Read an unsigned 8-bit integer.
      Returns:
      decoded value
    • i16

      public int i16()
      Read a signed little-endian 16-bit integer.
      Returns:
      decoded value
    • u16

      public int u16()
      Read an unsigned little-endian 16-bit integer.
      Returns:
      decoded value
    • u31

      public int u31()
      Read a non-negative little-endian 32-bit integer.
      Returns:
      decoded value
    • i32

      public int i32()
      Read a signed little-endian 32-bit integer.
      Returns:
      decoded value
    • u32

      public long u32()
      Read an unsigned little-endian 32-bit integer.
      Returns:
      decoded value
    • u63

      public long u63()
      Read a non-negative little-endian 64-bit integer.
      Returns:
      decoded value
    • i64

      public long i64()
      Read a signed little-endian 64-bit integer.
      Returns:
      decoded value
    • varu16

      public int varu16()
      Read an unsigned LEB128-encoded 16-bit integer.
      Returns:
      decoded value
    • vari32

      public int vari32()
      Read a signed LEB128-encoded 32-bit integer.
      Returns:
      decoded value
    • varu32

      public long varu32()
      Read an unsigned LEB128-encoded 32-bit integer.
      Returns:
      decoded value
    • vari64

      public long vari64()
      Read a signed LEB128-encoded 64-bit integer.
      Returns:
      decoded value
    • toString

      public String toString()
      Overrides:
      toString in class Object