Class MemoryTerminalSource

java.lang.Object
org.apache.juneau.terminal.region.MemoryTerminalSource
All Implemented Interfaces:
TerminalSource

public final class MemoryTerminalSource extends Object implements TerminalSource
A TerminalSource over a fixed-capacity ring buffer, for producers that run in the same process.

The producer calls write(byte[], int, int) and finally finish(). Once more than the capacity has been written, the oldest bytes are overwritten; a reader asking for them gets a truncated chunk that starts at the oldest offset still held. All methods are thread-safe.

Example:

MemoryTerminalSource term = MemoryTerminalSource.create(4 * 1024 * 1024, 120, 40); term.write(bytes, 0, bytes.length); term.finish();

  • Method Details

    • create

      public static MemoryTerminalSource create(int capacity, int cols, int rows)
      Creates a source.
      Parameters:
      capacity - The ring size in bytes, at least 1.
      cols - The producer's terminal width, 1-9999.
      rows - The producer's terminal height, 1-9999.
      Returns:
      A new source.
      Throws:
      IllegalArgumentException - If a value is out of range.
    • nudges

      public MemoryTerminalSource nudges(String terminalId, TerminalNudgePublisher publisher)
      Reports every append and the finish to a publisher, outside this source's lock.

      A RuntimeException thrown by the publisher is logged at FINE and otherwise ignored, so a write never fails because of a nudge.

      Parameters:
      terminalId - The terminal id passed to the publisher; must be a valid run id.
      publisher - The publisher.
      Returns:
      This object.
      Throws:
      NullPointerException - If an argument is null.
      IllegalArgumentException - If the id is not a valid run id.
      IllegalStateException - If a publisher is already set.
    • write

      public void write(byte[] b, int off, int len)
      Appends bytes.
      Parameters:
      b - The bytes.
      off - The start in b.
      len - The number of bytes.
      Throws:
      IllegalStateException - If finish() has been called.
      IndexOutOfBoundsException - If off and len do not fit in b.
    • finish

      public void finish()
      Marks the stream finished: readers that reach the end see done.
    • read

      public TerminalChunk read(long fromOffset, int maxBytes)
      Description copied from interface: TerminalSource
      Reads up to maxBytes bytes starting at fromOffset.

      When fromOffset equals the end, the chunk is empty. When it is past the end, the chunk is empty, its next is its end, and that is below fromOffset, which the endpoint answers with 416. When it is no longer held, the chunk is truncated and starts at the oldest offset held. When the backing store has disappeared, the chunk is empty and gone, with next and end equal to fromOffset.

      Specified by:
      read in interface TerminalSource
      Parameters:
      fromOffset - The offset to read from, at least 0.
      maxBytes - The most bytes to return, clamped to 0-TerminalSource.MAX_READ_BYTES.
      Returns:
      The chunk.