Class MemoryTerminalSource
- All Implemented Interfaces:
TerminalSource
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
-
Field Summary
Fields inherited from interface org.apache.juneau.terminal.region.TerminalSource
MAX_READ_BYTES -
Method Summary
Modifier and TypeMethodDescriptionstatic MemoryTerminalSourcecreate(int capacity, int cols, int rows) Creates a source.voidfinish()Marks the stream finished: readers that reach the end seedone.nudges(String terminalId, TerminalNudgePublisher publisher) Reports every append and the finish to a publisher, outside this source's lock.read(long fromOffset, int maxBytes) Reads up tomaxBytesbytes starting atfromOffset.voidwrite(byte[] b, int off, int len) Appends bytes.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.apache.juneau.terminal.region.TerminalSource
copyTo
-
Method Details
-
create
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
Reports every append and the finish to a publisher, outside this source's lock.A
RuntimeExceptionthrown by the publisher is logged atFINEand 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 isnull .IllegalArgumentException- If the id is not a valid run id.IllegalStateException- If a publisher is already set.
-
write
Appends bytes.- Parameters:
b- The bytes.off- The start inb.len- The number of bytes.- Throws:
IllegalStateException- Iffinish()has been called.IndexOutOfBoundsException- Ifoffandlendo not fit inb.
-
finish
Marks the stream finished: readers that reach the end seedone. -
read
Description copied from interface:TerminalSourceReads up tomaxBytesbytes starting atfromOffset.When
fromOffsetequals the end, the chunk is empty. When it is past the end, the chunk is empty, itsnextis itsend, and that is belowfromOffset, which the endpoint answers with416. When it is no longer held, the chunk istruncatedand starts at the oldest offset held. When the backing store has disappeared, the chunk is empty andgone, withnextandendequal tofromOffset.- Specified by:
readin interfaceTerminalSource- 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.
-