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

Source

Int avail()

Return the number of bytes available on the input stream without blocking. Return zero if no bytes available or unknown.

in

Source

InStream in()

Creates and returns a thread safe InStream wrapper for this Buf. InStream instances should be cached for re-use.

make

Source

new make(ActorPool actorPool, Int capacity := 1024)

out

Source

OutStream out()

Creates and returns a thread safe OutStream wrapper for this Buf. OutStream instances should be cached for re-use.

Note you need to call flush() to make data available to the InStream.

read

Source

Int? read()

Read the next unsigned byte from the input stream. Return null if at end of stream.

readBuf

Source

Buf readBuf(Int n)

Attempt to read the next n bytes. Note this method may not read the full number of n bytes.

size

Source

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.

Source

Int skip(Int n)

Attempt to skip n number of bytes. Return the number of bytes actually skipped which may be equal to or lesser than n.

unread

Source

Void unread(Int b)

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

Source

This write(Int b)

Write a byte to the output stream.

This method returns immediately, with the processing happening in the Buf thread.

writeBuf

Source

This writeBuf(Buf buf, Int n := buf.remaining())

Write n bytes from the given Buf at it's current position to the output stream. If n is defaulted to buf.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 given Buf is cleared / invalidated upon return.