Class BoundedLineAccumulator

java.lang.Object
org.a2aproject.sdk.client.http.BoundedLineAccumulator

public final class BoundedLineAccumulator extends Object
Accumulates raw bytes for a single line while enforcing a configurable byte limit.

Used by both the blocking (readBoundedLine) and reactive (BoundedLineBodySubscriber) bounded-line readers to centralise the accumulation logic and limit enforcement.

When maxLineBytes <= 0 the per-line byte cap is disabled.

  • Constructor Summary

    Constructors
    Constructor
    Description
    BoundedLineAccumulator(int maxLineBytes)
    Creates a new accumulator with the given per-line byte limit.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addByte(int b)
    Adds a single byte to the accumulator.
    void
    addChunk(byte[] chunk, int off, int len)
    Adds a chunk of bytes to the accumulator.
    boolean
    Returns true if the accumulated bytes have exceeded the configured limit.
    void
    Resets the accumulator for the next line, clearing all accumulated bytes and the "too long" flag.
    int
    Returns the number of bytes currently accumulated.
    Assembles the accumulated bytes into a UTF-8 string.

    Methods inherited from class Object

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

    • BoundedLineAccumulator

      public BoundedLineAccumulator(int maxLineBytes)
      Creates a new accumulator with the given per-line byte limit.
      Parameters:
      maxLineBytes - the maximum number of bytes per line (0 = disabled)
  • Method Details

    • addByte

      public void addByte(int b)
      Adds a single byte to the accumulator.

      If the limit has already been exceeded, the byte is silently discarded. If adding this byte would exceed the limit, the accumulator transitions to the "too long" state and discards all previously accumulated bytes.

      Parameters:
      b - the byte to add (only the low 8 bits are used)
    • addChunk

      public void addChunk(byte[] chunk, int off, int len)
      Adds a chunk of bytes to the accumulator.

      If the accumulated byte count plus the chunk length would exceed the limit, the accumulator transitions to the "too long" state and discards all previously accumulated bytes. The chunk is not written.

      Parameters:
      chunk - the byte array to add from
      off - the start offset within the array
      len - the number of bytes to add
    • isTooLong

      public boolean isTooLong()
      Returns true if the accumulated bytes have exceeded the configured limit.
    • size

      public int size()
      Returns the number of bytes currently accumulated.
    • toLine

      public String toLine()
      Assembles the accumulated bytes into a UTF-8 string.
      Returns:
      the decoded line
    • reset

      public void reset()
      Resets the accumulator for the next line, clearing all accumulated bytes and the "too long" flag.