Package io.opencensus.implcore.internal
Class VarInt
java.lang.Object
io.opencensus.implcore.internal.VarInt
Common methods to encode and decode varints and varlongs into ByteBuffers and arrays.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Maximum encoded size of 32-bit positive integers (in bytes)static final int
maximum encoded size of 64-bit longs, and negative 32-bit ints (in bytes) -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic int
getVarInt
(byte[] src, int offset, int[] dst) Reads a varint from src, places its values into the first element of dst and returns the offset in to src of the first byte after the varint.static int
getVarInt
(InputStream inputStream) Reads a varint from the given InputStream and returns the decoded value as an int.static int
getVarInt
(ByteBuffer src) Reads a varint from the current position of the given ByteBuffer and returns the decoded value as 32 bit integer.static long
getVarLong
(ByteBuffer src) Reads an up to 64 bit long varint from the current position of the given ByteBuffer and returns the decoded value as long.static int
putVarInt
(int v, byte[] sink, int offset) Encodes an integer in a variable-length encoding, 7 bits per byte, into a destination byte[], following the protocol buffer convention.static void
putVarInt
(int v, OutputStream outputStream) Encodes an integer in a variable-length encoding, 7 bits per byte, and writes it to the given OutputStream.static void
putVarInt
(int v, ByteBuffer sink) Encodes an integer in a variable-length encoding, 7 bits per byte, to a ByteBuffer sink.static void
putVarLong
(long v, ByteBuffer sink) Encodes a long integer in a variable-length encoding, 7 bits per byte, to a ByteBuffer sink.static int
varIntSize
(int i) Returns the encoding size in bytes of its input value.static int
varLongSize
(long v) Returns the encoding size in bytes of its input value.
-
Field Details
-
MAX_VARINT_SIZE
public static final int MAX_VARINT_SIZEMaximum encoded size of 32-bit positive integers (in bytes)- See Also:
-
MAX_VARLONG_SIZE
public static final int MAX_VARLONG_SIZEmaximum encoded size of 64-bit longs, and negative 32-bit ints (in bytes)- See Also:
-
-
Constructor Details
-
VarInt
private VarInt()
-
-
Method Details
-
varIntSize
public static int varIntSize(int i) Returns the encoding size in bytes of its input value.- Parameters:
i
- the integer to be measured- Returns:
- the encoding size in bytes of its input value
-
getVarInt
public static int getVarInt(byte[] src, int offset, int[] dst) Reads a varint from src, places its values into the first element of dst and returns the offset in to src of the first byte after the varint.- Parameters:
src
- source buffer to retrieve fromoffset
- offset within srcdst
- the resulting int value- Returns:
- the updated offset after reading the varint
-
putVarInt
public static int putVarInt(int v, byte[] sink, int offset) Encodes an integer in a variable-length encoding, 7 bits per byte, into a destination byte[], following the protocol buffer convention.- Parameters:
v
- the int value to write to sinksink
- the sink buffer to write tooffset
- the offset within sink to begin writing- Returns:
- the updated offset after writing the varint
-
getVarInt
Reads a varint from the current position of the given ByteBuffer and returns the decoded value as 32 bit integer.The position of the buffer is advanced to the first byte after the decoded varint.
- Parameters:
src
- the ByteBuffer to get the var int from- Returns:
- The integer value of the decoded varint
-
putVarInt
Encodes an integer in a variable-length encoding, 7 bits per byte, to a ByteBuffer sink.- Parameters:
v
- the value to encodesink
- the ByteBuffer to add the encoded value
-
getVarInt
Reads a varint from the given InputStream and returns the decoded value as an int.- Parameters:
inputStream
- the InputStream to read from- Throws:
IOException
-
putVarInt
Encodes an integer in a variable-length encoding, 7 bits per byte, and writes it to the given OutputStream.- Parameters:
v
- the value to encodeoutputStream
- the OutputStream to write to- Throws:
IOException
-
varLongSize
public static int varLongSize(long v) Returns the encoding size in bytes of its input value.- Parameters:
v
- the long to be measured- Returns:
- the encoding size in bytes of a given long value.
-
getVarLong
Reads an up to 64 bit long varint from the current position of the given ByteBuffer and returns the decoded value as long.The position of the buffer is advanced to the first byte after the decoded varint.
- Parameters:
src
- the ByteBuffer to get the var int from- Returns:
- The integer value of the decoded long varint
-
putVarLong
Encodes a long integer in a variable-length encoding, 7 bits per byte, to a ByteBuffer sink.- Parameters:
v
- the value to encodesink
- the ByteBuffer to add the encoded value
-