Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

character decoding

Status
Not open for further replies.

plunkett

Technical User
Jun 10, 2004
57
0
0
US
I'm receiving data through a socket....

socket = new Socket(server, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));


This is coming from a remote server that I don't control.
Special characters are encoded, and if I print them out to the console they appear as diamond question marks.

If I telnet to the server and view the data it appears strange:
If I tell the server to send me 棥¿ÄÅüÇ, it displays in a telnet session as 棥¿Ã
Ã
üÃ


What can I do to make these special characters show up correctly?


 
What can I do to make these special characters show up correctly?
Use the same characterset to display them, as the server used to encode them with? Ask the developer/admin of the server app what characterset is to be expected.
 
I found out that certain data is sent UTF8 encoded, and other data is not.

I know how to do this:

Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
socket = new Socket(server, port);
in = new BufferedReader(new InputStreamReader(socket.getInputStream(), decoder));

This works fine for the UTF-8 encoded bytes.

But the problem is, when the non-utf8 bytes comes through, and it contains weird chars, I get this:

java.nio.charset.MalformedInputException: Input length = 1
Exited whiel loop
at java.nio.charset.CoderResult.throwException(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at chatfoo.ClientCode.receive(ClientCode.java:148)
at chatfoo.ClientCode.connect(ClientCode.java:117)
at chatfoo.ThreadHelper.run(ThreadHelper.java:19)
at java.lang.Thread.run(Unknown Source)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top