title: Apache hadoop Java API - Difference between Text.readFields() vs Text.readString()
body: <p>I am trying to understand the difference between <code>Text.readFields()</code> vs <code>Text.readString()</code> in <a href="https://hadoop.apache.org/docs/r2.6.4/api/org/apache/hadoop/io/Text.html#readString(java.io.DataInput,%20int)" rel="nofollow noreferrer"><code>Text</code></a> class.</p>
  
  <p>From the <a href="https://hadoop.apache.org/docs/r2.6.4/api/org/apache/hadoop/io/Text.html#readString(java.io.DataInput,%20int)" rel="nofollow noreferrer">javadoc</a>, it is not very clear. The only slight hint is about that probably <code>Text.readString()</code> do perform UTF-8 encoding, which is evident from the actual implementation</p>
  
  <pre><code>467  /** Read a UTF8 encoded string with a maximum size
  468   */
  469  public static String readString(DataInput in, int maxLength)
  470      throws IOException {
  471    int length = WritableUtils.readVIntInRange(in, 0, maxLength);
  472    byte [] bytes = new byte[length];
  473    in.readFully(bytes, 0, length);
  474    return decode(bytes);
  475  }
  
  
  294  public void readFields(DataInput in, int maxLength) throws IOException {
  295    int newLength = WritableUtils.readVInt(in);
  296    if (newLength &lt; 0) {
  297      throw new IOException("tried to deserialize " + newLength +
  298          " bytes of data!  newLength must be non-negative.");
  299    } else if (newLength &gt;= maxLength) {
  300      throw new IOException("tried to deserialize " + newLength +
  301          " bytes of data, but maxLength = " + maxLength);
  302    }
  303    readWithKnownLength(in, newLength);
  304  }
  </code></pre>
  
  <p>Another difference is in the signature of the methods.  <code>readString()</code> is static, while other is not.</p>
  
  <p>Now I have below doubts:</p>
  
  <ol>
  <li>Am I missing something else?</li>
  <li>As far I know, Strings follows UTF encoding standard in Java API. What may be the condition when <code>readString()</code> and <code>readFields()</code> makes difference? Other way when should I use one instead of another.</li>
  <li>Why one is static and another is not? What difference it makes and when one should be used over another in this <code>Text</code> class context (class vs object level method)? </li>
  </ol>