From: Arne Vajhøj on
On 20-03-2010 10:53, Arne Vajh�j wrote:
> On 20-03-2010 10:03, Tony Johansson wrote:
>> "Arne Vajh�j"<arne(a)vajhoej.dk> skrev i meddelandet
>> news:4ba4c05d$0$281$14726298(a)news.sunsite.dk...
>>> On 20-03-2010 05:28, Tony Johansson wrote:
>>>> Note as I have noticed you can specify a larger buffer for
>>>> StreamWrite in
>>>> the c-tor for StreamWrite and therefore skip the BufferedStream.
>>>>
>>>> I just wonder if I still want to use a BufferedStream or a MemoryStream
>>>> aren't these more or less the same thing.
>>>> I mean if I use a BufferedStream this buffer is located in the
>>>> memory. So
>>>> I
>>>> mean that the functionality will be
>>>> about the same.
>>>>
>>>> So the purpose of a BufferedStream is to keep a buffer so the number of
>>>> writes to the underlaying stream will be lower and
>>>> therefore increased performance.
>>>> The purpose with MemoryStream it to write to the MemoryStream from
>>>> example a
>>>> StreamWriter and then use the MemoryStream with an encapulated
>>>> FileStream
>>>> to
>>>> write to a file.
>>>>
>>>> So as a summary I see these two MemoryStream and the BufferedStream as
>>>> doing
>>>> more or less the same thing ?
>>>>
>>>> Do you agree with me ?
>>>
>>> No.
>>>
>>> MemoryStream is usually used for conversions bytes<->something and
>>> the stuff is not to be written to disk or network.
>>>
>>> BufferedStream is used when disk or network IO needs buffering
>>> (and there is not something on top of it that provide such
>>> capabilities). Sometimes you do not use *Reader/*Writer for IO.
>>
>> What do you mean with this row that you wrote.
>>> MemoryStream is usually used for conversions bytes<->something and
>>> the stuff is not to be written to disk or network
>>
>> Do you mean the usage is like a common area that is used for
>> communication ?
>> But I mean if you use MemoryStream as I describe it will remind a lot
>> to the
>> functionallity of BufferedStream
>
> Yes.
>
> But that is not a typical usage of MemoryStream.
>
> Typical you use MemoryStream when you are reading from
> to writing to a byte array that is not a simple buffer.
>
> Example:
>
> public class Ser<T>
> {
> public static byte[] Object2ByteArray(T o)
> {
> MemoryStream ms = new MemoryStream();
> BinaryFormatter bf = new BinaryFormatter();
> bf.Serialize(ms, o);
> return ms.ToArray();
> }
> public static string Object2String(T o)
> {
> return Convert.ToBase64String(Object2ByteArray(o));
> }
> public static T ByteArray2Object(byte[] b)
> {
> MemoryStream ms = new MemoryStream(b);
> BinaryFormatter bf = new BinaryFormatter();
> ms.Position = 0;
> return (T)bf.Deserialize(ms);
> }
> public static T String2Object(string s)
> {
> return ByteArray2Object(Convert.FromBase64String(s));
> }
> }
>
> where String2Object(foobar) can be inserted in a database VARCHAR field.

Object2String

Arne