public void writeUTF(String s) throws IOException {
                int numchars = s.length();
                int numbytes = 0;

                for (int i = 0; i < numchars; i++) {
                        int c = s.charAt(i);
                        if ((c >= 0x0001) && (c <= 0x007F))
                                numbytes++;
                        else if (c > 0x07FF)
                                numbytes += 3;
                        else
                                numbytes += 2;
                }
                if (numbytes > 65535)
                        throw new UTFDataFormatException();

                out.write((numbytes >>> 8) & 0xFF);
                out.write(numbytes & 0xFF);
                for (int i = 0; i < numchars; i++) {
                        int c = s.charAt(i);
                        if ((c >= 0x0001) && (c <= 0x007F)) {
                                out.write(c);
                        } else if (c > 0x07FF) {
                                out.write(0xE0 | ((c >> 12) & 0x0F));
                                out.write(0x80 | ((c >> 6) & 0x3F));
                                out.write(0x80 | (c & 0x3F));

                        } else {
                                out.write(0xC0 | ((c >> 6) & 0x1F));
                                out.write(0x80 | (c & 0x3F));
                        }
                }

        }