String stream in .NET???

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey there,

I need a string stream, but I can't find one in .NET. I thought StringWriter
would derive from Stream, alas it doesn't do so.

Which leads me to my next question: What is the purpose of
System.IO.StringWriter (or, what is its added value)??? It can do the same
things with StringBuilder, and I see no added value.

The reason why I need the string stream, is that I have a library that knows
how to serialize certain library objects to a System.IO.Stream. My intent is
to serialize to a string, but I cannot do that with the current interface.

I do know that there is a memory stream. But my other string ouput methods
use StringBuilder..., not any preallocated byte buffer.

What I would prefer, is a StringStream class that derives from Stream, and
takes a StringBuilder as scratch-pad.

Any comment is welcome.

Thanks,
 
Tom,
Which leads me to my next question: What is the purpose of
System.IO.StringWriter (or, what is its added value)???
StringWriter inherits from TextWriter, it can be used any place a TextWriter
can be used. Creating functions that expect a TextWriter allows you to write
to either a Stream or a String...

The reason why I need the string stream, is that I have a library that
knows
how to serialize certain library objects to a System.IO.Stream. My intent
is
to serialize to a string, but I cannot do that with the current interface.
Remember that a String is a series of Unicode Chars (16 bit values). While a
Stream is a series of Bytes (8 bit values).
What I would prefer, is a StringStream class that derives from Stream, and
takes a StringBuilder as scratch-pad.
You could define one, however I wonder how well the encoding
(System.Text.Encoding) would work, as a series of Bytes is not necessarily
in any specific encoding, in fact a series of Bytes may be in multiple
encodings!

I do know that there is a memory stream. But my other string ouput methods
use StringBuilder..., not any preallocated byte buffer.
A Memory stream does not need to be preallocated, it can dynamically grow
just as a StringBuilder.


I would recommend using the MemoryStream when I wanted to serialize to
memory...

Hope this helps
Jay
 
Hi Jay,

Thanks for the response.

What should I do to get a string if I would serialize to a MemoryStream?
I've tried:

string s = encoding.GetString( ms.GetBuffer(), 0, (int) ms.Length );

but all that comes out is crap. I've tried ASCIIEncoding and UTF8 and UTF7.
I guess there is no good way...

Thanks,
Tom.
 
Tom,
but all that comes out is crap.
That's what I would expect you will see. As serialization to a stream is
more then likely Binary serialization. Binary serialization has no really
textual representation.

TextWriter (along with its descendents StringWriter & StreamWriter) are used
to create textual representation of objects. If you are writing to a Stream
this "Textual Representation" cannot magically happen by simply using a
StringWriter...
What should I do to get a string if I would serialize to a MemoryStream?

What are you expecting the string to look like? A String is text that people
can read, a Stream is bytes that people cannot read (well people can read
them, but they tend to have all sorts of funny characters in random order).

Hope this helps
Jay
 
Jay,

Thank you for sharing your insight. I now have a clearer view. The
serialization methods in the library were indeed for binary serialization and
not for string serialization.

I had to write my own string serialization routine.

Perhaps I made a mistake in comparing the use of std::stringstream in C++ to
streams in general.

Thank you,
Tom Tempelaere.
 
Back
Top