const classafConcurrent::SynchronizedBuf
sys::Obj afConcurrent::SynchronizedBuf
Provides synchronized multi-thread access to a mutable Buf.
SynchronizedBuf creates a Buf in its own thread and provides access to it via the read and write methods, and complementary InStream and OutStream implementations.
SynchronizedBuf is different to the default Buf.toImmutable() instance because SynchronizedBuf is mutable, designed to be constantly written to by one thread / stream, and constantly read by anther.
.--->--->--- --->--->--- | Producer | Sync | Consumer | ▲ Thread ▼ ---> Buf <--> ▲ Thread ▼ | Loop | | Loop | ---<---<--- ---<---<---
Note that SynchronizedBuf grows unbounded. Until, that is, reading catches up with the writing; at which point the internal Buf is cleared and reset to initial capacity.
See Threaded Streams on the Fantom forum for the initial design.
- avail
Int avail()Return the number of bytes available on the input stream without blocking. Return zero if no bytes available or unknown.
- in
InStream in()Creates and returns a thread safe
InStreamwrapper for thisBuf.InStreaminstances should be cached for re-use.- make
- out
OutStream out()Creates and returns a thread safe
OutStreamwrapper for thisBuf.OutStreaminstances should be cached for re-use.Note you need to call
flush()to make data available to theInStream.- read
Int? read()Read the next unsigned byte from the input stream. Return
nullif at end of stream.- readBuf
Attempt to read the next n bytes. Note this method may not read the full number of n bytes.
- size
Int size()Return the total number of bytes in the buffer. This is NOT the same as
avail().The internal buffer forever expands until the contents have been read, then it is cleared.
- skip
Attempt to skip
nnumber of bytes. Return the number of bytes actually skipped which may be equal to or lesser thann.- unread
Pushback a byte so that it is the next byte to be read. There is a finite limit to the number of bytes which may be pushed back.
- write
Write a byte to the output stream.
This method returns immediately, with the processing happening in the Buf thread.
- writeBuf
This writeBuf(Buf buf, Int n := buf.remaining())Write
nbytes from the givenBufat it's current position to the output stream. Ifnis defaulted tobuf.remaining(), then the entire buffer is drained to the output stream.This method return immediately, with the processing happening in the Buf thread.
Due to the use of
Buf.toImmutable()the givenBufis cleared / invalidated upon return.