- 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.Decoder
enables efficient, interruptible decoding of network protocols and data formats, without intermediate buffer copying.Decoder states
A
Decoder
is always in one of three states: continue, done, or error. The cont state indicates thatfeed
is ready to consume input buffer data; the done state indicates that decoding terminated successfully, and thatbind
will return the decoded result; the error state indicates that decoding terminated in failure, and thattrap
will return the decode error.Decoder
subclasses default to the cont state.Feeding input
The
feed(InputBuffer)
method incrementally decodes as muchInputBuffer
data as it can, before returning anotherDecoder
that represents the continuation of how to decode additionalInputBuffer
data. TheInputBuffer
passed tofeed
is only guaranteed to be valid for the duration of the method call; references to the providedInputBuffer
instance must not be stored.Decoder results
A
Decoder
produces a decoded 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 failedDecoder
provides a decode error via thetrap()
method.trap
is only guaranteed to return an error when in the error state.Continuations
A
Decoder
instance represents a continuation of how to decode remainingInputBuffer
data. Rather than parsing a completely buffered input in one go, aDecoder
takes a buffered chunk and returns anotherDecoder
instance that knows how to decode subsequent buffered chunks. This enables non-blocking, incremental decoding that can be interrupted whenever anInputBuffer
runs out of immediately available data. ADecoder
terminates by returning a continuation in either the done state, or the error state.done(Object)
returns aDecoder
in the done state.error(Throwable)
returns aDecoder
in the error state.Immutability
A
Decoder
should be immutable. Specifically, an invocation offeed
should not alter the behavior of future calls tofeed
on the sameDecoder
instance. ADecoder
should 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 aDecoder
continuation whose behavior may be altered by the given condition. For example, a textDecoder
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 Decoder()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <O> Decoder<O>
asError()
Casts an erroredDecoder
to a different output type.O
bind()
Returns the decoded result.static <O> Decoder<O>
done()
Returns aDecoder
in the done state thatbind
s anull
decoded result.static <O> Decoder<O>
done(O output)
Returns aDecoder
in the done state thatbind
s the given decodedoutput
.static <O> Decoder<O>
error(Throwable error)
Returns aDecoder
in the error state thattrap
s the given decodeerror
.abstract Decoder<O>
feed(InputBuffer input)
Incrementally decodes as muchinput
buffer data as possible, and returns anotherDecoder
that represents the continuation of how to decode additional buffer data.Decoder<O>
fork(Object condition)
Returns aDecoder
continuation whose behavior may be altered by the given out-of-bandcondition
.boolean
isCont()
boolean
isDone()
Returnstrue
when decoding has terminated successfully, andbind
will return the decoded result.boolean
isError()
Returnstrue
when decoding has terminated in failure, andtrap
will return the decode error.Throwable
trap()
Returns the decode error.
-
-
-
Method Detail
-
isCont
public boolean isCont()
-
isDone
public boolean isDone()
Returnstrue
when decoding has terminated successfully, andbind
will return the decoded result. i.e. thisDecoder
is in the done state.
-
isError
public boolean isError()
Returnstrue
when decoding has terminated in failure, andtrap
will return the decode error. i.e. thisDecoder
is in the error state.
-
feed
public abstract Decoder<O> feed(InputBuffer input)
Incrementally decodes as muchinput
buffer data as possible, and returns anotherDecoder
that represents the continuation of how to decode additional buffer data. IfisLast
istrue
, thenfeed
must return a terminatedDecoder
, i.e. aDecoder
in the done state, or in the error state. The giveninput
buffer is only guaranteed to be valid for the duration of the method call; references toinput
must not be stored.
-
fork
public Decoder<O> fork(Object condition)
Returns aDecoder
continuation 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 thisDecoder
is 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 thisDecoder
is not in the error state.
-
asError
public <O> Decoder<O> asError()
Casts an erroredDecoder
to a different output type. ADecoder
in the error state can have any output type.- Throws:
IllegalStateException
- if thisDecoder
is not in the error state.
-
done
public static <O> Decoder<O> done()
Returns aDecoder
in the done state thatbind
s anull
decoded result.
-
done
public static <O> Decoder<O> done(O output)
Returns aDecoder
in the done state thatbind
s the given decodedoutput
.
-
-