- 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 subsequentOutputtokens to a stream.Writerenables efficient, interruptible writing of network protocols and data formats, without intermediate buffering.Output tokens
A
Writerwrites tokens to anOutputwriter. Output tokens are modeled as primitiveints, commonly representing Unicode code points, or raw octets. EachWriterimplementation specifies the semantic type of output tokens it produces.Writer states
A
Writeris always in one of three states: continue, done, or error. The cont state indicates thatpullis ready to produceOutput; the done state indicates that writing terminated successfully, and thatbindwill return the written result; the error state indicates that writing terminated in failure, and thattrapwill return the write error.Writersubclasses default to the cont state.Feeding input
The
feed(I)method returns aWriterthat represents the continuation of how to write the given input object to subsequentOutputwriters.feedcan 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 muchOutputas it can, before returning anotherWriterthat represents the continuation of how to write additionalOutput. TheOutputpassed topullis only guaranteed to be valid for the duration of the method call; references to the providedOutputinstance must not be stored.Writer results
A
Writerproduces a written result of typeO, obtained via thebind()method.bindis only guaranteed to return a result when in the done state; thoughbindmay optionally make available partial results in other states. A failedWriterprovides a write error via thetrap()method.trapis only guaranteed to return an error when in the error state.Continuations
A
Writerinstance represents a continuation of how to write remainingOutput. Rather than writing a complete output in one go, aWritertakes anOutputchunk and returns anotherWriterinstance that knows how to write subsequentOutputchunks. This enables non-blocking, incremental writing that can be interrupted whenever anOutputwriter runs out of space. AWriterterminates by returning a continuation in either the done state, or the error state.done(Object)returns aWriterin the done state.error(Throwable)returns aWriterin the error state.Forking
The
fork(Object)method passes an out-of-band condition to aWriter, yielding aWritercontinuation whose behavior may be altered by the given condition. For example, a consoleWritermight support aforkcondition 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
Modifier and Type Method Description <O2> Writer<I,O2>andThen(Encoder<I,O2> that)Returns anEncoderthat continues encodingthatEncoder, after it finishes encodingthisEncoder.<O2> Writer<I,O2>andThen(Writer<I,O2> that)Returns aWriterthat continues writingthatWriter, after it finishes writingthisWriter.<I2> Writer<I2,O>asDone()Casts a doneWriterto a different input type.<I2,O2>
Writer<I2,O2>asError()Casts an erroredWriterto different input and output types.Obind()Returns the written result.static <I,O>
Writer<I,O>done()Returns aWriterin the done state thatbinds anullwritten result.static <I,O>
Writer<I,O>done(O output)Returns aWriterin the done state thatbinds the given writtenoutput.static <I,O>
Writer<I,O>error(Throwable error)Returns aWriterin the error state thattraps the given writeerror.Writer<I,O>feed(I input)Returns aWriterthat represents the continuation of how to write the giveninputobject.Writer<I,O>fork(Object condition)Returns aWritercontinuation whose behavior may be altered by the given out-of-bandcondition.booleanisCont()booleanisDone()Returnstruewhen writing has terminated successfully, andbindwill return the written result.booleanisError()Returnstruewhen writing has terminated in failure, andtrapwill return the write error.abstract Writer<I,O>pull(Output<?> output)Incrementally writes as muchoutputas possible, and returns anotherWriterthat represents the continuation of how to write additionalOutput.Writer<I,O>pull(OutputBuffer<?> output)Incrementally encodes as muchoutputbuffer data as possible, and returns anotherEncoderthat represents the continuation of how to write additional buffer data.Throwabletrap()Returns the write error.
-
-
-
Method Detail
-
done
public static <I,O> Writer<I,O> done()
Returns aWriterin the done state thatbinds anullwritten result.
-
done
public static <I,O> Writer<I,O> done(O output)
Returns aWriterin the done state thatbinds the given writtenoutput.
-
error
public static <I,O> Writer<I,O> error(Throwable error)
Returns aWriterin the error state thattraps the given writeerror.
-
isDone
public boolean isDone()
Returnstruewhen writing has terminated successfully, andbindwill return the written result. i.e. thisWriteris in the done state.
-
isError
public boolean isError()
Returnstruewhen writing has terminated in failure, andtrapwill return the write error. i.e. thisWriteris in the error state.
-
feed
public Writer<I,O> feed(I input)
Returns aWriterthat represents the continuation of how to write the giveninputobject.- Overrides:
feedin classEncoder<I,O>- Throws:
IllegalArgumentException- if thisWriterdoes not know how to write the giveninputobject.
-
pull
public abstract Writer<I,O> pull(Output<?> output)
Incrementally writes as muchoutputas possible, and returns anotherWriterthat represents the continuation of how to write additionalOutput. Ifoutputenters the done state,pullmust return a terminatedWriter, i.e. aWriterin the done state, or in the error state. The givenoutputis only guaranteed to be valid for the duration of the method call; references tooutputmust not be stored.
-
pull
public Writer<I,O> pull(OutputBuffer<?> output)
Description copied from class:EncoderIncrementally encodes as muchoutputbuffer data as possible, and returns anotherEncoderthat represents the continuation of how to write additional buffer data. IfisLastistrue, thenpullmust return a terminatedEncoder, i.e. anEncoderin the done state, or in the error state. The givenoutputbuffer is only guaranteed to be valid for the duration of the method call; references tooutputmust not be stored.
-
fork
public Writer<I,O> fork(Object condition)
Returns aWritercontinuation 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:
bindin classEncoder<I,O>- Throws:
IllegalStateException- if thisWriteris 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:
trapin classEncoder<I,O>- Throws:
IllegalStateException- if thisWriteris not in the error state.
-
asDone
public <I2> Writer<I2,O> asDone()
Casts a doneWriterto a different input type. AWriterin the done state can have any input type.- Overrides:
asDonein classEncoder<I,O>- Throws:
IllegalStateException- if thisWriteris not in the done state.
-
asError
public <I2,O2> Writer<I2,O2> asError()
Casts an erroredWriterto different input and output types. AWriterin the error state can have any input type, and any output type.- Overrides:
asErrorin classEncoder<I,O>- Throws:
IllegalStateException- if thisWriteris not in the error state.
-
andThen
public <O2> Writer<I,O2> andThen(Writer<I,O2> that)
Returns aWriterthat continues writingthatWriter, after it finishes writingthisWriter.
-
-