Class ProtobufParser

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class ProtobufParser
    extends java.lang.Object
    implements java.lang.AutoCloseable
    A basic Protobuf parser
    Since:
    17862
    • Constructor Summary

      Constructors 
      Constructor Description
      ProtobufParser​(byte[] bytes)
      Create a new parser
      ProtobufParser​(java.io.InputStream inputStream)
      Create a new parser
    • Constructor Detail

      • ProtobufParser

        public ProtobufParser​(byte[] bytes)
        Create a new parser
        Parameters:
        bytes - The bytes to parse
      • ProtobufParser

        public ProtobufParser​(java.io.InputStream inputStream)
        Create a new parser
        Parameters:
        inputStream - The InputStream (will be fully read at this time)
    • Method Detail

      • convertByteArray

        public static java.lang.Number convertByteArray​(byte[] bytes,
                                                        byte byteSize)
        Convert a byte array to a number (little endian)
        Parameters:
        bytes - The bytes to convert
        byteSize - The size of the byte. For var ints, this is 7, for other ints, this is 8.
        Returns:
        An appropriate Number class.
      • convertByteArray

        public static long convertByteArray​(byte[] bytes,
                                            byte byteSize,
                                            int start,
                                            int end)
        Convert a byte array to a number (little endian)
        Parameters:
        bytes - The bytes to convert
        byteSize - The size of the byte. For var ints, this is 7, for other ints, this is 8.
        start - The start position in the byte array
        end - The end position in the byte array (exclusive - [start, end) )
        Returns:
        the number from the byte array. Depending upon length of time the number will be stored, narrowing may be helpful.
        Since:
        18695
      • convertLong

        public static java.lang.Number convertLong​(long number)
        Convert a long to an appropriate Number class
        Parameters:
        number - The long to convert
        Returns:
        A Number
      • decodeZigZag

        public static java.lang.Number decodeZigZag​(java.lang.Number signed)
        Decode a zig-zag encoded value
        Parameters:
        signed - The value to decode
        Returns:
        The decoded value
      • decodeZigZag

        public static long decodeZigZag​(long signed)
        Decode a zig-zag encoded value
        Parameters:
        signed - The value to decode
        Returns:
        The decoded value
        Since:
        18695
      • encodeZigZag

        public static java.lang.Number encodeZigZag​(java.lang.Number signed)
        Encode a number to a zig-zag encode value
        Parameters:
        signed - The number to encode
        Returns:
        The encoded value
      • allRecords

        public java.util.Collection<ProtobufRecordallRecords()
                                                        throws java.io.IOException
        Read all records
        Returns:
        A collection of all records
        Throws:
        java.io.IOException - - if an IO error occurs
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
      • hasNext

        public boolean hasNext()
                        throws java.io.IOException
        Check if there is more data to read
        Returns:
        true if there is more data to read
        Throws:
        java.io.IOException - - if an IO error occurs
      • next

        public WireType next()
                      throws java.io.IOException
        Get the "next" WireType
        Returns:
        WireType expected
        Throws:
        java.io.IOException - - if an IO error occurs
      • nextByte

        public int nextByte()
                     throws java.io.IOException
        Get the next byte
        Returns:
        The next byte
        Throws:
        java.io.IOException - - if an IO error occurs
      • nextFixed32

        public byte[] nextFixed32()
                           throws java.io.IOException
        Get the next 32 bits (WireType.THIRTY_TWO_BIT)
        Returns:
        a byte array of the next 32 bits (4 bytes)
        Throws:
        java.io.IOException - - if an IO error occurs
      • nextFixed64

        public byte[] nextFixed64()
                           throws java.io.IOException
        Get the next 64 bits (WireType.SIXTY_FOUR_BIT)
        Returns:
        a byte array of the next 64 bits (8 bytes)
        Throws:
        java.io.IOException - - if an IO error occurs
      • nextLengthDelimited

        public byte[] nextLengthDelimited​(java.io.ByteArrayOutputStream byteArrayOutputStream)
                                   throws java.io.IOException
        Get the next delimited message (WireType.LENGTH_DELIMITED)
        Parameters:
        byteArrayOutputStream - A reusable stream to write bytes to. This can significantly reduce the allocations (150 MB to 95 MB in a test area).
        Returns:
        The next length delimited message
        Throws:
        java.io.IOException - - if an IO error occurs
      • nextVarInt

        public byte[] nextVarInt​(java.io.ByteArrayOutputStream byteArrayOutputStream)
                          throws java.io.IOException
        Get the next var int (WireType#VARINT)
        Parameters:
        byteArrayOutputStream - A reusable stream to write bytes to. This can significantly reduce the allocations (150 MB to 95 MB in a test area).
        Returns:
        The next var int (int32, int64, uint32, uint64, bool, enum)
        Throws:
        java.io.IOException - - if an IO error occurs
      • readNextBytes

        private byte[] readNextBytes​(int size)
                              throws java.io.IOException
        Read an arbitrary number of bytes
        Parameters:
        size - The number of bytes to read
        Returns:
        a byte array filled with bytes read (unsigned)
        Throws:
        java.io.IOException - - if an IO error occurs