Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reg: Not able to read double bytes using input stream through socket  RSS feed

 
Tarun Khandelwal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

We need to read some Chinese characters which is received from socket. but when we tried to read the data, its not getting read properly

Text that I got from socket... is below
příliš žluťoučký kůň úpěl ďábelské kódy

I read it.. using input stream

but when I try to write the same data to file ...it gives "?" in some places

we are converting into ut8.. so it should be write problem....

Please find the snippet of code


Can you please help us with the same

Thanks
Tarun
 
Stephan van Hulst
Saloon Keeper
Posts: 6971
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tarun, welcome to CodeRanch!

When you post code here, please UseCodeTags. It will make it more likely for people to want to read your code, and help you out. I added them for you this time, and cleaned up your formatting and indentation. (Tip: Don't use tabs in code. Use spaces instead, even for indentation).

As for your issue, you're going about the problem wrongly. Don't mess with bytes, when all you care about is text. The Java standard library has some great classes that help you out with text.

You want to interpret the incoming data as text, and then store it as text. This requires that you know what encoding the incoming text is, and what encoding you want to use to output the text.

Let's say the incoming text is encoded in UTF-8, and you also want to save it as UTF-8. You should use Readers and Writers to deal with the problem, not InputStreams and OutputStreams. Here's an example:
This will read characters from the InputStream, and store them in a file. You can easily change the encoding of both the input and the output.

To see if the characters are correct, you can open the file in a proper text editor, and view the text using the MS Gothic font.
 
Tarun Khandelwal
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stephan,

Thanks for your suggestion.
I have tried Reader earlier, but its giving me MalformedInputException, I am not able to identify the reason for the same.
And i have to use only InputStream i.e. byte stream for the same.

at sun.io.ByteToCharUTF8.convert(Unknown Source)
at java.io.InputStreamReader.convertInto(Unknown Source)
at java.io.InputStreamReader.fill(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.read(Unknown Source)


Thanks
Tarun
 
Stephan van Hulst
Saloon Keeper
Posts: 6971
109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, can you post an SSCCE that reproduces the problem?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!