Byte Array x Stream

Asked

Viewed 189 times

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 preferable in the case of sending and receiving, returning an array of bytes or a stream ? Some other more specialized class ?

2 answers

1

On the MSDN website, the definition for the Stream class

Provides a generic way to see a sequence of bytes.

https://msdn.microsoft.com/pt-br/library/system.io.stream%28v=vs.110%29.aspx

Analyzing

However, I disagree with them as to what is summed up in this simple sentence above. Stream, imagine a flow, which may be unidirectional or bidirectional. Byte array is closely linked to primitive data, it is very close, however with Stream you gain more resources and better management on the part of . NET.


Setting

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


Stream vs. Array Bytes

  1. Flexibility for handling.
  2. More autonomous memory management for GC.
  3. Functions that characterize a better defined object.
  4. Async.
  5. Useful and objective properties.
  6. Wrapper for Thread-Safe.
  7. Disposable.
  8. Buffer may also be located in virtual memory p/files.

Additional information

It was all based on experiences I had, so there may be more details I might have forgotten. I will leave a link below that is simple and objective as to Stream Operations in WCF. http://www.codeproject.com/Articles/36973/Stream-Operation-in-WCF

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 seems quite like Ienumerable at times the data is in computer memory, but it could also represent data you do not have yet.

Finally, 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 (not WCF): https://msdn.microsoft.com/pt-br/library/system.net.sockets.tcpclient.getstream%28v=vs.110%29.aspx

After you have obtained Networkstream, call the Write method to send data to the remote host. Call the Read method to receive data arriving from remote host

Browser other questions tagged

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