Class ProtobufParser
java.lang.Object
org.openstreetmap.josm.data.protobuf.ProtobufParser
- All Implemented Interfaces:
AutoCloseable
A basic Protobuf parser
- Since:
- 17862
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byteThe default byte size (seeVAR_INT_BYTE_SIZEfor var ints)private static final byte[]private final InputStream(package private) static final byteUsed to get the most significant bytestatic final byteThe byte size for var ints (since the first byte is just an indicator for if the var int is done) -
Constructor Summary
ConstructorsConstructorDescriptionProtobufParser(byte[] bytes) Create a new parserProtobufParser(InputStream inputStream) Create a new parser -
Method Summary
Modifier and TypeMethodDescriptionRead all recordsvoidclose()static NumberconvertByteArray(byte[] bytes, byte byteSize) Convert a byte array to a number (little endian)static longconvertByteArray(byte[] bytes, byte byteSize, int start, int end) Convert a byte array to a number (little endian)static NumberconvertLong(long number) Convert a long to an appropriateNumberclassstatic longdecodeZigZag(long signed) Decode a zig-zag encoded valuestatic NumberdecodeZigZag(Number signed) Decode a zig-zag encoded valuestatic NumberencodeZigZag(Number signed) Encode a number to a zig-zag encode valuebooleanhasNext()Check if there is more data to readnext()Get the "next" WireTypeintnextByte()Get the next bytebyte[]Get the next 32 bits (WireType.THIRTY_TWO_BIT)byte[]Get the next 64 bits (WireType.SIXTY_FOUR_BIT)byte[]nextLengthDelimited(ByteArrayOutputStream byteArrayOutputStream) Get the next delimited message (WireType.LENGTH_DELIMITED)byte[]nextVarInt(ByteArrayOutputStream byteArrayOutputStream) Get the next var int (WireType#VARINT)private byte[]readNextBytes(int size) Read an arbitrary number of bytes
-
Field Details
-
BYTE_SIZE
The default byte size (seeVAR_INT_BYTE_SIZEfor var ints)- See Also:
-
VAR_INT_BYTE_SIZE
The byte size for var ints (since the first byte is just an indicator for if the var int is done)- See Also:
-
MOST_SIGNIFICANT_BYTE
Used to get the most significant byte- See Also:
-
EMPTY_BYTE_ARRAY
-
inputStream
-
-
Constructor Details
-
ProtobufParser
Create a new parser- Parameters:
bytes- The bytes to parse
-
ProtobufParser
Create a new parser- Parameters:
inputStream- The InputStream (will be fully read at this time)
-
-
Method Details
-
convertByteArray
Convert a byte array to a number (little endian)- Parameters:
bytes- The bytes to convertbyteSize- The size of the byte. For var ints, this is 7, for other ints, this is 8.- Returns:
- An appropriate
Numberclass.
-
convertByteArray
Convert a byte array to a number (little endian)- Parameters:
bytes- The bytes to convertbyteSize- The size of the byte. For var ints, this is 7, for other ints, this is 8.start- The start position in the byte arrayend- 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
Convert a long to an appropriateNumberclass- Parameters:
number- The long to convert- Returns:
- A
Number
-
decodeZigZag
Decode a zig-zag encoded value- Parameters:
signed- The value to decode- Returns:
- The decoded value
-
decodeZigZag
Decode a zig-zag encoded value- Parameters:
signed- The value to decode- Returns:
- The decoded value
- Since:
- 18695
-
encodeZigZag
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
- Specified by:
closein interfaceAutoCloseable
-
hasNext
Check if there is more data to read- Returns:
trueif there is more data to read- Throws:
IOException- - if an IO error occurs
-
next
Get the "next" WireType- Returns:
WireTypeexpected- Throws:
IOException- - if an IO error occurs
-
nextByte
Get the next byte- Returns:
- The next byte
- Throws:
IOException- - if an IO error occurs
-
nextFixed32
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
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
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
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
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
-