Class VByte


  • public class VByte
    extends Object
    Variable byte encoding for numbers.

    A variable number of bytes is used to encode (unsigned) numeric values, the first bit (MSB) of each byte indicates if there are more bytes to read, the other 7 bits are used to encode the value.

    In this implementation, the MSB is set to 1 if this byte is the last one.

    E.g: 10000001 is value 1, 00000001 10000001 is 128 (decimal). Note that the value is stored little-endian, so in this example 10000001 00000001.

    Author:
    Bart.Hanssens
    See Also:
    Variable byte codes
    • Constructor Detail

      • VByte

        public VByte()
    • Method Detail

      • hasNext

        public static boolean hasNext​(byte b)
        Checks if the most significant bit is set. If this bit is zero, then the next byte must also be read to decode the number.
        Parameters:
        b -
        Returns:
        true if there is a next byte
      • decode

        public static long decode​(byte[] bytes,
                                  int len)
        Decode a series of encoded bytes, with a maximum of 8 bytes
        Parameters:
        bytes - byte array
        len - number of bytes to decode
        Returns:
        long value
      • decode

        public static long decode​(InputStream is)
                           throws IOException
        Decode a maximum of 8 bytes from the input stream.
        Parameters:
        is - input stream
        Returns:
        decode value
        Throws:
        IOException
      • decodeFrom

        public static long decodeFrom​(byte[] b,
                                      int start)
                               throws IOException
        Decode a maximum of 8 bytes from a byte array.
        Parameters:
        b - byte array
        start - starting position
        Returns:
        decode value
        Throws:
        IOException
      • encodedLength

        public static int encodedLength​(long value)
        Calculate the number of bytes needed for encoding a value
        Parameters:
        value - numeric value
        Returns:
        number of bytes