Package 

Class ProtobufReader


  • 
    public final class ProtobufReader
    
                        

    Protobuf wire-format reader.

    Instances of this are backed with a {@code byte}buffer and are used to read and decode a Protobuf payload. The {@code readType} family of methods, where {@code Type} is a value type, are used to read values for a tag, while readTag is used to read the next field tag and type.

    {@code * var writer = new ProtobufWriter(); * * writer.writeUtf8(1, "Hello"); * writer.writeUtf8(2, "World"); * writer.writeInt(3, 123); * * var reader = new ProtobufReader(writer.finish()); * * while (reader.hasRemaining()) { * var tag = reader.readTag(); * * switch (tag) { * case 1 -> { * assert reader.isNextSequence(); * assert "Hello".equals(reader.readUtf8()); * }; * case 2 -> { * assert reader.isNextSequence(); * assert "World".equals(reader.readUtf8()); * }; * case 3 -> { * assert reader.isNextVarint(); * assert 123 == reader.readInt(); * }; * default -> { * // unknown tag, fail or read and discard value * reader.readAndDiscard(); * }; * } * } * }
    • Constructor Summary

      Constructors 
      Constructor Description
      ProtobufReader(ByteBuffer source) Construct a new reader with underlying input.
    • Method Summary

      Modifier and Type Method Description
      boolean hasRemaining() Test whether reader has additional input available.
      int readTag() Read next {@code varint} as field tag.
      boolean isNextVarint() Test whether next value has {@code varint} coding.
      boolean isNextFixed64() Test whether next value has {@code fixed64} coding.
      boolean isNextFixed32() Test whether next value has {@code fixed32} coding.
      boolean isNextSequence() Test whether next value has length-delimited sequence coding.
      long readUnsignedLong() Read {@code varint} coded unsigned {@code long} value.
      long readLong() Read {@code varint} coded {@code long} value.
      int readUnsignedInt() Read {@code varint} coded unsigned {@code int} value.
      int readInt() Read {@code varint} coded {@code int} value.
      boolean readBoolean() Read {@code varint} coded {@code boolean} value.
      long readFixedLong() Read {@code fixed64} coded {@code long} value.
      int readFixedInt() Read {@code fixed32} coded {@code int} value.
      double readFixedDouble() Read {@code fixed64} coded {@code double} value.
      float readFixedFloat() Read {@code fixed32} coded {@code float} value.
      ByteBuffer readByteBufferView() Read length-delimited sequence into {@code byte}buffer view.
      Array<byte> readByteArray() Read length-delimited sequence into newly allocated {@code byte} array.
      Array<long> readUnsignedLongArray() Read packed {@code varint} length-delimited sequence into newly allocated unsigned {@code * long} array.
      Array<long> readLongArray() Read packed {@code varint} length-delimited sequence into newly allocated {@code long} array.
      Array<int> readUnsignedIntArray() Read packed {@code varint} length-delimited sequence into newly allocated unsigned {@code * int} array.
      Array<int> readIntArray() Read packed {@code varint} length-delimited sequence into newly allocated {@code int} array.
      Array<boolean> readBooleanArray() Read packed {@code varint} length-delimited sequence into newly allocated {@code boolean} array.
      Array<long> readFixedLongArray() Read packed {@code fixed64} length-delimited sequence into newly allocated {@code long} array.
      Array<int> readFixedIntArray() Read packed {@code fixed32} length-delimited sequence into newly allocated {@code int} array.
      Array<double> readFixedDoubleArray() Read packed {@code fixed64} length-delimited sequence into newly allocated {@code double} array.
      Array<float> readFixedFloatArray() Read packed {@code fixed32} length-delimited sequence into newly allocated {@code float} array.
      String readUtf8() Read UTF-8 length-delimited sequence into string.
      Pair<String, String> readUtf8Pair() Read pair of UTF-8 length-delimited sequences into pair of strings.
      <M extends ProtobufMessage> M readMessage(M base) Read and merge length-delimited sequence into message.
      <M extends ProtobufMessage> List<M> readRepeatedMessage(List<M> dst, List<M> src, M base) Read and merge repeated length-delimited sequence into message.
      void readAndDiscard() Read and discard next value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ProtobufReader

        ProtobufReader(ByteBuffer source)
        Construct a new reader with underlying input.
        Parameters:
        source - input buffer
    • Method Detail

      • hasRemaining

         boolean hasRemaining()

        Test whether reader has additional input available.

      • readTag

         int readTag()

        Read next {@code varint} as field tag.

        Upon successful return, the {@code isNextType} family of methods can be used to determinethe value type associated with the field. Once the field value is read, using the respective {@code readType} method, the current field information is cleared.

      • isNextVarint

         boolean isNextVarint()

        Test whether next value has {@code varint} coding.

      • isNextFixed64

         boolean isNextFixed64()

        Test whether next value has {@code fixed64} coding.

      • isNextFixed32

         boolean isNextFixed32()

        Test whether next value has {@code fixed32} coding.

      • isNextSequence

         boolean isNextSequence()

        Test whether next value has length-delimited sequence coding.

      • readUnsignedLong

         long readUnsignedLong()

        Read {@code varint} coded unsigned {@code long} value.

      • readLong

         long readLong()

        Read {@code varint} coded {@code long} value.

      • readUnsignedInt

         int readUnsignedInt()

        Read {@code varint} coded unsigned {@code int} value.

      • readInt

         int readInt()

        Read {@code varint} coded {@code int} value.

      • readBoolean

         boolean readBoolean()

        Read {@code varint} coded {@code boolean} value.

      • readFixedLong

         long readFixedLong()

        Read {@code fixed64} coded {@code long} value.

      • readFixedInt

         int readFixedInt()

        Read {@code fixed32} coded {@code int} value.

      • readFixedDouble

         double readFixedDouble()

        Read {@code fixed64} coded {@code double} value.

      • readFixedFloat

         float readFixedFloat()

        Read {@code fixed32} coded {@code float} value.

      • readByteBufferView

         ByteBuffer readByteBufferView()

        Read length-delimited sequence into {@code byte}buffer view.

        This decodes and returns a view of the next length-delimited sequence. The view returnedshares the same underlying buffer as this reader; however, its position and limit are independent.As such, any modifications to the underlying buffer will be visible in the returned view,and vice-versa.

      • readByteArray

         Array<byte> readByteArray()

        Read length-delimited sequence into newly allocated {@code byte} array.

      • readUnsignedLongArray

         Array<long> readUnsignedLongArray()

        Read packed {@code varint} length-delimited sequence into newly allocated unsigned {@code * long} array.

      • readLongArray

         Array<long> readLongArray()

        Read packed {@code varint} length-delimited sequence into newly allocated {@code long} array.

      • readUnsignedIntArray

         Array<int> readUnsignedIntArray()

        Read packed {@code varint} length-delimited sequence into newly allocated unsigned {@code * int} array.

      • readIntArray

         Array<int> readIntArray()

        Read packed {@code varint} length-delimited sequence into newly allocated {@code int} array.

      • readBooleanArray

         Array<boolean> readBooleanArray()

        Read packed {@code varint} length-delimited sequence into newly allocated {@code boolean} array.

      • readFixedLongArray

         Array<long> readFixedLongArray()

        Read packed {@code fixed64} length-delimited sequence into newly allocated {@code long} array.

      • readFixedIntArray

         Array<int> readFixedIntArray()

        Read packed {@code fixed32} length-delimited sequence into newly allocated {@code int} array.

      • readFixedDoubleArray

         Array<double> readFixedDoubleArray()

        Read packed {@code fixed64} length-delimited sequence into newly allocated {@code double} array.

      • readFixedFloatArray

         Array<float> readFixedFloatArray()

        Read packed {@code fixed32} length-delimited sequence into newly allocated {@code float} array.

      • readUtf8

         String readUtf8()

        Read UTF-8 length-delimited sequence into string.

      • readMessage

         <M extends ProtobufMessage> M readMessage(M base)

        Read and merge length-delimited sequence into message.

        Parameters:
        base - base message
      • readRepeatedMessage

         <M extends ProtobufMessage> List<M> readRepeatedMessage(List<M> dst, List<M> src, M base)

        Read and merge repeated length-delimited sequence into message.

        Like readMessage; however, the read message is added into alist, and the list is returned. If {@code dst} is identically equal to {@code src}, thena new list is constructed, and the read message is added to it; otherwise, {@code dst} isupdated with the read message.

        Parameters:
        dst - list to operate on
        src - immutable list to compare against
        base - base message