Thursday, 15 August 2013

.net - Is there a way to convert between byte[] to Stream and back without copying? -


I have to convert a byte [] array to a stream (such as to upload the file), then the second part In the code, convert any stream back into an array (for example to download a file). Is there any way to do these tasks without copying the byte array?

This bit of code shows that MemoryStream copies makes me want to do security business for better memory performance.

  Fixed zero main (string [] args) {byte [] array1 = new byte [1024]; (Using memorystream s = new memorystream (array 1)) {byte [] array2 = s.ToArray (); Console.light line (object referencing aqual (array 1, arr2)); // "false"}}    

You can use to avoid copying Just keep in mind that it is a "raw" backing buffer, which can be longer than the fictional length of the stream.

Alternatively, if you are providing a byte array to begin, you can use it only later:

  byte [] buffer = New byte [1024]; (Using memorystream stream = new memorystream (buffer)) {// Write it here} // Now buffer will contain written data   

Again, you need to know how much data actually Has been written in

No comments:

Post a Comment