- java.lang.Object
-
- swim.codec.Encoder<I,O>
-
- swim.codec.Writer<I,O>
-
public abstract class Writer<I,O> extends Encoder<I,O>
Continuation of how to write subsequentOutput
tokens to a stream.Writer
enables efficient, interruptible writing of network protocols and data formats, without intermediate buffering.Output tokens
A
Writer
writes tokens to anOutput
writer. Output tokens are modeled as primitiveint
s, commonly representing Unicode code points, or raw octets. EachWriter
implementation specifies the semantic type of output tokens it produces.Writer states
A
Writer
is always in one of three states: continue, done, or error. The cont state indicates thatpull
is ready to produceOutput
; the done state indicates that writing terminated successfully, and thatbind
will return the written result; the error state indicates that writing terminated in failure, and thattrap
will return the write error.Writer
subclasses default to the cont state.Feeding input
The
feed(I)
method returns aWriter
that represents the continuation of how to write the given input object to subsequentOutput
writers.feed
can be used to specify an initial object to write, or to change the object to be written.Pulling output
The
pull(Output)
method incrementally writes as muchOutput
as it can, before returning anotherWriter
that represents the continuation of how to write additionalOutput
. TheOutput
passed topull
is only guaranteed to be valid for the duration of the method call; references to the providedOutput
instance must not be stored.Writer results
A
Writer
produces a written result of typeO
, obtained via thebind()
method.bind
is only guaranteed to return a result when in the done state; thoughbind
may optionally make available partial results in other states. A failedWriter
provides a write error via thetrap()
method.trap
is only guaranteed to return an error when in the error state.Continuations
A
Writer
instance represents a continuation of how to write remainingOutput
. Rather than writing a complete output in one go, aWriter
takes anOutput
chunk and returns anotherWriter
instance that knows how to write subsequentOutput
chunks. This enables non-blocking, incremental writing that can be interrupted whenever anOutput
writer runs out of space. AWriter
terminates by returning a continuation in either the done state, or the error state.done(Object)
returns aWriter
in the done state.error(Throwable)
returns aWriter
in the error state.Forking
The
fork(Object)
method passes an out-of-band condition to aWriter
, yielding aWriter
continuation whose behavior may be altered by the given condition. For example, a consoleWriter
might support afork
condition that changes the color and style of printed text. The types of conditions accepted byfork
, and their intended semantics, are implementation defined.
-
-
Constructor Summary
Constructors Constructor Description Writer()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <O2> Writer<I,O2>
andThen(Encoder<I,O2> that)
Returns anEncoder
that continues encodingthat
Encoder
, after it finishes encodingthis
Encoder
.<O2> Writer<I,O2>
andThen(Writer<I,O2> that)
Returns aWriter
that continues writingthat
Writer
, after it finishes writingthis
Writer
.<I2> Writer<I2,O>
asDone()
Casts a doneWriter
to a different input type.<I2,O2>
Writer<I2,O2>asError()
Casts an erroredWriter
to different input and output types.O
bind()
Returns the written result.static <I,O>
Writer<I,O>done()
Returns aWriter
in the done state thatbind
s anull
written result.static <I,O>
Writer<I,O>done(O output)
Returns aWriter
in the done state thatbind
s the given writtenoutput
.static <I,O>
Writer<I,O>error(Throwable error)
Returns aWriter
in the error state thattrap
s the given writeerror
.Writer<I,O>
feed(I input)
Returns aWriter
that represents the continuation of how to write the giveninput
object.Writer<I,O>
fork(Object condition)
Returns aWriter
continuation whose behavior may be altered by the given out-of-bandcondition
.boolean
isCont()
boolean
isDone()
Returnstrue
when writing has terminated successfully, andbind
will return the written result.boolean
isError()
Returnstrue
when writing has terminated in failure, andtrap
will return the write error.abstract Writer<I,O>
pull(Output<?> output)
Incrementally writes as muchoutput
as possible, and returns anotherWriter
that represents the continuation of how to write additionalOutput
.Writer<I,O>
pull(OutputBuffer<?> output)
Incrementally encodes as muchoutput
buffer data as possible, and returns anotherEncoder
that represents the continuation of how to write additional buffer data.Throwable
trap()
Returns the write error.
-
-
-
Method Detail
-
isDone
public boolean isDone()
Returnstrue
when writing has terminated successfully, andbind
will return the written result. i.e. thisWriter
is in the done state.
-
isError
public boolean isError()
Returnstrue
when writing has terminated in failure, andtrap
will return the write error. i.e. thisWriter
is in the error state.
-
feed
public Writer<I,O> feed(I input)
Returns aWriter
that represents the continuation of how to write the giveninput
object.- Overrides:
feed
in classEncoder<I,O>
- Throws:
IllegalArgumentException
- if thisWriter
does not know how to write the giveninput
object.
-
pull
public abstract Writer<I,O> pull(Output<?> output)
Incrementally writes as muchoutput
as possible, and returns anotherWriter
that represents the continuation of how to write additionalOutput
. Ifoutput
enters the done state,pull
must return a terminatedWriter
, i.e. aWriter
in the done state, or in the error state. The givenoutput
is only guaranteed to be valid for the duration of the method call; references tooutput
must not be stored.
-
pull
public Writer<I,O> pull(OutputBuffer<?> output)
Description copied from class:Encoder
Incrementally encodes as muchoutput
buffer data as possible, and returns anotherEncoder
that represents the continuation of how to write additional buffer data. IfisLast
istrue
, thenpull
must return a terminatedEncoder
, i.e. anEncoder
in the done state, or in the error state. The givenoutput
buffer is only guaranteed to be valid for the duration of the method call; references tooutput
must not be stored.
-
fork
public Writer<I,O> fork(Object condition)
Returns aWriter
continuation whose behavior may be altered by the given out-of-bandcondition
.
-
bind
public O bind()
Returns the written result. Only guaranteed to return a result when in the done state.- Overrides:
bind
in classEncoder<I,O>
- Throws:
IllegalStateException
- if thisWriter
is not in the done state.
-
trap
public Throwable trap()
Returns the write error. Only guaranteed to return an error when in the error state.- Overrides:
trap
in classEncoder<I,O>
- Throws:
IllegalStateException
- if thisWriter
is not in the error state.
-
asDone
public <I2> Writer<I2,O> asDone()
Casts a doneWriter
to a different input type. AWriter
in the done state can have any input type.- Overrides:
asDone
in classEncoder<I,O>
- Throws:
IllegalStateException
- if thisWriter
is not in the done state.
-
asError
public <I2,O2> Writer<I2,O2> asError()
Casts an erroredWriter
to different input and output types. AWriter
in the error state can have any input type, and any output type.- Overrides:
asError
in classEncoder<I,O>
- Throws:
IllegalStateException
- if thisWriter
is not in the error state.
-
andThen
public <O2> Writer<I,O2> andThen(Writer<I,O2> that)
Returns aWriter
that continues writingthat
Writer
, after it finishes writingthis
Writer
.
-
andThen
public <O2> Writer<I,O2> andThen(Encoder<I,O2> that)
Description copied from class:Encoder
Returns anEncoder
that continues encodingthat
Encoder
, after it finishes encodingthis
Encoder
.
-
done
public static <I,O> Writer<I,O> done()
Returns aWriter
in the done state thatbind
s anull
written result.
-
done
public static <I,O> Writer<I,O> done(O output)
Returns aWriter
in the done state thatbind
s the given writtenoutput
.
-
-