MemoryStream vs Stream

11

What would be the main difference between the two?

Are there any advantages to performance gain?

For web use, which one is the right one?

I am using to "read" an array Byte[] :

private void bytetoStrem(byte[] pdf)
{
    Stream pdfStream = new MemoryStream(pdf);

    pdfViewer1.LoadDocument(pdfStream);
}
    
asked by anonymous 05.08.2017 / 15:39

3 answers

10
  

What would be the main difference between the two?

The Stream is one abstract base class for all streams (more complete definition) that can exist as well you can encode algorithms that require stream without knowing where they come from and go.

MemoryStream is one concrete class, obviously derived from Stream as can be observed in the question code, and works with data streams in raw memory.

  

Are there any advantages to performance gain?

For what? Between one of them? How one is abstract can not even be compared. If it is with others you need to see the implementation of each one, it depends on the hardware where it works.

  

For web use, which one is the right one?

Between the two will always be the concrete class since the abstract does nothing. If you want to compare with other things it does not make sense to analyze the use for web, web development is something too generic to define something.

Among the existing streams in .NET I do not see another that could be used for web specifically, but in general yes. It is possible to use some other third party or develop your own. The linked documentation above has all .NET classes that inherit from Stream .

Your case

If what you are doing works and meets all the requirements you want in all situations, that is, if it is right, okay. It seems to me that this case is okay, it is loading a PDF with specific API and throwing it in memory as this API allows. I'm going to kick that it does not need to be a stream in this case, but I do not know if the PDF Viewer API has another way to access it. If the intention is to send directly to another location then it may be that another is more appropriate, but the question we can not know.

The problem usually occurs because people do not read the documentation, do not understand collateral points and do not test all situations that can occur, in general the person tests to work, when the test is sure to not work and find out where to give problem and avoid it.

    
05.08.2017 / 16:34
10

Stream is nothing more than the base and abstract class (that is, can not be instantiated) for all other Stream types available.

MemoryStream is one of the implementations of Stream , which provides a way for you to store a Buffer (data in raw bytes) into memory.

In the documentation page you will find several native implementations of the .NET framework itself, such as:

  • System.IO.Compression.GZipStream : Encapsulates another Stream , (des) compressing its information;
  • System.IO.FileStream : Provides manipulation (reading, writing, browsing) in a buffer pointing to a file system file;
  • System.Net.Sockets.NetworkStream : Provides buffer manipulation of the network;
05.08.2017 / 16:34
6

As already said, STREAM is an abstract class. MEMORYSTREAM is a class derived from this class, as well as FILESTREAM.

I usually use MEMORYSTREAM when I'm working with small files and I see no problem in keeping them fully in memory (MEMORYSTREAM). Eg a photo, or something. When working with large files, imagine a video of 200MB for example, it is much more advisable to use a FILESTREAM, which will read and transmit the file gradually, without loading it fully into memory. I hope I have helped to clarify your doubt.

    
07.08.2017 / 01:36