code: public void writeUTF(String pString) throws IOException {
          int numchars = pString.length();
          int numbytes = 0;
  
          for (int i = 0; i < numchars; i++) {
              int c = pString.charAt(i);
  
              if ((c >= 0x0001) && (c <= 0x007F)) {
                  numbytes++;
              }
              else if (c > 0x07FF) {
                  numbytes += 3;
              }
              else {
                  numbytes += 2;
              }
          }
  
          if (numbytes > 65535) {
              throw new UTFDataFormatException();
          }
  
          file.write((numbytes >>> 8) & 0xFF);
          file.write(numbytes & 0xFF);
  
          for (int i = 0; i < numchars; i++) {
              int c = pString.charAt(i);
  
              if ((c >= 0x0001) && (c <= 0x007F)) {
                  file.write(c);
              }
              else if (c > 0x07FF) {
                  file.write(0xE0 | ((c >> 12) & 0x0F));
                  file.write(0x80 | ((c >> 6) & 0x3F));
                  file.write(0x80 | (c & 0x3F));
              }
              else {
                  file.write(0xC0 | ((c >> 6) & 0x1F));
                  file.write(0x80 | (c & 0x3F));
              }
          }
      }
docs: Writes a string of no more than 65,535 characters
  to the underlying output stream using UTF-8
  encoding. This method first writes a two byte short
  in <b>big</b> endian order as required by the
  UTF-8 specification. This gives the number of bytes in the
  UTF-8 encoded version of the string, not the number of characters
  in the string. Next each character of the string is written
  using the UTF-8 encoding for the character.
  
  @param pString the string to be written.
  @throws UTFDataFormatException if the string is longer than
  65,535 characters.
  @throws IOException            if the underlying stream throws an IOException.