StringOutputStream?

  • Thread starter =?iso-8859-1?B?bW9vcJk=?=
  • Start date
?

=?iso-8859-1?B?bW9vcJk=?=

Hi,
Why there is no output stream that maps output to a String object? I
wonder how diffcult or why it is no one out there for use.
 
R

Robert Klemme

Why there is no output stream that maps output to a String object? I
wonder how diffcult or why it is no one out there for use.

Strings hold characters, OutputStream deal with bytes and character !=
byte. To bridge between them you need a character encoding.

Depending on what you want to do you can either use a StringWriter or
use a ByteArrayOutputStream and convert during reading from that using a
InputStreamReader.

Regards

robert
 
J

Jeffrey Schwab

moop™ said:
Why there is no output stream that maps output to a String object? I
wonder how diffcult or why it is no one out there for use.

Good question.

Strings hold characters, whereas OutputStreams output bytes. There is a
ByteArrayOutputStream.
 
P

Patricia Shanahan

moop™ said:
Hi,
Why there is no output stream that maps output to a String object? I
wonder how diffcult or why it is no one out there for use.

I started quite a long thread by asking the dual of this question:

http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/de63a5dc2a5eea8d

The bottom line seems to be:

1. Direct conversion between stream and String is deprecated.

2. Conversion between String and Reader is supported, but conversion
between Reader and Stream always assumes the Reader is built on top of
the Stream, not the other way round, so it doesn't help.

3. Support for byte arrays is much more flexible.

4. Some people think one should not try to relates streams and strings,
even if it is the most direct solution to a real problem.

I ended up converting my String to a byte array, and then building an
input stream from the array.

I think the corresponding solution to your problem would be to use a
ByteArrayOutputStream, and use one of its toString methods to extract
the String data. Clunky, but about the best that is supported.

Patricia
 
T

Thomas Weidenfeller

moop™ said:
Why there is no output stream that maps output to a String object?

ByteArrayOutputStream does, if you ask it nicely via toString().

/Thomas
 
T

Tor Iver Wilhelmsen

moop™ said:
Why there is no output stream that maps output to a String object?

Primarily because a Sting is immutable so you should not be able to
modify it.

Now, a stream that you can get a String out of after writing... try
implementing an OutputStream yourself that wraps a StringBuffer or
StringBuilder(), and whose toString() returns the underlying object's
toString(). Rememeber to handle close() in such a way that attempts to
write throws an exception.
 
M

Mike Schilling

moopT said:
Hi,
Why there is no output stream that maps output to a String object? I
wonder how diffcult or why it is no one out there for use.

Strings hold characters, not bytes, so I think you want to ask:

Why is there no StringWriter?

The answer to *this* is that Strings are immutable. There is a
StringBufferWriter, which appends characters to a StringBuffer as they're
output. At the end of that process, you can say

sbWriter.toString()

to collect all of the characters into a String.
 
M

Mike Schilling

Mike Schilling said:
Strings hold characters, not bytes, so I think you want to ask:

Why is there no StringWriter?

The answer to *this* is that Strings are immutable. There is a
StringBufferWriter,

Actually, this is called StringWriter, even though what it writes to is a
StringBuffer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,268
Messages
2,571,096
Members
48,773
Latest member
Kaybee

Latest Threads

Top