Class CRAMByteWriter
java.lang.Object
htsjdk.samtools.cram.io.CRAMByteWriter
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
ConstructorsConstructorDescriptionCreate 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 TypeMethodDescriptionintReturn the current write position (alias forsize()).voidreset()Reset the writer to empty, reusing the existing buffer.intsize()Return the number of bytes written so far.byte[]Return a copy of the bytes written so far.voidwrite(byte[] b) Write all bytes from the given array.voidwrite(byte[] b, int off, int len) Writelenbytes from the given array starting at offsetoff.voidwrite(int b) Write a single byte.
-
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) Writelenbytes from the given array starting at offsetoff.- Parameters:
b- source arrayoff- offset in source to start readinglen- number of bytes to write
-
toByteArray
public byte[] toByteArray()Return a copy of the bytes written so far. Matches the contract ofByteArrayOutputStream.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 forsize()). -
reset
public void reset()Reset the writer to empty, reusing the existing buffer.
-