For Java compatability
Close the resource with the given deadline.
Close the resource with the given timeout.
Close the resource with the given timeout. This timeout is advisory, giving the callee some leeway, for example to drain clients or finish up other tasks.
Close the resource.
Close the resource. The returned Future is completed when the resource has been fully relinquished.
Given f, a function from B into A, creates an Writer[B] whose fail
and close
functions
are equivalent to Writer[A]'s.
Given f, a function from B into A, creates an Writer[B] whose fail
and close
functions
are equivalent to Writer[A]'s. Writer[B]'s write
function is equivalent to:
def write(element: B) = Writer[A].write(f(element))
Discard this stream as its output is no longer required.
Discard this stream as its output is no longer required. This could be used to signal the producer of this stream similarly how Future.raise used to propagate interrupts across future chains.
Fail this stream with a given cause
.
Construct a new Reader by applying f
to every item read from this Reader
Construct a new Reader by applying f
to every item read from this Reader
the function constructs a new Reader[B] from the value of this Reader.read
All operations of the new Reader will be in sync with self Reader. Discarding one Reader will discard the other Reader. When one Reader's onClose resolves, the other Reader's onClose will be resolved immediately with the same value.
Converts a Reader[Reader[B]]
into a Reader[B]
Converts a Reader[Reader[B]]
into a Reader[B]
Construct a new Reader by applying f
to every item read from this Reader
Construct a new Reader by applying f
to every item read from this Reader
the function transforms data of type A to B
All operations of the new Reader will be in sync with self Reader. Discarding one Reader will discard the other Reader. When one Reader's onClose resolves, the other Reader's onClose will be resolved immediately with the same value.
A Future that resolves once this writer is closed and flushed.
Asynchronously read the next element of this stream.
Write an element
into this stream.
Write an element
into this stream. Although undefined by this contract, a trustworthy
implementation (such as Pipe) would do its best to resolve the returned Future only
when a consumer observes a written element
.
The write can also resolve into a failure (failed Future).
A synchronous in-memory pipe that connects Reader and Writer in the sense that a reader's input is the output of a writer.
A pipe is structured as a smash of both interfaces, a Reader and a Writer such that can be passed directly to a consumer or a producer.
Reads and writes on the pipe are matched one to one and only one outstanding
read
orwrite
is permitted in the current implementation (multiple pending writes or reads resolve into IllegalStateException while leaving the pipe healthy). That is, thewrite
(its returned Future) is resolved when theread
consumes the written data.Here is, for example, a very typical write-loop that writes into a pipe-backed Writer:
Reading from a pipe-backed Reader is no different from working with any other reader:
Thread Safety
It is safe to call
read
,write
,fail
,discard
, andclose
concurrently. The individual calls are synchronized on the given Pipe.Closing or Failing Pipes
Besides expecting a write or a read, a pipe can be closed or failed. A writer can do both
close
andfail
the pipe, while reader can only fail the pipe viadiscard
.The following behavior is expected with regards to reading from or writing into a closed or a failed pipe:
It's also worth discussing how pipes are being closed. As closure is always initiated by a producer (writer), there is a machinery allowing it to be notified when said closure is observed by a consumer (reader).
The following rules should help reasoning about closure signals in pipes:
- Closing a pipe with a pending read resolves said read into EOF and returns a Future.Unit - Closing a pipe with a pending write by default fails said write with IllegalStateException and returns a future that will be satisfied when a consumer observes the closure (EOF) via read. If a timer is provided, the pipe will wait until the provided deadline for a successful read before failing the write. - Closing an idle pipe returns a future that will be satisfied when a consumer observes the closure (EOF) via read when a timer is provided, otherwise the pipe will be closed immedidately.