aspose file tools*
The moose likes I/O and Streams and the fly likes Input Stream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Input Stream" Watch "Input Stream" New topic
Author

Input Stream

k mayank
Greenhorn

Joined: Feb 09, 2009
Posts: 27
Hi friends,

I'm facing a strange problem over here. I'm trying to post a request to HttpServlet.
In between I have a small JAVA application which reads the request body from imputStream and streams the same(along with headers) to the servlet.

Due to legacy reasons we are using BufferedReader and thus read in achar array instead of byte.

This all works fine till I post a request containing Hebrew characters, in which case when I stream the request to servlet, the Hebrew charactes are changed and thus take more bytes than original Content-Length header.

I have tried System.setProperty("file.encoding", "UTF-8") and also setProperty("sun.jnu.encoding", "UTF-8") without any luck.

One thing which blows me off is if I set LANG variable in Linux to have UTF-8 as character encoding(ex en_US.UTF-8 ), this all works fine and characters are not converted. Any encoding apart from UTF-8 fails, but I dont understand the dependency on OS level Lang parameter given that many sites quote that streams in java are converted from file.encoding to unicode and viceverca
Any suggestions and help in this is highly appreciated.

Regards,
Mayank[/color]


mayank
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
Hi have you tried to add a header to the request saying that its contents are UTF-8 encoded ? another solution would be calculaing the size of your contents and change the original Content-Length in the header.


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
k mayank
Greenhorn

Joined: Feb 09, 2009
Posts: 27
Hi All,

Sorry for comming back late on this... was out of town(without internet access) for some emergency.
I have read both the options given below.

The problem scenario is that we intercept the request to servlet in between and validate it against certain rules.
I case all the rules are satisfied then the request is forwarded to the servlet else, it is not.

In the entire process we are not allowed to change the request header or doby even by a bit.

Please help me understand that how can Java depend on Linux LANG setting instead of its own file.encoding property.
I was setting the file.encoding in the constructor of the class as well as at startup of application in my main method.

Any link which shall provide some information on this is highly appretiable.

Regards,
Mayank
k mayank
Greenhorn

Joined: Feb 09, 2009
Posts: 27
Hi Friends,

Any Advises please....


Regards,
Mayank
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
I would guess that some part of the whole chain of processing does not handle encodings correctly. In particular, that it uses the platform default encoding instead of the encoding specified by the code.


Ping & DNS - my free Android networking tools app
k mayank
Greenhorn

Joined: Feb 09, 2009
Posts: 27
Hi Ulf,

Is it possible for java to use platform default encoding instead of its own file.encoding.
In what scenarios will that be done.

Regards,
Mayank
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
Java *always* uses the platform default encoding -which is the system property "file.encoding"- unless you specifically tell it to do otherwise.

I can't tell from your earlier posts whether you change that property at JVM startup -in which case it becomes the default encoding- or if you set it later during runtime (which could easily lead to compatibility problems, I would imagine).

Either set the property as a JVM parameter, or make sure that ALL your I/O handling specifies the encoding explicitly.
k mayank
Greenhorn

Joined: Feb 09, 2009
Posts: 27
Sorry to be asking stupid question, but by JVM startup do you mean when I call public static void main.
In that case I have set it there. This little multi threaded application is sitting on a different machine from the servlet.
Regards,
Mayank
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42277
    
  64
No, I meant as a JVM parameter. But upon examining this JVM bug report it seems that not all (any?) JVMs support changing that property. The preferred way of influencing the platform default encoding is thus to do what you did before - set the OS locale accordingly.
k mayank
Greenhorn

Joined: Feb 09, 2009
Posts: 27
Hi Ulf,

Thanks a lot for your reply and time. This was really helpful.

Thanks and Regards,
Mayank
 
Consider Paul's rocket mass heater.
 
subject: Input Stream