File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes InputStream, UTF-8/16 und Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "InputStream, UTF-8/16 und Strings" Watch "InputStream, UTF-8/16 und Strings" New topic

InputStream, UTF-8/16 und Strings

S Lei

Joined: Feb 20, 2010
Posts: 7
I'm working on a project these days, where I'm not responsible for the framework I have to work within. Now, ther are some problems with the encoding and special characters:

I've got to process text from an InputStream, and return an InputStream itself again. For this processing, I need the complete String from the given InputStream. Furthermore, I have to assume that the text in the InputStream contains every sort of characters, special characters and such. I assume, that the content of the InputStream is UTF-8 encoded.
Now, my first question is: Is this even possible? As far as I read, InputStreams work with bytes and byteArrays. byte can only take the values from -128 to 127, no? So, how can UTF-8 text be stored in an InputStream?

Now, assuming the answer to the question above is "yes" - I read the text from the Stream with an InputStreamReader, and I'm processing some form of output, as String.
How can I create an InputStream that contains the output, and ensure UTF-8 compatibility? Until now, I just found a possibility with ByteArrayInputStream, but they say it's deprecated, and I have to use a StringReader. But how can I convert/transform a StringReader into an InputStream?

-- PS: 1) Sorry for this post in the first place -- I had a total blackout and postet it in my mother language ^^
2) ByteArrayInputstream seems to work - but they say, that there is a more preferable way with StringReader. Mh.

Thanks in Advance!
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

Please translate your post to English. This is an international forum with a wide user base. I would say that most of us don't understand German. I myself can read it a bit but I would say I'm an exception. By using German for your post you are limiting the number of people who can help you out.

As for the answer, you can use String.getBytes(String encoding) in combination with a ByteArrayInputStream:

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: InputStream, UTF-8/16 und Strings
It's not a secret anymore!