public static byte[] toUTF(String s) {
    HeapDataOutputStream hdos = new HeapDataOutputStream(s);
    return hdos.toByteArray();
  }
The logic used here is based on java's DataInputStream.writeUTF() from the version 1.6.0_10.

@return byte[]
geode-core/src/main/java/org/apache/geode/internal/cache/tier/sockets/CacheServerHelper.java