Class ProtobufParser

java.lang.Object
org.openstreetmap.josm.data.protobuf.ProtobufParser
All Implemented Interfaces:
AutoCloseable

public class ProtobufParser extends Object implements AutoCloseable
A basic Protobuf parser
Since:
17862
  • Field Details

  • Constructor Details

    • ProtobufParser

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

      public ProtobufParser(InputStream inputStream)
      Create a new parser
      Parameters:
      inputStream - The InputStream (will be fully read at this time)
  • Method Details

    • convertByteArray

      public static 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 Number convertLong(long number)
      Convert a long to an appropriate Number class
      Parameters:
      number - The long to convert
      Returns:
      A Number
    • decodeZigZag

      public static Number decodeZigZag(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 Number encodeZigZag(Number signed)
      Encode a number to a zig-zag encode value
      Parameters:
      signed - The number to encode
      Returns:
      The encoded value
    • allRecords

      Read all records
      Returns:
      A collection of all records
      Throws:
      IOException - - if an IO error occurs
    • close

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

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

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

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

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

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

      public byte[] nextLengthDelimited(ByteArrayOutputStream byteArrayOutputStream) throws 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:
      IOException - - if an IO error occurs
    • nextVarInt

      public byte[] nextVarInt(ByteArrayOutputStream byteArrayOutputStream) throws 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:
      IOException - - if an IO error occurs
    • readNextBytes

      private byte[] readNextBytes(int size) throws 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:
      IOException - - if an IO error occurs