tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Len Popp" <>
Subject Re: [OT] Basic int/char conversion question
Date Thu, 01 Jan 2009 17:03:57 GMT
On Thu, Jan 1, 2009 at 11:13, André Warnier <> wrote:
> Hi.
> This has nothing specific to Tomcat, it's just a problem I'm having as a
> non-java expert in modifying an exiting webapp.
> I hope someone on this list can answer quickly, or send me to the
> appropriate place to find out.  I have tried to find, but get somewhat lost
> in the Java docs.
> Problem :
> an existing webapp reads from a socket connected to an external program.
> The input stream is created as follows :
> fromApp = socket.getInputStream();
> The read is as follows :
> StringBuffer buf = new StringBuffer(2000);
> int ic;
> while((ic = != 26 && ic != -1) // hex 1A (SUB)
>           buf.append((char)ic);
> This is wrong, because it assumes that the input stream is always in an
> 8-bit default platform encoding, which it isn't.
> How do I do this correctly, assuming that I do know that the incoming stream
> is an 8-bit stream (like iso-8859-x), and I do know which 8-bit encoding is
> being used (such as iso-8859-1 or iso-8859-2) ?
> I cannot change the InputStream into something else, because there are a
> zillion other places where this webapp tests on the read byte's value,
> numerically.
> I mean, to append correctly to "buf" what was read in the "int", knowing
> that the proper encoding (charset) of "fromApp" is "X", how do I write this
> ?
> Thanks.

Another option: Read the bytes into a ByteBuffer, then convert the
bytes into a string. You can tell the String constructor which charset
to use.
View raw message