Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MemoryStream.WriteTo(Stream destinationStream) versus Stream.CopyTo(Stream destinationStream)

Which one is better : MemoryStream.WriteTo(Stream destinationStream) or Stream.CopyTo(Stream destinationStream)??

I am talking about the comparison of these two methods without Buffer as I am doing like this :

Stream str = File.Open("SomeFile.file");
MemoryStream mstr = new MemoryStream(File.ReadAllBytes("SomeFile.file"));

using(var Ms = File.Create("NewFile.file", 8 * 1024))
{
    str.CopyTo(Ms) or mstr.WriteTo(Ms);// Which one will be better??
}

Update

Here is what I want to Do :

  • Open File [ Say "X" Type File]
  • Parse the Contents
  • From here I get a Bunch of new Streams [ 3 ~ 4 Files ]
  • Parse One Stream
  • Extract Thousands of files [ The Stream is an Image File ]
  • Save the Other Streams To Files
  • Editing all the Files
  • Generate a New "X" Type File.

I have written every bit of code which is actually working correctly..

But Now I am optimizing the code to make the most efficient.

like image 912
Writwick Avatar asked May 19 '12 10:05

Writwick


People also ask

What is the difference between Stream and MemoryStream?

You would use the FileStream to read/write a file but a MemoryStream to read/write in-memory data, such as a byte array decoded from a string. You would not use a Stream in and of itself, but rather use it for polymorphism, i.e. passing it to methods that can accept any implementation of Stream as an argument.

What is a MemoryStream?

MemoryStream encapsulates data stored as an unsigned byte array. The encapsulated data is directly accessible in memory. Memory streams can reduce the need for temporary buffers and files in an application. The current position of a stream is the position at which the next read or write operation takes place.

What is stream copy?

Stream copy is a mode selected by supplying the copy parameter to the ' -codec ' option. It makes ffmpeg omit the decoding and encoding step for the specified stream, so it does only demuxing and muxing. It is useful for changing the container format or modifying container-level metadata.


1 Answers

It is an historical accident that there are two ways to do the same thing. MemoryStream always had the WriteTo() method, Stream didn't acquire the CopyTo() method until .NET 4.

The MemoryStream.WriteTo() version looks like this:

public virtual void WriteTo(Stream stream)
{
    // Exception throwing code elided...
    stream.Write(this._buffer, this._origin, this._length - this._origin);
}

The Stream.CopyTo() implementation like this:

private void InternalCopyTo(Stream destination, int bufferSize)
{
    int num;
    byte[] buffer = new byte[bufferSize];
    while ((num = this.Read(buffer, 0, buffer.Length)) != 0)
    {
        destination.Write(buffer, 0, num);
    }
}

Stream.CopyTo() is more universal, it works for any stream. And helps programmers that fumble copying data from, say, a NetworkStream. Forgetting to pay attention to the return value from Read() was a very common bug. But it of course copies the bytes twice and allocates that temporary buffer, MemoryStream doesn't need it since it can write directly from its own buffer. So you'd still prefer WriteTo(). Noticing the difference isn't very likely.

like image 159
Hans Passant Avatar answered Nov 25 '22 04:11

Hans Passant