Class ProtobufParser
- java.lang.Object
-
- org.openstreetmap.josm.data.protobuf.ProtobufParser
-
- All Implemented Interfaces:
java.lang.AutoCloseable
public class ProtobufParser extends java.lang.Object implements java.lang.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 static byte[]
EMPTY_BYTE_ARRAY
private java.io.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(java.io.InputStream inputStream)
Create a new parser
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Collection<ProtobufRecord>
allRecords()
Read all recordsvoid
close()
static java.lang.Number
convertByteArray(byte[] bytes, byte byteSize)
Convert a byte array to a number (little endian)static long
convertByteArray(byte[] bytes, byte byteSize, int start, int end)
Convert a byte array to a number (little endian)static java.lang.Number
convertLong(long number)
Convert a long to an appropriateNumber
classstatic long
decodeZigZag(long signed)
Decode a zig-zag encoded valuestatic java.lang.Number
decodeZigZag(java.lang.Number signed)
Decode a zig-zag encoded valuestatic java.lang.Number
encodeZigZag(java.lang.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(java.io.ByteArrayOutputStream byteArrayOutputStream)
Get the next delimited message (WireType.LENGTH_DELIMITED
)byte[]
nextVarInt(java.io.ByteArrayOutputStream byteArrayOutputStream)
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
-
EMPTY_BYTE_ARRAY
private static final byte[] EMPTY_BYTE_ARRAY
-
inputStream
private final java.io.InputStream inputStream
-
-
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 convertbyteSize
- 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 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
public static java.lang.Number convertLong(long number)
Convert a long to an appropriateNumber
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<ProtobufRecord> allRecords() 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 interfacejava.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
-
-