Are these two more or less the same thing MemoryStream and BufferedStream

Are these two more or less the same thing MemoryStream and BufferedStream

Post by Tony Johan » Sun, 21 Mar 2010 18:28:29


Hi!

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 ?


//Tony
 
 
 

Are these two more or less the same thing MemoryStream and BufferedStream

Post by Patric » Sun, 21 Mar 2010 19:26:51

By this criteria you'll find numerous things that are all stored in
memory...

Use a memory stream when you want to store the whole content in memory.
Use a buffered stream when you want to use a buffer around a stream that
doesn't already use one.

Being close doesn't mean you should use only one of them. They are here at
your disposal to be used when appropriate.

 
 
 

Are these two more or less the same thing MemoryStream and BufferedStream

Post by Arne Vaj » Sun, 21 Mar 2010 21:32:37


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.

Arne
 
 
 

Are these two more or less the same thing MemoryStream and BufferedStream

Post by Tony Johan » Sun, 21 Mar 2010 23:03:12

"Arne Vajh" < XXXX@XXXXX.COM > skrev i meddelandet



Hi!

What do you mean with this row that you wrote.

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

//Tony
 
 
 

Are these two more or less the same thing MemoryStream and BufferedStream

Post by Arne Vaj » Sun, 21 Mar 2010 23:53:48


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.

Arne
 
 
 

Are these two more or less the same thing MemoryStream and BufferedStream

Post by Arne Vaj » Sun, 21 Mar 2010 23:54:30

n 20-03-2010 10:53, Arne Vajh wrote:

Object2String

Arne