Interface StreamDecoder<I,O>
- Type Parameters:
I
- the input type to decodeO
- the output type of being decoded
- All Known Subinterfaces:
HttpDecoder<T>
- All Known Implementing Classes:
ArmeriaMessageDeframer
Decodes a stream of data to N objects.
Follow the below steps to decode data using StreamDecoder
.
- Implement your decoding logic in
StreamDecoder
.> class FixedLengthDecoder implements StreamDecoder<String> { > private final int length; > > FixedLengthDecoder(int length) { > this.length = length; > } > > @Override > public ByteBuf toByteBuf(HttpData in) { > return in.byteBuf(); > } > > @Override > public void process(StreamDecoderInput in, StreamDecoderOutput<String> out) { > int remaining = in.readableBytes(); > if (remaining < length) { > // The input is not enough to process. Waiting for more data. > return; > } > > do { > // Read data from 'StreamDecoderInput' and > // write the processed result to 'StreamDecoderOutput'. > ByteBuf buf = in.readBytes(length); > out.add(buf.toString(StandardCharsets.UTF_8)); > // Should release the returned 'ByteBuf' > buf.release(); > remaining -= length; > } while (remaining >= length); > } > }
- Create a decoded
StreamMessage
usingStreamMessage.decode(StreamDecoder)
with theStreamDecoder
instance.FixedLengthDecoder decoder = new FixedLengthDecoder(11); StreamMessage<HttpData> stream = ...; StreamMessage<String> decoded = stream.decode(decoder);
- Subscribe to the
Publisher
of the decoded data and connect to your business logic.import reactor.core.publisher.Flux; Flux.from(decoded).map(...); // Consume and manipulate the decoded data.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
process
(StreamDecoderInput in, StreamDecoderOutput<O> out) Decodes a stream of data to N objects.default void
Invoked when the input is fully consumed.default void
processOnError
(Throwable cause) Invoked when aThrowable
is raised while deframing.Converts the specifiedStreamDecoder
type object into aByteBuf
that is added toStreamDecoderInput
.
-
Method Details
-
toByteBuf
Converts the specifiedStreamDecoder
type object into aByteBuf
that is added toStreamDecoderInput
. -
process
Decodes a stream of data to N objects. This method will be called whenever an object is signaled fromPublisher
.- Throws:
Exception
-
processOnComplete
Invoked when the input is fully consumed.- Throws:
Exception
-
processOnError
Invoked when aThrowable
is raised while deframing.
-