Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an Stream into a byte[] in C#? [duplicate]

Is there a simple way or method to convert an Stream into a byte[] in C#?

like image 569
pupeno Avatar asked Jul 03 '09 18:07

pupeno


People also ask

How do you convert char stream to byte streams?

1. You have to use OutputStreamWriter class for converting Character stream to Byte stream. 2. InputStreamReader class for converting Byte stream to Character stream, as these classes are used for stream conversions between two different streams.

Is a byte array a stream?

Streams are commonly written to byte arrays. The byte array is the preferred way to reference binary data in . NET. It can be used to data like the contents of a file or the pixels that make up the bitmap for an image.

What is used to convert stream of bytes to object?

The readObject method is called by Java, when a stream of byte is deserialized to Java object. This time, output will be updated based on the current year. Conclusively serialization is the process of converting an object into stream of bytes. The converted byte stream can be used for multiple purposes.

How do I read a byte stream?

The read() method of ByteArrayInputStream class in Java is used to read the next byte of the ByteArrayInputStream. This read() method returns the byte that is read int the form of an integer and if the input stream is ended this method return -1. This method reads one byte at a time from the stream.


1 Answers

The shortest solution I know:

using(var memoryStream = new MemoryStream()) {   sourceStream.CopyTo(memoryStream);   return memoryStream.ToArray(); } 
like image 139
James Dingle Avatar answered Oct 05 '22 20:10

James Dingle