memorystream and Flush

T

Tony Johansson

Hi!

Here is a simple program where a StreamWriter writes some strings to a
MemoryStream and
then is this MemoryStream writing it's contents to a file by using a
FileStream

Now is it possible that some data will not be written to the MyFile.txt if I
don't have a Flush
on the memstr like this memstr.Flush();?

MemoryStream memstr = new MemoryStream();
StreamWriter sw = new StreamWriter(memstr );
sw.WriteLine("Test1");
sw.WriteLine("Test2"):
sw.WriteLine("Test3"):
sw.Flush();
FileStream fs = File.Create("MyFile.txt");
memstr.WriteTo(fs);

....

//Tony
 
M

mick

Tony Johansson said:
Hi!

Here is a simple program where a StreamWriter writes some strings to a
MemoryStream and
then is this MemoryStream writing it's contents to a file by using a
FileStream

Now is it possible that some data will not be written to the MyFile.txt if
I don't have a Flush
on the memstr like this memstr.Flush();?

MemoryStream memstr = new MemoryStream();
StreamWriter sw = new StreamWriter(memstr );
sw.WriteLine("Test1");
sw.WriteLine("Test2"):
sw.WriteLine("Test3"):
sw.Flush();
FileStream fs = File.Create("MyFile.txt");
memstr.WriteTo(fs);

I think Close() does a flush anyway.

mick
 
A

Arne Vajhøj

Here is a simple program where a StreamWriter writes some strings to a
MemoryStream and
then is this MemoryStream writing it's contents to a file by using a
FileStream

Now is it possible that some data will not be written to the MyFile.txt if I
don't have a Flush
on the memstr like this memstr.Flush();?

MemoryStream memstr = new MemoryStream();
StreamWriter sw = new StreamWriter(memstr );
sw.WriteLine("Test1");
sw.WriteLine("Test2"):
sw.WriteLine("Test3"):
sw.Flush();
FileStream fs = File.Create("MyFile.txt");
memstr.WriteTo(fs);

The doc does not say that WriteTo calls Flush, so
to be sure that it is out then you should call
fs.Flush.

Or just call Close or Dispose on fs - that will
also Flush.

Flush is for the situations where you want to
get something actually written but do not want
to close.

It is frequently used for sockets. And occasionally
for shared access files.

Arne
 
M

Mr. Arnold

Tony said:
Hi!

Here is a simple program where a StreamWriter writes some strings to a
MemoryStream and
then is this MemoryStream writing it's contents to a file by using a
FileStream

Now is it possible that some data will not be written to the MyFile.txt if I
don't have a Flush
on the memstr like this memstr.Flush();?

MemoryStream memstr = new MemoryStream();
StreamWriter sw = new StreamWriter(memstr );
sw.WriteLine("Test1");
sw.WriteLine("Test2"):
sw.WriteLine("Test3"):
sw.Flush();
FileStream fs = File.Create("MyFile.txt");
memstr.WriteTo(fs);

Here is an example as to when the Flush comes into play.

http://www.gnu.org/software/dotgnu/pnetlib-doc/System/IO/FileStream.html
 
J

Jeff Johnson

Here is a simple program where a StreamWriter writes some strings to a
MemoryStream and
then is this MemoryStream writing it's contents to a file by using a
FileStream

Now is it possible that some data will not be written to the MyFile.txt if
I don't have a Flush
on the memstr like this memstr.Flush();?

No. A MemoryStream is simply a construct that sits over bytes in memory (a
backing store). When data is written to a MemoryStream it immediately goes
into the backing store. There is never a need to flush a MemoryStream. The
whole concept of flushing is to force the movement of bytes from a buffer
into the destination, but the MemoryStream IS the buffer in this case.

I highly recommend you go get Reflector (http://reflector.red-gate.com). All
you'd need to do is disassemble System.IO.MemoryStream.Flush() and you'd see
that it's implemented like this:

public override void Flush()
{
}

How's that for low overhead?
 
T

Tony Johansson

Jeff Johnson said:
No. A MemoryStream is simply a construct that sits over bytes in memory (a
backing store). When data is written to a MemoryStream it immediately goes
into the backing store. There is never a need to flush a MemoryStream. The
whole concept of flushing is to force the movement of bytes from a buffer
into the destination, but the MemoryStream IS the buffer in this case.

I highly recommend you go get Reflector (http://reflector.red-gate.com).
All you'd need to do is disassemble System.IO.MemoryStream.Flush() and
you'd see that it's implemented like this:

public override void Flush()
{
}

How's that for low overhead?

I hope you understand you correct. So in this example no flush is needed to
move all data into the file from the memory ?

MemoryStream memstr = new MemoryStream();
StreamWriter sw = new StreamWriter(memstr );
sw.WriteLine("Test1");
sw.WriteLine("Test2"):
sw.WriteLine("Test3"):
sw.Flush();
FileStream fs = File.Create("MyFile.txt");
memstr.WriteTo(fs);

//Tony
 
A

Arne Vajhøj

I hope you understand you correct. So in this example no flush is needed to
move all data into the file from the memory ?

MemoryStream memstr = new MemoryStream();
StreamWriter sw = new StreamWriter(memstr );
sw.WriteLine("Test1");
sw.WriteLine("Test2"):
sw.WriteLine("Test3"):
sw.Flush();
FileStream fs = File.Create("MyFile.txt");
memstr.WriteTo(fs);

You need to call fs.Flush or fs.Close or fs.Dispose !

Arne
 
T

Tony Johansson

Arne Vajhøj said:
You need to call fs.Flush or fs.Close or fs.Dispose !

Arne

I dont really understand what you mean. Here is what I mean.
Here I create a MemoryStream and a StreamWriter and write something to the
memory by using the StreamWriter.
To move any data left in the StreamWriter buffer I use a flush on the
StreamWriter to move these data to the memoryStream.
Now I know that all data is in the MemoryStream.
Assume that I now want to move all data in the MemoryStream to a FileStream
that is refering to a file do I
then need to do a flush on the MemoryStream ?

MemoryStream memstr = new MemoryStream();
StreamWriter sw = new StreamWriter(memstr );
sw.WriteLine("Test1");
sw.WriteLine("Test2"):
sw.WriteLine("Test3"):
sw.Flush();
FileStream fs = File.Create("MyFile.txt");

//Tony
 
T

Tony Johansson

Tony Johansson said:
I dont really understand what you mean. Here is what I mean.
Here I create a MemoryStream and a StreamWriter and write something to the
memory by using the StreamWriter.
To move any data left in the StreamWriter buffer I use a flush on the
StreamWriter to move these data to the memoryStream.
Now I know that all data is in the MemoryStream.
Assume that I now want to move all data in the MemoryStream to a
FileStream that is refering to a file do I
then need to do a flush on the MemoryStream ?

MemoryStream memstr = new MemoryStream();
StreamWriter sw = new StreamWriter(memstr );
sw.WriteLine("Test1");
sw.WriteLine("Test2"):
sw.WriteLine("Test3"):
sw.Flush();
FileStream fs = File.Create("MyFile.txt");

//Tony

I looked at the Red-Gate's Reflector and the method Flush for Class
MemoryStream and here is the definition
public override void Flush()
{
}

So calling Flush on a MemoryStream is just doing nothing useful.
So when I want to move the data from a MemoryStream to a FileStream which is
refering to a file I
pass in the FileStream into the MemoryStream. I assume there is an internal
buffer between the FileStream and referenced file
so as Arne pointed out I probably must call flush on the underlaying stream
which is a FileStream to move all data in this internal buffer to the file
that is referenced by the FileStream.

//Tony
 
A

Arne Vajhøj

I dont really understand what you mean. Here is what I mean.
Here I create a MemoryStream and a StreamWriter and write something to the
memory by using the StreamWriter.
To move any data left in the StreamWriter buffer I use a flush on the
StreamWriter to move these data to the memoryStream.
Now I know that all data is in the MemoryStream.
Assume that I now want to move all data in the MemoryStream to a FileStream
that is refering to a file do I
then need to do a flush on the MemoryStream ?

MemoryStream memstr = new MemoryStream();
StreamWriter sw = new StreamWriter(memstr );
sw.WriteLine("Test1");
sw.WriteLine("Test2"):
sw.WriteLine("Test3"):
sw.Flush();
FileStream fs = File.Create("MyFile.txt");

No need to call Flush on sw or memstr, but you need to call
one of Flush/Close/Dispose on fs to be sure that the
content of is in myfile.txt !

Arne
 
J

Jeff Johnson

I hope you understand you correct. So in this example no flush is needed
to move all data into the file from the memory ?

MemoryStream memstr = new MemoryStream();
StreamWriter sw = new StreamWriter(memstr );
sw.WriteLine("Test1");
sw.WriteLine("Test2"):
sw.WriteLine("Test3"):
sw.Flush();
FileStream fs = File.Create("MyFile.txt");
memstr.WriteTo(fs);

Okay, I've been reading that wrong all this time, sorry. I thought you were
talking about flushing the MemoryStream, but you were talking about flushing
the StreamWriter. From looking at it in Reflector I would say it's a good
idea to flush it, BUT I would not call Flush() directly. Instead I'd wrap
the StreamWriter in a using { } block to make sure it gets disposed of
before you write to the FileStream. That will automatically flush the
writer. So I'd do something like this:

using (<memory stream>)
{
using (<stream writer>)
{
...
}

using (<file stream>)
{
memstr.WriteTo(fs);
}
}
 

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