Class CRAMByteWriter

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

public final class CRAMByteWriter extends Object
Unsynchronized growable byte writer for CRAM codec encode operations. Replaces ByteArrayOutputStream in the hot encode path to eliminate the overhead of synchronized write() methods.

The internal buffer doubles in size when full, matching the growth strategy of ByteArrayOutputStream. This is a final class (not an OutputStream 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
    Create a writer with a default initial capacity of 256 bytes.
    CRAMByteWriter(int initialCapacity)
    Create a writer with the specified initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Return the current write position (alias for size()).
    void
    Reset the writer to empty, reusing the existing buffer.
    int
    Return the number of bytes written so far.
    byte[]
    Return a copy of the bytes written so far.
    void
    write(byte[] b)
    Write all bytes from the given array.
    void
    write(byte[] b, int off, int len)
    Write len bytes from the given array starting at offset off.
    void
    write(int b)
    Write a single byte.

    Methods inherited from class Object

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

    • CRAMByteWriter

      public CRAMByteWriter()
      Create a writer with a default initial capacity of 256 bytes.
    • CRAMByteWriter

      public CRAMByteWriter(int initialCapacity)
      Create a writer with the specified initial capacity.
      Parameters:
      initialCapacity - initial buffer size in bytes
  • Method Details

    • write

      public void write(int b)
      Write a single byte.
      Parameters:
      b - the byte to write (only the low 8 bits are used)
    • write

      public void write(byte[] b)
      Write all bytes from the given array.
      Parameters:
      b - the bytes to write
    • write

      public void write(byte[] b, int off, int len)
      Write len bytes from the given array starting at offset off.
      Parameters:
      b - source array
      off - offset in source to start reading
      len - number of bytes to write
    • toByteArray

      public byte[] toByteArray()
      Return a copy of the bytes written so far. Matches the contract of ByteArrayOutputStream.toByteArray().
      Returns:
      a new byte array containing the written data
    • size

      public int size()
      Return the number of bytes written so far.
    • getPosition

      public int getPosition()
      Return the current write position (alias for size()).
    • reset

      public void reset()
      Reset the writer to empty, reusing the existing buffer.