• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

[JAVA] Endoding an InputStream

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
For my next trick, I'll need the help of a tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic