Interface StreamDecoder<I,O>

Type Parameters:
I - the input type to decode
O - the output type of being decoded
All Known Subinterfaces:
HttpDecoder<T>
All Known Implementing Classes:
ArmeriaMessageDeframer

@UnstableApi public interface StreamDecoder<I,O>
Decodes a stream of data to N objects.

Follow the below steps to decode data using StreamDecoder.

  1. 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);
           >     }
           > }
           
  2. Create a decoded StreamMessage using StreamMessage.decode(StreamDecoder) with the StreamDecoder instance.
    
           FixedLengthDecoder decoder = new FixedLengthDecoder(11);
           StreamMessage<HttpData> stream = ...;
           StreamMessage<String> decoded = stream.decode(decoder);
           
  3. 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.