Friday, June 09, 2006

Sending non ASCII byte characters with Java

I recently wrote a short piece about a simple Telnet implementation. I got it all working...until I moved the code to a Solaris machine. Then it stopped working.

It turned out that the problem was the platforms' default charsets.

So, if you plan to send byte long control characters over TCP with Java. If you also choose to use the character based print and read methods from the Writer and Reader classes, as opposed to the byte based write and read methods. Then you should seriously consider manually setting the charset.

For example, in the case of a BufferedReader:


BufferedReader br = new BufferedReader(
new InputStreamReader(socket.getInputStream(),
Charset.forName("ISO-8859-1")));


Over and out for now then.