Class CRAMByteReader

java.lang.Object
htsjdk.samtools.cram.io.CRAMByteReader

public final class CRAMByteReader extends Object
Unsynchronized reader over a byte[] for CRAM codec decode operations. Replaces ByteArrayInputStream in the hot decode path to eliminate the overhead of synchronized read() methods (which showed up as ~10% of total decode CPU in profiling).

This is a final class (not an InputStream subclass) so the JIT can inline its methods. Thread safety is explicitly not provided — CRAM codec operations are single-threaded.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    CRAMByteReader(byte[] buf)
    Create a reader over the given byte array, starting at position 0.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Return the number of bytes remaining to be read.
    byte[]
    Return a reference to the underlying byte array.
    int
    Return the current read position within the buffer.
    int
    Read one byte, returning it as an unsigned int (0-255), or -1 if at end of buffer.
    int
    read(byte[] b, int off, int len)
    Read up to len bytes into the destination array.
    byte[]
    readFully(int len)
    Read exactly len bytes, returning them as a new array.

    Methods inherited from class Object

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

    • CRAMByteReader

      public CRAMByteReader(byte[] buf)
      Create a reader over the given byte array, starting at position 0.
      Parameters:
      buf - the data to read from (not copied — caller must not modify while reading)
  • Method Details

    • read

      public int read()
      Read one byte, returning it as an unsigned int (0-255), or -1 if at end of buffer. Matches the contract of InputStream.read().
      Returns:
      the next byte as an unsigned int, or -1 at end of data
    • read

      public int read(byte[] b, int off, int len)
      Read up to len bytes into the destination array.
      Parameters:
      b - destination array
      off - offset in destination to start writing
      len - maximum number of bytes to read
      Returns:
      the number of bytes actually read, or -1 if at end of data
    • readFully

      public byte[] readFully(int len)
      Read exactly len bytes, returning them as a new array.
      Parameters:
      len - number of bytes to read
      Returns:
      a new byte array of length len
      Throws:
      IllegalStateException - if fewer than len bytes remain
    • available

      public int available()
      Return the number of bytes remaining to be read.
    • getPosition

      public int getPosition()
      Return the current read position within the buffer.
    • getBuffer

      public byte[] getBuffer()
      Return a reference to the underlying byte array.