This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

[JAVA] Endoding an InputStream

 
Adriano Bellavita
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,

I have a problem with encoding in my project.

I am developing a proxy, I get remote files. xml.

Some of these have UTF-8 encoding (the type of coding that I intend to return to the client).

Other, different encoding (ISO-8859-1).

The xml returned by remote is converted to json from inputStream in this way.

JSON json = new XMLSerializer (). readFromStream (res.getEntity (). getContent ());


In particular,

Code:
res.getEntity (). getContent ()


returns, as mentioned, the inputStream of the response.

I simply wish to specify the inputStream encoding , so that the json obtained from the XML is correctly displayed in my client that uses encoding as UTF-8.

Specifically, without being able to change the charset, the client (UTF-8) does not correctly display the special characters and accented in case the remote XML has the ISO.


Good day,

Adriano.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Streams do not have an encoding - they're simply a sequence of bytes. The XMLSerializer object had better be smart enough to discover the encoding of the XML in the stream (XML has a mechanism for specifying the encoding).

I think the problem is more likely to originate while displaying those characters - how and where are you doing that?
 
Adriano Bellavita
Ranch Hand
Posts: 37
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do the following:

I take an InputStream from a http response.



I use the calss XmlSerializable to get a json.



I give back the JSON serialized to the customer.



toJson the method returns an object of type JSONObject.

When the xml that is returned by the server has an encoding ISO.8859-1, the client does not correctly display accented letters and special characters.

Otherwise, if the XML encoding UTF-8, everything goes well.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the code snippets you posted it's not clear where the problem could arise. For example, what is the XMLSerializer class? It's not the Xerces class, as that has no readFromStream method. And how does the "json" object become a "baos" object? That process must involve specifying an encoding.

Is the client capable of displaying ISO-8859-1 correctly? How are you telling the client that ISO-8859-1 is being used?
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic