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

T

Tony Johansson

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
 
P

Patrice

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.
 
A

Arne Vajhøj

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.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
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

Hi!

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

//Tony
 
A

Arne Vajhøj

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

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
 
A

Arne Vajhøj

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

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
 

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

Top