Memorystream vs Stream

Asked

Viewed 2,118 times

14

What would be the main difference between the two?

There are advantages to performance gain?

For web use, which is the most appropriate?

I’m using to "read" a array Byte[]:

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

    pdfViewer1.LoadDocument(pdfStream);
}
  • As the main method (that calls this bytetoStrem) loads this array of PDF bytes? Is it an API that returns to it? Or do you read from a folder on the server itself?

3 answers

13


What would be the main difference between the two?

The Stream is an abstract class basis for all streams (fuller definition) that may exist, so you can encode algorithms that need to stream without knowing where they come from and go.

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

There are advantages to performance gain?

In relation to what? Between one of them? As one is abstract can even be compared. If go with others need to see the implementation of each, depends on the hardware where it works.

For web use, which is the most appropriate?

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

Among the streams existing on . NET does not see another that could be used for web specifically, but overall yes. It is possible to use some other third party or develop your own. The documentation linked 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, ok. It seems to me that this case is ok, is loading a PDF with specific API and throwing it into memory as this API allows. I’ll guess it’s not necessary to be a stream in the case, but I do not know if the PDF viewer API has another way to access. If the intention is to send directly to another location then it may be that another is more suitable, but by the question we have no way of knowing.

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

12

Stream is nothing more than the basic and abstract class (i.e., cannot be instantiated) for all other types of Stream available.

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

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

  • System.IO.Compression.GZipStream: Encapsulates one another Stream, (des)compacting the information of this;
  • System.IO.FileStream: Provides manipulation (read, write, navigation) in a buffer pointing to a file from the file system;
  • System.Net.Sockets.NetworkStream: Provides manipulation of buffer network;

7

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

I usually use MEMORYSTREAM when I am working with small files and I see no problem in keeping them fully in memory (MEMORYSTREAM). Ex. A photo or something. When you go to work with large files, imagine a video of 200MB for example, it is much more advisable to use a FILESTREAM, which it goes reading and transmitting the file gradually, without fully loading it to memory. I hope I’ve helped clear up your doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.