An elegant way to consume (all bytes of a) BinaryReader?

C#.NetStreamStreamreaderBinaryreader

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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();
	}
	
}

Solution 2 - C#

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().

Solution 3 - C#

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);                        
    }                
}

Solution 4 - C#

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));

Solution 5 - C#

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.

Solution 6 - C#

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..);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSamFisher83View Question on Stackoverflow
Solution 1 - C#Scott RippeyView Answer on Stackoverflow
Solution 2 - C#ianoView Answer on Stackoverflow
Solution 3 - C#Seraphim'sView Answer on Stackoverflow
Solution 4 - C#Omer HijaziView Answer on Stackoverflow
Solution 5 - C#mageosView Answer on Stackoverflow
Solution 6 - C#Mark A. DonohoeView Answer on Stackoverflow