persolijn

an efficient router for busses
Log | Files | Refs

MessageDecoder.java (1178B)


      1 package protobuf;
      2 
      3 import java.io.ByteArrayInputStream;
      4 import java.io.InputStream;
      5 import java.util.Iterator;
      6 
      7 /**
      8  * Functional interface representing a message parser for Protobuf data.
      9  *
     10  * @param <T> The type of the parsed message.
     11  */
     12 @FunctionalInterface
     13 public interface MessageDecoder<T> {
     14 
     15     /**
     16      * Parses a byte array as a message.
     17      *
     18      * @param input The byte array containing Protobuf data.
     19      * @return The parsed message.
     20      */
     21     default T parse(byte[] input) {
     22         return parse(new ByteArrayInputStream(input), input.length);
     23     }
     24 
     25     /**
     26      * Parses an input stream with a specified length as a message.
     27      *
     28      * @param input  The input stream containing Protobuf data.
     29      * @param length The length of the Protobuf data in the input stream.
     30      * @return The parsed message.
     31      */
     32     default T parse(InputStream input, int length) {
     33         return parse(new MessageIterator(input, length));
     34     }
     35 
     36     /**
     37      * Parses Protobuf data using a custom iterator.
     38      *
     39      * @param tags The iterator providing Protobuf data elements.
     40      * @return The parsed message.
     41      */
     42     T parse(Iterator<ProtobufReader> tags);
     43 }