- java.lang.Object
-
- swim.codec.Encoder<I,O>
-
- Direct Known Subclasses:
Deflate
,DynamicEncoder
,Writer
public abstract class Encoder<I,O> extends Object
Continuation of how encode subsequent output buffers for a byte stream.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 thatpull
is ready to produce buffer data; the done state indicates that encoding terminated successfully, and thatbind
will return the encoded result; the error state indicates that encoding terminated in failure, and thattrap
will return the encode error.Encoder
subclasses default to the cont state.Feeding input
The
feed(I)
method returns anEncoder
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 anotherEncoder
that represents the continuation of how to encoded additional buffer data. The buffer passed topull
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 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 failedEncoder
provides a write error via thetrap()
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, anEncoder
takes a buffer chunk and returns anotherEncoder
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. AnEncoder
terminates by returning a continuation in either the done state, or the error state.done(Object)
returns anEncoder
in the done state.error(Throwable)
returns anEncoder
in the error state.Forking
The
fork(Object)
method passes an out-of-band condition to anEncoder
, yielding anEncoder
continuation whose behavior may be altered by the given condition. For example, a textEncoder
might support afork
condition to change the character encoding. The types of conditions accepted byfork
, and their intended semantics, are implementation defined.
-
-
Constructor Summary
Constructors Constructor Description Encoder()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <O2> Encoder<I,O2>
andThen(Encoder<I,O2> that)
Returns anEncoder
that continues encodingthat
Encoder
, after it finishes encodingthis
Encoder
.<I2> Encoder<I2,O>
asDone()
Casts a doneEncoder
to a different input type.<I2,O2>
Encoder<I2,O2>asError()
Casts an erroredEncoder
to different input and output types.O
bind()
Returns the encoded result.static <I,O>
Encoder<I,O>done()
Returns anEncoder
in the done state thatbind
s anull
encoded result.static <I,O>
Encoder<I,O>done(O output)
Returns anEncoder
in the done state thatbind
s the given encodedoutput
.static <I,O>
Encoder<I,O>error(Throwable error)
Returns anEncoder
in the error state thattrap
s the given encodeerror
.Encoder<I,O>
feed(I input)
Returns anEncoder
that represents the continuation of how to encode the giveninput
object.Encoder<I,O>
fork(Object condition)
Returns anEncoder
continuation whose behavior may be altered by the given out-of-bandcondition
.boolean
isCont()
Returnstrue
whenpull
is able to produce buffer data.boolean
isDone()
Returnstrue
when encoding has terminated successfully, andbind
will return the encoded result.boolean
isError()
Returnstrue
when encoding has terminated in failure, andtrap
will return the encode error.abstract Encoder<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 encode error.
-
-
-
Method Detail
-
isCont
public boolean isCont()
-
isDone
public boolean isDone()
Returnstrue
when encoding has terminated successfully, andbind
will return the encoded result. i.e. thisEncoder
is in the done state.
-
isError
public boolean isError()
Returnstrue
when encoding has terminated in failure, andtrap
will return the encode error. i.e. thisEncoder
is in the error state.
-
feed
public Encoder<I,O> feed(I input)
Returns anEncoder
that represents the continuation of how to encode the giveninput
object.- Throws:
IllegalArgumentException
- if thisEncoder
does not know how to encode the giveninput
object.
-
pull
public abstract Encoder<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. 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 Encoder<I,O> fork(Object condition)
Returns anEncoder
continuation whose behavior may be altered by the given out-of-bandcondition
.
-
bind
public O bind()
Returns the encoded result. Only guaranteed to return a result when in the done state.- Throws:
IllegalStateException
- if thisEncoder
is not in the done state.
-
trap
public Throwable trap()
Returns the encode error. Only guaranteed to return an error when in the error state.- Throws:
IllegalStateException
- if thisEncoder
is not in the error state.
-
asDone
public <I2> Encoder<I2,O> asDone()
Casts a doneEncoder
to a different input type. AnEncoder
in the done state can have any input type.- Throws:
IllegalStateException
- if thisEncoder
is not in the done state.
-
asError
public <I2,O2> Encoder<I2,O2> asError()
Casts an erroredEncoder
to different input and output types. AnEncoder
in the error state can have any input type, and any output type.- Throws:
IllegalStateException
- if thisEncoder
is not in the error state.
-
andThen
public <O2> Encoder<I,O2> andThen(Encoder<I,O2> that)
Returns anEncoder
that continues encodingthat
Encoder
, after it finishes encodingthis
Encoder
.
-
done
public static <I,O> Encoder<I,O> done()
Returns anEncoder
in the done state thatbind
s anull
encoded result.
-
done
public static <I,O> Encoder<I,O> done(O output)
Returns anEncoder
in the done state thatbind
s the given encodedoutput
.
-
-