Class ByteWriter

java.lang.Object
dev.relism.flash.bytes.ByteWriter

public final class ByteWriter extends Object
Index-based writer into a growable byte[] scratch buffer. Every write* method bounds-checks and grows the backing array only when the write would not otherwise fit — on an already-warm buffer (the steady-state case: the buffer has already grown to the connection's high-water mark), no method here allocates. Callers build a complete message in a ByteWriter-backed scratch buffer and then issue one bulk write(buffer, 0, length()). The same writer is shared by HTTP/1.1 and HTTP/2.

Lifetime and thread-safety contract

Not thread-safe — exactly one writer at a time, matching every other per-connection scratch object in this codebase (ConnectionScratch, Http1HeaderMap). reset() repositions this writer to the start of its backing array for the next message; the backing array itself is never shrunk back down, only grown — the same amortized-to-zero-allocation growth policy RequestParser's read buffer already uses.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ByteWriter(int initialCapacity)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    byte[]
    The backing buffer.
    int
    How many bytes have been written since the last reset().
    void
    Repositions this writer to the start of its buffer, ready for the next message.
    void
    Writes s's ASCII bytes, case preserved.
    void
    Writes s's ASCII bytes, lower-cased.
    void
    writeByte(byte b)
     
    void
    writeBytes(byte[] src)
     
    void
    writeBytes(byte[] src, int off, int srcLen)
     
    void
    writeDecimal(long value)
    Writes value's ASCII decimal digits (no sign — callers write '-' via writeByte(byte) first if needed).
    void
    writeHex(int value)
    Writes value's lowercase hex digits, no leading zeros (except for value == 0, which writes "0").
    void
    writeUInt16(int value)
    Big-endian 16-bit write — an HTTP/2 frame's stream-dependent fields, SETTINGS values, etc.
    void
    writeUInt24(int value)
    Big-endian 24-bit write — an HTTP/2 frame header's length field.
    void
    writeUInt31(int value)
    Big-endian 31-bit write (top bit always 0) — an HTTP/2 stream identifier.
    void
    writeUInt32(int value)
    Big-endian 32-bit write — an HTTP/2 window-size increment, SETTINGS value, etc.

    Methods inherited from class java.lang.Object

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

    • ByteWriter

      public ByteWriter(int initialCapacity)
  • Method Details

    • reset

      public void reset()
      Repositions this writer to the start of its buffer, ready for the next message.
    • array

      public byte[] array()
      The backing buffer. Valid content is [0, length()) — never assume buf.length == length().
    • length

      public int length()
      How many bytes have been written since the last reset().
    • writeByte

      public void writeByte(byte b)
    • writeBytes

      public void writeBytes(byte[] src)
    • writeBytes

      public void writeBytes(byte[] src, int off, int srcLen)
    • writeDecimal

      public void writeDecimal(long value)
      Writes value's ASCII decimal digits (no sign — callers write '-' via writeByte(byte) first if needed). value must be non-negative.
    • writeHex

      public void writeHex(int value)
      Writes value's lowercase hex digits, no leading zeros (except for value == 0, which writes "0").
    • writeAsciiLower

      public void writeAsciiLower(String s)
      Writes s's ASCII bytes, lower-cased. s must be ASCII-only.
    • writeAscii

      public void writeAscii(String s)
      Writes s's ASCII bytes, case preserved. s must be ASCII-only. Unlike new String(...).getBytes(UTF_8), writes each character directly into this buffer and avoids an intermediate byte[].
    • writeUInt16

      public void writeUInt16(int value)
      Big-endian 16-bit write — an HTTP/2 frame's stream-dependent fields, SETTINGS values, etc.
    • writeUInt24

      public void writeUInt24(int value)
      Big-endian 24-bit write — an HTTP/2 frame header's length field.
    • writeUInt31

      public void writeUInt31(int value)
      Big-endian 31-bit write (top bit always 0) — an HTTP/2 stream identifier.
    • writeUInt32

      public void writeUInt32(int value)
      Big-endian 32-bit write — an HTTP/2 window-size increment, SETTINGS value, etc.