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 Summary
Fields Modifier and Type Field Description static byte
BYTE_SIZE
The default byte size (seeVAR_INT_BYTE_SIZE
for var ints)private InputStream
inputStream
(package private) static byte
MOST_SIGNIFICANT_BYTE
Used to get the most significant bytestatic byte
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)
-
Constructor Summary
Constructors Constructor Description ProtobufParser(byte[] bytes)
Create a new parserProtobufParser(InputStream inputStream)
Create a new parser
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Collection<ProtobufRecord>
allRecords()
Read all recordsvoid
close()
static Number
convertByteArray(byte[] bytes, byte byteSize)
Convert a byte array to a number (little endian)static Number
convertLong(long number)
Convert a long to an appropriateNumber
classstatic Number
decodeZigZag(Number signed)
Decode a zig-zag encoded valuestatic Number
encodeZigZag(Number signed)
Encode a number to a zig-zag encode valueboolean
hasNext()
Check if there is more data to readWireType
next()
Get the "next" WireTypeint
nextByte()
Get the next bytebyte[]
nextFixed32()
Get the next 32 bits (WireType.THIRTY_TWO_BIT
)byte[]
nextFixed64()
Get the next 64 bits (WireType.SIXTY_FOUR_BIT
)byte[]
nextLengthDelimited()
Get the next delimited message (WireType.LENGTH_DELIMITED
)byte[]
nextVarInt()
Get the next var int (WireType#VARINT
)private byte[]
readNextBytes(int size)
Read an arbitrary number of bytes
-
-
-
Field Detail
-
BYTE_SIZE
public static final byte BYTE_SIZE
The default byte size (seeVAR_INT_BYTE_SIZE
for var ints)- See Also:
- Constant Field Values
-
VAR_INT_BYTE_SIZE
public static final byte 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:
- Constant Field Values
-
MOST_SIGNIFICANT_BYTE
static final byte MOST_SIGNIFICANT_BYTE
Used to get the most significant byte- See Also:
- Constant Field Values
-
inputStream
private final InputStream inputStream
-
-
Constructor Detail
-
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 Detail
-
convertByteArray
public static Number convertByteArray(byte[] bytes, byte byteSize)
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
Number
class.
-
convertLong
public static Number convertLong(long number)
Convert a long to an appropriateNumber
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
-
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
public Collection<ProtobufRecord> allRecords() throws IOException
Read all records- Returns:
- A collection of all records
- Throws:
IOException
- - if an IO error occurs
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
-
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() throws IOException
Get the next delimited message (WireType.LENGTH_DELIMITED
)- Returns:
- The next length delimited message
- Throws:
IOException
- - if an IO error occurs
-
nextVarInt
public byte[] nextVarInt() throws IOException
Get the next var int (WireType#VARINT
)- 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 of the specified size, filled with bytes read (unsigned)
- Throws:
IOException
- - if an IO error occurs
-
-