Array of Bytes x Stream

1

I'm developing a WCF service that will convert files. I will upload and then download the files. The files are not large.

What is preferred in the case of sending and receiving, returning an array of bytes or a stream? Any other more specialized classes?

    
asked by anonymous 12.08.2015 / 22:41

2 answers

1

On the MSDN website , the definition for the Stream class

  

Provides a generic way to see a byte stream.

link

Analysis

However, I disagree with them as to what is summed up in this simple sentence above. Stream, imagine a flow , which can be either unidirectional or bidirectional. Byte array is closely linked to primitive data, it's close enough, however with Stream you gain more features and better management by .NET.

Scenario

You mentioned that these files are not great, I understood that your idea is to pass the entire file to an array of bytes, even if they are not large, it would not be the correct one. Working with Streams you grant more autonomy to the GC to take care of this part for you and guarantee performance to your service, also being able to use virtual memory. Because you're on the side of a server, you have to ensure performance and low power consumption.

Stream vs. Array Bytes

  • Flexibility for manipulation.
  • More autonomous memory management for the GC.
  • Functions that characterize a better defined object.
  • Async.
  • Useful and objective properties.
  • Thread-Safe wrapper.
  • Disposable.
  • Buffer can also be located in virtual memory for files.
  • Additional information

    Everything was based on experiences I had, may have more details that I may have forgotten. I will leave a link below that is simple and objective regarding Stream Operations in WCF. link

        
    14.08.2015 / 03:45
    0

    It really depends on how your system is created. It is important to know that a stream does not mean that the data has arrived on the computer. It looks quite like IEnumerable in that sometimes the data is in the computer's memory, but could also represent data that you do not have yet.

    In the end, I was going to return a Stream because when they are returned you can still choose when and how to get the data in Stream (but again, it depends on the system). There is nothing wrong with an array of bytes, but a Stream would be more correct.

    Example of a Stream that does this (it's not WCF): link

      

    After you have obtained NetworkStream, call the Write method to send data to the remote host. Call the Read method to receive incoming remote host data.

        
    12.08.2015 / 22:52