- 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.Encoderenables efficient, interruptible encoding of network protocols and data formats, without intermediate buffer copying.Encoder states
An
Encoderis always in one of three states: continue, done, or error. The cont state indicates thatpullis ready to produce buffer data; the done state indicates that encoding terminated successfully, and thatbindwill return the encoded result; the error state indicates that encoding terminated in failure, and thattrapwill return the encode error.Encodersubclasses default to the cont state.Feeding input
The
feed(I)method returns anEncoderthat represents the continuation of how to encode the given input object to subsequent output buffers.feedcan 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 anotherEncoderthat represents the continuation of how to encoded additional buffer data. The buffer passed topullis only guaranteed to be valid for the duration of the method call; references to the provided buffer must not be stored.Encoder results
An
Encoderproduces an encoded 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 failedEncoderprovides a write error via thetrap()method.trapis only guaranteed to return an error when in the error state.Continuations
An
Encoderinstance represents a continuation of how to encode remaining buffer data. Rather than encoding a completely buffered output in one go, anEncodertakes a buffer chunk and returns anotherEncoderinstance 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. AnEncoderterminates by returning a continuation in either the done state, or the error state.done(Object)returns anEncoderin the done state.error(Throwable)returns anEncoderin the error state.Forking
The
fork(Object)method passes an out-of-band condition to anEncoder, yielding anEncodercontinuation whose behavior may be altered by the given condition. For example, a textEncodermight support aforkcondition 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 anEncoderthat continues encodingthatEncoder, after it finishes encodingthisEncoder.<I2> Encoder<I2,O>asDone()Casts a doneEncoderto a different input type.<I2,O2>
Encoder<I2,O2>asError()Casts an erroredEncoderto different input and output types.Obind()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>error(Throwable error)Returns anEncoderin the error state thattraps the given encodeerror.Encoder<I,O>feed(I input)Returns anEncoderthat represents the continuation of how to encode the giveninputobject.Encoder<I,O>fork(Object condition)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.abstract Encoder<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 encode error.
-
-
-
Method Detail
-
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
public Encoder<I,O> feed(I input)
Returns anEncoderthat represents the continuation of how to encode the giveninputobject.- Throws:
IllegalArgumentException- if thisEncoderdoes not know how to encode the giveninputobject.
-
pull
public abstract Encoder<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. 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 Encoder<I,O> fork(Object condition)
Returns anEncodercontinuation 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 thisEncoderis 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 thisEncoderis not in the error state.
-
asDone
public <I2> Encoder<I2,O> 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
public <I2,O2> Encoder<I2,O2> 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
public <O2> Encoder<I,O2> andThen(Encoder<I,O2> that)
Returns anEncoderthat continues encodingthatEncoder, after it finishes encodingthisEncoder.
-
done
public static <I,O> Encoder<I,O> done()
Returns anEncoderin the done state thatbinds anullencoded result.
-
done
public static <I,O> Encoder<I,O> done(O output)
Returns anEncoderin the done state thatbinds the given encodedoutput.
-
-