Code example for DataInput

Methods: readBytereadFullyreadIntreadUnsignedBytereadUnsignedShort

0
   * @return the decoded long integer. 
   * @throws IOException 
   */ 
   
  public static long readVLong(DataInput in) throws IOException {
    int firstByte = in.readByte();
    if (firstByte >= -32) {
      return firstByte;
    } 
     
    switch ((firstByte + 128) / 8) {
      case 11: 
      case 10: 
      case 9: 
      case 8: 
      case 7: 
        return ((firstByte + 52) << 8) | in.readUnsignedByte();
      case 6: 
      case 5: 
      case 4: 
      case 3: 
        return ((firstByte + 88) << 16) | in.readUnsignedShort();
      case 2: 
      case 1: 
        return ((firstByte + 112) << 24) | (in.readUnsignedShort() << 8) | in.readUnsignedByte();
      case 0: 
        int len = firstByte + 129;
        switch (len) {
          case 4: 
            return in.readInt();
          case 5: 
            return ((long) in.readInt()) << 8 | in.readUnsignedByte();
          case 6: 
            return ((long) in.readInt()) << 16 | in.readUnsignedShort();
          case 7: 
            return ((long) in.readInt()) << 24 | (in.readUnsignedShort() << 8) | in.readUnsignedByte();
          case 8: 
            return in.readLong();
          default: 
            throw new IOException("Corrupted VLong encoding");
        } 
      default: 
        throw new RuntimeException("Internal error");
    } 
  } 
   
  /** 
   * Write a String as a VInt n, followed by n Bytes as in Text format. 
   *  
   * @param out 
   * @param s 
   * @throws IOException 
   */ 
  public static void writeString(DataOutput out, String s) throws IOException {
    if (s != null) {
      Text text = new Text(s);
      byte[] buffer = text.getBytes();
      int len = text.getLength();
      writeVInt(out, len);
      out.write(buffer, 0, len);
    } else { 
      writeVInt(out, -1);
    } 
  } 
   
  /** 
   * Read a String as a VInt n, followed by n Bytes in Text format. 
   *  
   * @param in 
   *          The input stream. 
   * @return The string 
   * @throws IOException 
   */ 
  public static String readString(DataInput in) throws IOException {
    int length = readVInt(in);
    if (length == -1)
      return null; 
    byte[] buffer = new byte[length];
    in.readFully(buffer);
    return Text.decode(buffer);
  } 
   
  /** 
   * A generic Version class. We suggest applications built on top of TFile use this class to maintain version information in their meta blocks. 
Contextual code suggestions in your IDE  Get Codota for Java