tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: [OT] Basic int/char conversion question
Date Tue, 13 Jan 2009 01:06:09 GMT
Hash: SHA1


André Warnier wrote:
> 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.

Does it?

The only assumption I see here is that the byte code 0x1a has a special
meaning. Since ASCII is usually the lowest common denominator for
character encodings, is this a bad assumption?

> 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 like Chuck's suggestion to use an InputStreamReader because the
interfaces are (at least accidentally) the same, at least for the method
in question. I'm not sure how you would modify an entire application to
"fix" this code everywhere, though.

- -chris
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla -


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message