Class VarInt

java.lang.Object
io.opencensus.implcore.internal.VarInt

public class VarInt extends Object
Common methods to encode and decode varints and varlongs into ByteBuffers and arrays.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static 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
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    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.
    static int
    getVarInt(InputStream inputStream)
    Reads a varint from the given InputStream and returns the decoded value as an int.
    static int
    Reads a varint from the current position of the given ByteBuffer and returns the decoded value as 32 bit integer.
    static long
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MAX_VARINT_SIZE

      public static final int MAX_VARINT_SIZE
      Maximum encoded size of 32-bit positive integers (in bytes)
      See Also:
    • MAX_VARLONG_SIZE

      public static final int MAX_VARLONG_SIZE
      maximum 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 from
      offset - offset within src
      dst - 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 sink
      sink - the sink buffer to write to
      offset - the offset within sink to begin writing
      Returns:
      the updated offset after writing the varint
    • getVarInt

      public 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.

      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

      public static void putVarInt(int v, ByteBuffer sink)
      Encodes an integer in a variable-length encoding, 7 bits per byte, to a ByteBuffer sink.
      Parameters:
      v - the value to encode
      sink - the ByteBuffer to add the encoded value
    • getVarInt

      public static int getVarInt(InputStream inputStream) throws IOException
      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

      public static void putVarInt(int v, OutputStream outputStream) throws IOException
      Encodes an integer in a variable-length encoding, 7 bits per byte, and writes it to the given OutputStream.
      Parameters:
      v - the value to encode
      outputStream - 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

      public 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.

      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

      public static void putVarLong(long v, ByteBuffer sink)
      Encodes a long integer in a variable-length encoding, 7 bits per byte, to a ByteBuffer sink.
      Parameters:
      v - the value to encode
      sink - the ByteBuffer to add the encoded value