Class Utils

java.lang.Object
org.apache.hadoop.io.file.tfile.Utils

@Public @Evolving public final class Utils extends Object
Supporting Utility classes used by TFile, and shared by users of TFile.
  • Method Details

    • writeVInt

      public static void writeVInt(DataOutput out, int n) throws IOException
      Encoding an integer into a variable-length encoding format. Synonymous to Utils#writeVLong(out, n).
      Parameters:
      out - output stream
      n - The integer to be encoded
      Throws:
      IOException - raised on errors performing I/O.
      See Also:
    • writeVLong

      public static void writeVLong(DataOutput out, long n) throws IOException
      Encoding a Long integer into a variable-length encoding format.
      • if n in [-32, 127): encode in one byte with the actual value. Otherwise,
      • if n in [-20*2^8, 20*2^8): encode in two bytes: byte[0] = n/256 - 52; byte[1]=n&0xff. Otherwise,
      • if n IN [-16*2^16, 16*2^16): encode in three bytes: byte[0]=n/2^16 - 88; byte[1]=(n>>8)&0xff; byte[2]=n&0xff. Otherwise,
      • if n in [-8*2^24, 8*2^24): encode in four bytes: byte[0]=n/2^24 - 112; byte[1] = (n>>16)&0xff; byte[2] = (n>>8)&0xff; byte[3]=n&0xff. Otherwise:
      • if n in [-2^31, 2^31): encode in five bytes: byte[0]=-125; byte[1] = (n>>24)&0xff; byte[2]=(n>>16)&0xff; byte[3]=(n>>8)&0xff; byte[4]=n&0xff;
      • if n in [-2^39, 2^39): encode in six bytes: byte[0]=-124; byte[1] = (n>>32)&0xff; byte[2]=(n>>24)&0xff; byte[3]=(n>>16)&0xff; byte[4]=(n>>8)&0xff; byte[5]=n&0xff
      • if n in [-2^47, 2^47): encode in seven bytes: byte[0]=-123; byte[1] = (n>>40)&0xff; byte[2]=(n>>32)&0xff; byte[3]=(n>>24)&0xff; byte[4]=(n>>16)&0xff; byte[5]=(n>>8)&0xff; byte[6]=n&0xff;
      • if n in [-2^55, 2^55): encode in eight bytes: byte[0]=-122; byte[1] = (n>>48)&0xff; byte[2] = (n>>40)&0xff; byte[3]=(n>>32)&0xff; byte[4]=(n>>24)&0xff; byte[5]= (n>>16)&0xff; byte[6]=(n>>8)&0xff; byte[7]=n&0xff;
      • if n in [-2^63, 2^63): encode in nine bytes: byte[0]=-121; byte[1] = (n>>54)&0xff; byte[2] = (n>>48)&0xff; byte[3] = (n>>40)&0xff; byte[4]=(n>>32)&0xff; byte[5]=(n>>24)&0xff; byte[6]=(n>>16)&0xff; byte[7]= (n>>8)&0xff; byte[8]=n&0xff;
      Parameters:
      out - output stream
      n - the integer number
      Throws:
      IOException - raised on errors performing I/O.
    • readVInt

      public static int readVInt(DataInput in) throws IOException
      Decoding the variable-length integer. Synonymous to (int)Utils#readVLong(in).
      Parameters:
      in - input stream
      Returns:
      the decoded integer
      Throws:
      IOException - raised on errors performing I/O.
      See Also:
    • readVLong

      public static long readVLong(DataInput in) throws IOException
      Decoding the variable-length integer. Suppose the value of the first byte is FB, and the following bytes are NB[*].
      • if (FB >= -32), return (long)FB;
      • if (FB in [-72, -33]), return (FB+52)<<8 + NB[0]&0xff;
      • if (FB in [-104, -73]), return (FB+88)<<16 + (NB[0]&0xff)<<8 + NB[1]&0xff;
      • if (FB in [-120, -105]), return (FB+112)<<24 + (NB[0]&0xff) <<16 + (NB[1]&0xff)<<8 + NB[2]&0xff;
      • if (FB in [-128, -121]), return interpret NB[FB+129] as a signed big-endian integer.
      Parameters:
      in - input stream
      Returns:
      the decoded long integer.
      Throws:
      IOException - raised on errors performing I/O.
    • writeString

      public static void writeString(DataOutput out, String s) throws IOException
      Write a String as a VInt n, followed by n Bytes as in Text format.
      Parameters:
      out - out.
      s - s.
      Throws:
      IOException - raised on errors performing I/O.
    • readString

      public static String readString(DataInput in) throws IOException
      Read a String as a VInt n, followed by n Bytes in Text format.
      Parameters:
      in - The input stream.
      Returns:
      The string
      Throws:
      IOException - raised on errors performing I/O.
    • lowerBound

      public static <T> int lowerBound(List<? extends T> list, T key, Comparator<? super T> cmp)
      Lower bound binary search. Find the index to the first element in the list that compares greater than or equal to key.
      Type Parameters:
      T - Type of the input key.
      Parameters:
      list - The list
      key - The input key.
      cmp - Comparator for the key.
      Returns:
      The index to the desired element if it exists; or list.size() otherwise.
    • upperBound

      public static <T> int upperBound(List<? extends T> list, T key, Comparator<? super T> cmp)
      Upper bound binary search. Find the index to the first element in the list that compares greater than the input key.
      Type Parameters:
      T - Type of the input key.
      Parameters:
      list - The list
      key - The input key.
      cmp - Comparator for the key.
      Returns:
      The index to the desired element if it exists; or list.size() otherwise.
    • lowerBound

      public static <T> int lowerBound(List<? extends Comparable<? super T>> list, T key)
      Lower bound binary search. Find the index to the first element in the list that compares greater than or equal to key.
      Type Parameters:
      T - Type of the input key.
      Parameters:
      list - The list
      key - The input key.
      Returns:
      The index to the desired element if it exists; or list.size() otherwise.
    • upperBound

      public static <T> int upperBound(List<? extends Comparable<? super T>> list, T key)
      Upper bound binary search. Find the index to the first element in the list that compares greater than the input key.
      Type Parameters:
      T - Type of the input key.
      Parameters:
      list - The list
      key - The input key.
      Returns:
      The index to the desired element if it exists; or list.size() otherwise.