- java.lang.Object
-
- swim.codec.Decoder<O>
-
- Direct Known Subclasses:
DynamicDecoder,Inflate,Parser
public abstract class Decoder<O> extends Object
Continuation of how to decode subsequent input buffers from a byte stream.Decoderenables efficient, interruptible decoding of network protocols and data formats, without intermediate buffer copying.Decoder states
A
Decoderis always in one of three states: continue, done, or error. The cont state indicates thatfeedis ready to consume input buffer data; the done state indicates that decoding terminated successfully, and thatbindwill return the decoded result; the error state indicates that decoding terminated in failure, and thattrapwill return the decode error.Decodersubclasses default to the cont state.Feeding input
The
feed(InputBuffer)method incrementally decodes as muchInputBufferdata as it can, before returning anotherDecoderthat represents the continuation of how to decode additionalInputBufferdata. TheInputBufferpassed tofeedis only guaranteed to be valid for the duration of the method call; references to the providedInputBufferinstance must not be stored.Decoder results
A
Decoderproduces a decoded 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 failedDecoderprovides a decode error via thetrap()method.trapis only guaranteed to return an error when in the error state.Continuations
A
Decoderinstance represents a continuation of how to decode remainingInputBufferdata. Rather than parsing a completely buffered input in one go, aDecodertakes a buffered chunk and returns anotherDecoderinstance that knows how to decode subsequent buffered chunks. This enables non-blocking, incremental decoding that can be interrupted whenever anInputBufferruns out of immediately available data. ADecoderterminates by returning a continuation in either the done state, or the error state.done(Object)returns aDecoderin the done state.error(Throwable)returns aDecoderin the error state.Immutability
A
Decodershould be immutable. Specifically, an invocation offeedshould not alter the behavior of future calls tofeedon the sameDecoderinstance. ADecodershould only mutate its internal state if it's essential to do so, such as for critical path performance reasons.Forking
The
fork(Object)method passes an out-of-band condition to aDecoder, yielding aDecodercontinuation whose behavior may be altered by the given condition. For example, a textDecodermight 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 Decoder()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <O> Decoder<O>asError()Casts an erroredDecoderto a different output type.Obind()Returns the decoded result.static <O> Decoder<O>done()Returns aDecoderin the done state thatbinds anulldecoded result.static <O> Decoder<O>done(O output)Returns aDecoderin the done state thatbinds the given decodedoutput.static <O> Decoder<O>error(Throwable error)Returns aDecoderin the error state thattraps the given decodeerror.abstract Decoder<O>feed(InputBuffer input)Incrementally decodes as muchinputbuffer data as possible, and returns anotherDecoderthat represents the continuation of how to decode additional buffer data.Decoder<O>fork(Object condition)Returns aDecodercontinuation whose behavior may be altered by the given out-of-bandcondition.booleanisCont()booleanisDone()Returnstruewhen decoding has terminated successfully, andbindwill return the decoded result.booleanisError()Returnstruewhen decoding has terminated in failure, andtrapwill return the decode error.Throwabletrap()Returns the decode error.
-
-
-
Method Detail
-
isCont
public boolean isCont()
-
isDone
public boolean isDone()
Returnstruewhen decoding has terminated successfully, andbindwill return the decoded result. i.e. thisDecoderis in the done state.
-
isError
public boolean isError()
Returnstruewhen decoding has terminated in failure, andtrapwill return the decode error. i.e. thisDecoderis in the error state.
-
feed
public abstract Decoder<O> feed(InputBuffer input)
Incrementally decodes as muchinputbuffer data as possible, and returns anotherDecoderthat represents the continuation of how to decode additional buffer data. IfisLastistrue, thenfeedmust return a terminatedDecoder, i.e. aDecoderin the done state, or in the error state. The giveninputbuffer is only guaranteed to be valid for the duration of the method call; references toinputmust not be stored.
-
fork
public Decoder<O> fork(Object condition)
Returns aDecodercontinuation whose behavior may be altered by the given out-of-bandcondition.
-
bind
public O bind()
Returns the decoded result. Only guaranteed to return a result when in the done state.- Throws:
IllegalStateException- if thisDecoderis not in the done state.
-
trap
public Throwable trap()
Returns the decode error. Only guaranteed to return an error when in the error state.- Throws:
IllegalStateException- if thisDecoderis not in the error state.
-
asError
public <O> Decoder<O> asError()
Casts an erroredDecoderto a different output type. ADecoderin the error state can have any output type.- Throws:
IllegalStateException- if thisDecoderis not in the error state.
-
done
public static <O> Decoder<O> done()
Returns aDecoderin the done state thatbinds anulldecoded result.
-
done
public static <O> Decoder<O> done(O output)
Returns aDecoderin the done state thatbinds the given decodedoutput.
-
-