Creating a byte array from a stream

后端 未结 16 2945
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 23:22

What is the prefered method for creating a byte array from an input stream?

Here is my current solution with .NET 3.5.

Stream s;
byte[] b;

using (         


        
相关标签:
16条回答
  • 2020-11-21 23:29
    MemoryStream ms = new MemoryStream();
    file.PostedFile.InputStream.CopyTo(ms);
    var byts = ms.ToArray();
    ms.Dispose();
    
    0 讨论(0)
  • 2020-11-21 23:29

    I get a compile time error with Bob's (i.e. the questioner's) code. Stream.Length is a long whereas BinaryReader.ReadBytes takes an integer parameter. In my case, I do not expect to be dealing with Streams large enough to require long precision, so I use the following:

    Stream s;
    byte[] b;
    
    if (s.Length > int.MaxValue) {
      throw new Exception("This stream is larger than the conversion algorithm can currently handle.");
    }
    
    using (var br = new BinaryReader(s)) {
      b = br.ReadBytes((int)s.Length);
    }
    
    0 讨论(0)
  • 2020-11-21 23:35

    The one above is ok...but you will encounter data corruption when you send stuff over SMTP (if you need to). I've altered to something else that will help to correctly send byte for byte: '

    using System;
    using System.IO;
    
            private static byte[] ReadFully(string input)
            {
                FileStream sourceFile = new FileStream(input, FileMode.Open); //Open streamer
                BinaryReader binReader = new BinaryReader(sourceFile);
                byte[] output = new byte[sourceFile.Length]; //create byte array of size file
                for (long i = 0; i < sourceFile.Length; i++)
                    output[i] = binReader.ReadByte(); //read until done
                sourceFile.Close(); //dispose streamer
                binReader.Close(); //dispose reader
                return output;
            }'
    
    0 讨论(0)
  • 2020-11-21 23:36

    just my couple cents... the practice that I often use is to organize the methods like this as a custom helper

    public static class StreamHelpers
    {
        public static byte[] ReadFully(this Stream input)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                input.CopyTo(ms);
                return ms.ToArray();
            }
        }
    }
    

    add namespace to the config file and use it anywhere you wish

    0 讨论(0)
  • 2020-11-21 23:36

    In case anyone likes it, here is a .NET 4+ only solution formed as an extension method without the needless Dispose call on the MemoryStream. This is a hopelessly trivial optimization, but it is worth noting that failing to Dispose a MemoryStream is not a real failure.

    public static class StreamHelpers
    {
        public static byte[] ReadFully(this Stream input)
        {
            var ms = new MemoryStream();
            input.CopyTo(ms);
            return ms.ToArray();
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:37

    In namespace RestSharp.Extensions there is method ReadAsBytes. Inside this method is used MemoryStream and there is the same code like in some examples on this page but when you are using RestSharp this is easiest way.

    using RestSharp.Extensions;
    var byteArray = inputStream.ReadAsBytes();
    
    0 讨论(0)
提交回复
热议问题