Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to MemoryStream with StreamWriter returns empty

I am not sure what I am doing wrong, have seen a lot of examples, but can't seem to get this working.

public static Stream Foo() {     var memStream = new MemoryStream();     var streamWriter = new StreamWriter(memStream);      for (int i = 0; i < 6; i++)         streamWriter.WriteLine("TEST");      memStream.Seek(0, SeekOrigin.Begin);     return memStream; } 

I am doing a simple test on this method to try and get it to pass, but no matter what, my collection count is 0.

[Test] public void TestStreamRowCount() {     var stream = Foo();      using (var reader = new StreamReader(stream))     {         var collection = new List<string>();         string input;          while ((input = reader.ReadLine()) != null)             collection.Add(input);          Assert.AreEqual(6, collection.Count);     } } 

Note: I changed some syntax above without compiling in the Test method. What is more important is the first method which seems to be returning an empty stream (my reader.ReadLine() always reads once). Not sure what I am doing wrong. Thank you.

like image 657
jsmith Avatar asked Apr 13 '11 17:04

jsmith


People also ask

Where does StreamWriter write to?

StreamWriter. WriteLine() method writes a string to the next line to the steam. The following code snippet creates and writes different author names to the stream.

How do you save on MemoryStream?

Save MemoryStream to a String StreamWriter sw = new StreamWriter(memoryStream); sw. WriteLine("Your string to Memoery"); This string is currently saved in the StreamWriters buffer. Flushing the stream will force the string whose backing store is memory (MemoryStream).

How do I write a string to MemoryStream?

MemoryStream stringInMemoryStream = new MemoryStream(ASCIIEncoding. Default. GetBytes("Your string here")); The string will be loaded into the MemoryStream , and you can read from it.

What is the difference between StreamWriter and TextWriter?

The StreamWriter class in C# is used for writing characters to a stream. It uses the TextWriter class as a base class and provides the overload methods for writing data into a file. The StreamWriter is mainly used for writing multiple characters of data into a file.


1 Answers

You are forgetting to flush your StreamWriter instance.

public static Stream Foo() {     var memStream = new MemoryStream();     var streamWriter = new StreamWriter(memStream);      for (int i = 0; i < 6; i++)         streamWriter.WriteLine("TEST");      streamWriter.Flush();                                   <-- need this     memStream.Seek(0, SeekOrigin.Begin);     return memStream; } 

Also note that StreamWriter is supposed to be disposed of, since it implements IDisposable, but that in turn generates another problem, it will close the underlying MemoryStream as well.

Are you sure you want to return a MemoryStream here?

I would change the code to this:

public static byte[] Foo() {     using (var memStream = new MemoryStream())     using (var streamWriter = new StreamWriter(memStream))     {         for (int i = 0; i < 6; i++)             streamWriter.WriteLine("TEST");          streamWriter.Flush();         return memStream.ToArray();     } }  [Test] public void TestStreamRowCount() {     var bytes = Foo();      using (var stream = new MemoryStream(bytes))     using (var reader = new StreamReader(stream))     {         var collection = new List<string>();         string input;          while ((input = reader.ReadLine()) != null)             collection.Add(input);          Assert.AreEqual(6, collection.Count);     } } 
like image 145
Lasse V. Karlsen Avatar answered Sep 24 '22 09:09

Lasse V. Karlsen