77

Is there an elegant to emulate the StreamReader.ReadToEnd method with BinaryReader? Perhaps to put all the bytes into a byte array?

I do this:

read1.ReadBytes((int)read1.BaseStream.Length);

...but there must be a better way.

SamFisher83
  • 3,937
  • 9
  • 39
  • 52

6 Answers6

111

Original Answer (Read Update Below!)

Simply do:

byte[] allData = read1.ReadBytes(int.MaxValue);

The documentation says that it will read all bytes until the end of the stream is reached.


Update

Although this seems elegant, and the documentation seems to indicate that this would work, the actual implementation (checked in .NET 2, 3.5, and 4) allocates a full-size byte array for the data, which will probably cause an OutOfMemoryException on a 32-bit system.

Therefore, I would say that actually there isn't an elegant way.

Instead, I would recommend the following variation of @iano's answer. This variant doesn't rely on .NET 4:
Create an extension method for BinaryReader (or Stream, the code is the same for either).

public static byte[] ReadAllBytes(this BinaryReader reader)
{
    const int bufferSize = 4096;
    using (var ms = new MemoryStream())
    {
        byte[] buffer = new byte[bufferSize];
        int count;
        while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
            ms.Write(buffer, 0, count);
        return ms.ToArray();
    }
    
}
noio
  • 5,744
  • 7
  • 44
  • 61
Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
  • 13
    This gives me an OutOfMemoryException in .NET 4.0 (testing with LINQPad). Indeed, decompiling the source with Reflector reveals that ReadBytes tries to allocate a byte array with size of the given count: byte[] buffer = new byte[count];. – iano Apr 06 '12 at 01:01
  • 1
    @iano You are correct. I also decompiled .NET 2.0, and it's the same. I'm gonna update my answer with a disclaimer. – Scott Rippey Apr 09 '12 at 16:47
  • Can someone explain to me the basics why buffer = new byte[count] would cause an outofmemory exception? I would like to understand the fundamentals of buffering why it's needed. Thanks – Shrage Smilowitz Dec 10 '14 at 15:59
  • 1
    @ShrageSmilowitz Well, if you create an array that holds `int.MaxValue` 32-bit integers, you'll be allocating 8GB of memory ... so that's why you should build the results using smaller buffers! – Scott Rippey Dec 11 '14 at 22:33
  • The second attempt is bad too, it's not short nor elegant. There are much better answers here. – user626528 Feb 14 '15 at 11:14
  • 1
    @user626528 Back in 2011, when .NET 3.5 was dominant, this was the simplest answer. And I agree with you, iano's answer is much better nowadays. – Scott Rippey Feb 15 '15 at 19:25
  • 1
    Is this actually better than the approach from the question? The line `read1.ReadBytes((int)read1.BaseStream.Length);` seems not that bad to justify an extension method. As long as Microsoft doesn't add ReadToEnd to BinaryReader (which is present in StreamReader) I'd stick with the one-liner. – LRMAAX Jun 22 '20 at 23:21
82

There is not an easy way to do this with BinaryReader. If you don't know the count you need to read ahead of time, a better bet is to use MemoryStream:

public byte[] ReadAllBytes(Stream stream)
{
    using (var ms = new MemoryStream())
    {
        stream.CopyTo(ms);
        return ms.ToArray();
    }
}

To avoid the additional copy when calling ToArray(), you could instead return the Position and buffer, via GetBuffer().

iano
  • 2,061
  • 1
  • 18
  • 22
  • I agree, this is probably the most elegant answer. Worth noting, though, `Stream.CopyTo` is only available in .NET 4. – Scott Rippey Apr 09 '12 at 17:20
  • +1. I stumbled across this answer when searching for a solution for my woes. I had a problem with a class in a 3rd party assembly (from which I wanted to get all the bytes) which derived from `Stream` but its `Length` property was always zero. I initially tried an extension method-based approach, but felt it was unwieldy. – Wai Ha Lee Mar 04 '15 at 15:05
  • 2
    I would add stream.Position = 0; before CopyTo – Ivan Sokalskiy Mar 31 '16 at 22:58
  • how do you get stream? Hate these incomplete answers – Gustavo Baiocchi Costa Aug 13 '18 at 16:15
  • 2
    @GustavoBaiocchiCosta `yourBinaryReader.BaseStream` – jtate Nov 05 '18 at 15:05
  • Take a look at my just-posted version. Not sure if `binaryReader.BaseStream.Length` was available back then, but it is now so we can use that without having to construct a new `MemoryStream`! :) – Mark A. Donohoe Jan 18 '21 at 04:10
  • This led me to the solution I needed. Note that Stream also has a `CopyToAsync` method – Giles Sep 12 '22 at 11:18
  • Please be aware this will cause some buffers allocation at MemoryStream side. Typically twice the size of the stream (since MemoryStream buffer is doubled each type it reach maximum capacity) – tigrou Jul 28 '23 at 23:39
4

To copy the content of a stream to another, I've solved reading "some" bytes until the end of the file is reached:

private const int READ_BUFFER_SIZE = 1024;
using (BinaryReader reader = new BinaryReader(responseStream))
{
    using (BinaryWriter writer = new BinaryWriter(File.Open(localPath, FileMode.Create)))
    {
        int byteRead = 0;
        do
        {
            byte[] buffer = reader.ReadBytes(READ_BUFFER_SIZE);
            byteRead = buffer.Length;
            writer.Write(buffer);
            byteTransfered += byteRead;
        } while (byteRead == READ_BUFFER_SIZE);                        
    }                
}
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
  • That worked for me thanks. (You need to remove the 'byteTransfered += byteRead;' line though to run as-is.) – hillstuk Oct 26 '21 at 08:58
1

Had the same problem.
First, get the file's size using FileInfo.Length.
Next, create a byte array and set its value to BinaryReader.ReadBytes(FileInfo.Length). e.g.

var size = new FileInfo(yourImagePath).Length;
byte[] allBytes = yourReader.ReadBytes(System.Convert.ToInt32(size));
0

Another approach to this problem is to use C# extension methods:

public static class StreamHelpers
{
   public static byte[] ReadAllBytes(this BinaryReader reader)
   {
      // Pre .Net version 4.0
      const int bufferSize = 4096;
      using (var ms = new MemoryStream())
      {
        byte[] buffer = new byte[bufferSize];
        int count;
        while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
            ms.Write(buffer, 0, count);
        return ms.ToArray();
      }

      // .Net 4.0 or Newer
      using (var ms = new MemoryStream())
      {
         stream.CopyTo(ms);
         return ms.ToArray();
      }
   }
}

Using this approach will allow for both reusable as well as readable code.

mageos
  • 1,216
  • 7
  • 15
0

I use this, which utilizes the underlying BaseStream property to give you the length info you need. It keeps things nice and simple.

Below are three extension methods on BinaryReader:

  • The first reads from wherever the stream's current position is to the end
  • The second reads the entire stream in one go
  • The third utilizes the Range type to specify the subset of data you are interested in.
public static class BinaryReaderExtensions {

    public static byte[] ReadBytesToEnd(this BinaryReader binaryReader) {
    
        var length = binaryReader.BaseStream.Length - binaryReader.BaseStream.Position;
        return binaryReader.ReadBytes((int)length);
    }
    
    public static byte[] ReadAllBytes(this BinaryReader binaryReader) {
    
        binaryReader.BaseStream.Position = 0;
        return binaryReader.ReadBytes((int)binaryReader.BaseStream.Length);
    }

    public static byte[] ReadBytes(this BinaryReader binaryReader, Range range) {

        var (offset, length) = range.GetOffsetAndLength((int)binaryReader.BaseStream.Length);
        binaryReader.BaseStream.Position = offset;
        return binaryReader.ReadBytes(length);      
    }
}

Using them is then trivial and clear...

// 1 - Reads everything in as a byte array
var rawBytes = myBinaryReader.ReadAllBytes();

// 2 - Reads a string, then reads the remaining data as a byte array
var someString = myBinaryReader.ReadString();
var rawBytes = myBinaryReader.ReadBytesToEnd();

// 3 - Uses a range to read the last 44 bytes
var rawBytes = myBinaryReader.ReadBytes(^44..);

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286