Class Num


  • public final class Num
    extends Object

    This class provides operations to compress and decompress 4-byte integer values in byte arrays in order to save memory.

    The first two bits of a Num array indicate the range of the compressed number:

    • 00: the number (0x00-0x3F) is encoded in the remaining 6 bits of the current byte
    • 01: the number (0x40-0x3FFF) is encoded in 14 bits of the current and the following byte
    • 10: the number (0x4000-0x3FFFFFFF) is encoded in 30 bits of the current and the following three bytes
    • 11: the number (0x40000000-0xFFFFFFFF) is encoded in 32 bits of the following four bytes
    Author:
    BaseX Team 2005-23, BSD License, Christian Gruen
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] add​(byte[] array, int value)
      Compresses and adds a value to the specified array and returns the resulting array.
      static int get​(byte[] array, int pos)
      Decompresses and returns a value from the specified byte array.
      static int length​(byte[] array, int pos)
      Returns the compressed length of the value at the specified position.
      static int length​(int v)
      Returns the compressed length of the specified value.
      static byte[] newNum​(int value)
      Creates a new number array, in which the first four bytes contain the number of occupied bytes.
      static byte[] num​(int value)
      Creates a compressed representation of the specified value.
      static int set​(byte[] array, int value)
      Compresses and stores an integer value at the beginning of the byte array.
      static int size​(byte[] array)
      Returns the length value of the specified array, stored in the first four bytes.
      static void size​(byte[] array, int length)
      Stores the specified length value in the first bytes of the specified array.
      static String toString​(byte[] num)
      Returns a string representation of the specified number array.
    • Method Detail

      • newNum

        public static byte[] newNum​(int value)
        Creates a new number array, in which the first four bytes contain the number of occupied bytes.
        Parameters:
        value - initial value to be compressed and stored
        Returns:
        new number array
      • num

        public static byte[] num​(int value)
        Creates a compressed representation of the specified value.
        Parameters:
        value - value to be compressed
        Returns:
        new number array
      • add

        public static byte[] add​(byte[] array,
                                 int value)
        Compresses and adds a value to the specified array and returns the resulting array.
        Parameters:
        array - input array
        value - value to be added
        Returns:
        resulting array (can be the same as input array)
      • get

        public static int get​(byte[] array,
                              int pos)
        Decompresses and returns a value from the specified byte array.
        Parameters:
        array - array
        pos - position where the value is found
        Returns:
        decompressed value
      • set

        public static int set​(byte[] array,
                              int value)
        Compresses and stores an integer value at the beginning of the byte array.
        Parameters:
        array - array
        value - value to be stored
        Returns:
        length
      • size

        public static int size​(byte[] array)
        Returns the length value of the specified array, stored in the first four bytes.
        Parameters:
        array - input array
        Returns:
        array length
      • size

        public static void size​(byte[] array,
                                int length)
        Stores the specified length value in the first bytes of the specified array.
        Parameters:
        array - input array
        length - length to be stored
      • length

        public static int length​(byte[] array,
                                 int pos)
        Returns the compressed length of the value at the specified position.
        Parameters:
        array - array
        pos - position where the value is found
        Returns:
        value length
      • length

        public static int length​(int v)
        Returns the compressed length of the specified value.
        Parameters:
        v - integer value
        Returns:
        value length
      • toString

        public static String toString​(byte[] num)
        Returns a string representation of the specified number array.
        Parameters:
        num - numbers
        Returns:
        string