code: public static void writeStringOpt(DataOutput out, String str) 
        throws IOException{
      if (str == null) {
        WritableUtils.writeVInt(out, NULL_STRING_LENGTH);
        return;
      }
      final int len = str.length();
      TempArrays ta = UTF8.getArrays(len);
      byte[] rawBytes = ta.byteArray;
      char[] charArray = ta.charArray;
      str.getChars(0, len, charArray, 0);
  
      boolean ascii = true;
      for (int i = 0; i < len; i++) {
        if (charArray[i] > UTF8.MAX_ASCII_CODE) {
          ascii = false;
          break;
        }
        rawBytes[i] = (byte) charArray[i];
      }
      if(ascii) {
        WritableUtils.writeVInt(out, len);
        out.write(rawBytes, 0, len);
      } else {
        writeString(out, str);
      }
    }
docs: Writes the string to the output, if possible
  the encoding part is optimized.