TextPart.buffered constructor
- StringBuffer buffer
Creates a text part whose content is accumulated in buffer, materialized
to a String lazily on first read of text.
Internal to the streaming reducer: appending deltas to one shared
StringBuffer keeps accumulation linear (O(total length)) instead of
reallocating the whole string on every delta. The expensive toString()
happens only when a consumer actually reads the text (e.g. at a frame
boundary), not once per token.
This wrapper freezes at the buffer's length at construction time (see
text): the reducer appends the next delta to the same buffer and wraps it
in a new TextPart, so a previously returned conversation snapshot never
observes the later appends. Value equality therefore holds mid-stream —
two snapshots taken at different points compare unequal. Do not construct
or read the buffer outside the reducer.
Implementation
TextPart.buffered(StringBuffer buffer)
: _text = null,
_buffer = buffer,
_bufferLength = buffer.length;