- Direct Known Subclasses:
Deflate,DynamicEncoder,Writer
Encoder enables efficient, interruptible encoding of network
protocols and data formats, without intermediate buffer copying.
Encoder states
An Encoder is always in one of three states: continue,
done, or error. The cont state indicates that
pull is ready to produce buffer data; the
done state indicates that encoding terminated successfully, and that
bind will return the encoded result; the error
state indicates that encoding terminated in failure, and that trap will return the encode error. Encoder subclasses default to
the cont state.
Feeding input
The feed(I) method returns an Encoder that
represents the continuation of how to encode the given input object to
subsequent output buffers. feed can be used to specify an initial
object to encode, or to change the object to be encoded.
Pulling output
The pull(OutputBuffer) method incrementally encodes as much
buffer data as it can, before returning another Encoder that
represents the continuation of how to encoded additional buffer data.
The buffer passed to pull is only guaranteed to be valid for the
duration of the method call; references to the provided buffer must not be
stored.
Encoder results
An Encoder produces an encoded 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 Encoder
provides a write error via the trap() method. trap is only
guaranteed to return an error when in the error state.
Continuations
An Encoder instance represents a continuation of how to encode
remaining buffer data. Rather than encoding a completely buffered output in
one go, an Encoder takes a buffer chunk and returns another
Encoder instance that knows how to write subsequent buffer chunks.
This enables non-blocking, incremental encoding that can be interrupted
whenever an output buffer runs out of space. An Encoder terminates
by returning a continuation in either the done state, or the
error state. done(Object) returns an
Encoder in the done state. error(Throwable)
returns an Encoder in the error state.
Forking
The fork(Object) method passes an out-of-band condition to an
Encoder, yielding an Encoder continuation whose behavior may
be altered by the given condition. For example, a text Encoder
might support a fork condition to change the character encoding.
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.asDone()Casts a doneEncoderto a different input type.<I2,O2> Encoder<I2, O2> asError()Casts an erroredEncoderto different input and output types.bind()Returns the encoded result.static <I,O> Encoder<I, O> done()Returns anEncoderin the done state thatbinds anullencoded result.static <I,O> Encoder<I, O> done(O output) Returns anEncoderin the done state thatbinds the given encodedoutput.static <I,O> Encoder<I, O> Returns anEncoderin the error state thattraps the given encodeerror.Returns anEncoderthat represents the continuation of how to encode the giveninputobject.Returns anEncodercontinuation whose behavior may be altered by the given out-of-bandcondition.booleanisCont()Returnstruewhenpullis able to produce buffer data.booleanisDone()Returnstruewhen encoding has terminated successfully, andbindwill return the encoded result.booleanisError()Returnstruewhen encoding has terminated in failure, andtrapwill return the encode error.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 encode error.
-
Constructor Details
-
Encoder
public Encoder()
-
-
Method Details
-
isCont
public boolean isCont() -
isDone
public boolean isDone()Returnstruewhen encoding has terminated successfully, andbindwill return the encoded result. i.e. thisEncoderis in the done state. -
isError
public boolean isError()Returnstruewhen encoding has terminated in failure, andtrapwill return the encode error. i.e. thisEncoderis in the error state. -
feed
Returns anEncoderthat represents the continuation of how to encode the giveninputobject.- Throws:
IllegalArgumentException- if thisEncoderdoes not know how to encode the giveninputobject.
-
pull
Incrementally 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 anEncodercontinuation whose behavior may be altered by the given out-of-bandcondition. -
bind
Returns the encoded result. Only guaranteed to return a result when in the done state.- Throws:
IllegalStateException- if thisEncoderis not in the done state.
-
trap
Returns the encode error. Only guaranteed to return an error when in the error state.- Throws:
IllegalStateException- if thisEncoderis not in the error state.
-
asDone
Casts a doneEncoderto a different input type. AnEncoderin the done state can have any input type.- Throws:
IllegalStateException- if thisEncoderis not in the done state.
-
asError
Casts an erroredEncoderto different input and output types. AnEncoderin the error state can have any input type, and any output type.- Throws:
IllegalStateException- if thisEncoderis not in the error state.
-
andThen
Returns anEncoderthat continues encodingthatEncoder, after it finishes encodingthisEncoder. -
done
Returns anEncoderin the done state thatbinds anullencoded result. -
done
Returns anEncoderin the done state thatbinds the given encodedoutput. -
error
Returns anEncoderin the error state thattraps the given encodeerror.
-