Output 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 an Output writer. Output
tokens are modeled as primitive ints, commonly representing Unicode
code points, or raw octets. Each Writer 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 that
pull is ready to produce Output; the
done state indicates that writing terminated successfully, and that
bind will return the written result; the error
state indicates that writing terminated in failure, and that trap will return the write error. Writer subclasses default to the
cont state.
Feeding input
The feed(I) method returns a Writer that
represents the continuation of how to write the given input object to
subsequent Output 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 much
Output as it can, before returning another Writer that represents
the continuation of how to write additional Output. The
Output passed to pull is only guaranteed to be valid for the
duration of the method call; references to the provided Output
instance must not be stored.
Writer results
A Writer produces a written result of type O, obtained
via the bind() method. bind is only guaranteed to return a
result when in the done state; though bind may optionally
make available partial results in other states. A failed Writer
provides a write error via the trap() 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
remaining Output. Rather than writing a complete output in one go,
a Writer takes an Output chunk and returns another
Writer instance that knows how to write subsequent Output chunks.
This enables non-blocking, incremental writing that can be interrupted
whenever an Output writer runs out of space. A Writer
terminates by returning a continuation in either the done state,
or the error state. done(Object) returns a
Writer in the done state. error(Throwable) returns
a Writer in the error state.
Forking
The fork(Object) method passes an out-of-band condition to a
Writer, yielding a Writer continuation whose behavior may
be altered by the given condition. For example, a console Writer
might support a fork condition that changes the color and style of
printed text. The types of conditions accepted by fork, and their
intended semantics, are implementation defined.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns anEncoderthat continues encodingthatEncoder, after it finishes encodingthisEncoder.Returns aWriterthat continues writingthatWriter, after it finishes writingthisWriter.asDone()Casts a doneWriterto a different input type.<I2,O2> Writer<I2, O2> asError()Casts an erroredWriterto different input and output types.bind()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> Returns aWriterin the error state thattraps the given writeerror.Returns aWriterthat represents the continuation of how to write the giveninputobject.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.Incrementally writes as muchoutputas possible, and returns anotherWriterthat represents the continuation of how to write additionalOutput.pull(OutputBuffer<?> output) Incrementally encodes as muchoutputbuffer data as possible, and returns anotherEncoderthat represents the continuation of how to write additional buffer data.trap()Returns the write error.
-
Constructor Details
-
Writer
public Writer()
-
-
Method Details
-
isCont
public boolean isCont() -
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
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
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
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
Returns aWritercontinuation whose behavior may be altered by the given out-of-bandcondition. -
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
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
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
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
Returns aWriterthat continues writingthatWriter, after it finishes writingthisWriter. -
andThen
Description copied from class:EncoderReturns anEncoderthat continues encodingthatEncoder, after it finishes encodingthisEncoder. -
done
Returns aWriterin the done state thatbinds anullwritten result. -
done
Returns aWriterin the done state thatbinds the given writtenoutput. -
error
Returns aWriterin the error state thattraps the given writeerror.
-