An Elegant Way to Consume (All Bytes of A) Binaryreader

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

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

}

(F#) Smoothest way to read all bytes from a CryptoStream

Draining stream byte by byte will be very slow.

if you have .NET 4.0 then the most straightforward way will be:

open System.IO

let readAllBytes (s : Stream) =
let ms = new MemoryStream()
s.CopyTo(ms)
ms.ToArray()

else you need to reproduce CopyTo functionality manually

let readAllBytes (s : Stream) =
let ms = new MemoryStream()
let buf = Array.zeroCreate 8192
let rec impl () =
let read = s.Read(buf, 0, buf.Length)
if read > 0 then
ms.Write(buf, 0, read)
impl ()
impl ()

What is the correct way to read from a NetStream through a BinaryReader?

BinaryReader.Read(byte[], int, int) just forwards the call to the underlying stream. The semantics are the same. For this scenario, however, there is also the helper method BinaryReader.ReadBytes(int) which reads a specific number of bytes, so you don't have to keep track of how many bytes have been read yourself.

BinaryReader Skipping Ahead In Position

Oh I feel stupid, I must need to take a break or get a coffee. The problem was I was advancing the position for a comparison and not resetting it if it failed.

if (Binary.ReadByte().ToString("X2") == "D9")

Was advancing it the one byte and returning false, so I just needed to add and else saying

Binary.BaseStream.Position--;

BinaryReader on MemoryStream stay empty

Since you just wrote to the MemoryStream, the pointer is at the end where there is nothing to read. Reset the position back to 0 (zero) before attempting to read from it:

    mStream.Position = 0

C# BinaryReader notably slower. Alternative?

You have all the data upfront in an array in memory whereas BinaryReader streams the bytes in one at a time from the source which I guess is a file on disk. I guess you could speed it up by passing it a stream that reads from an in-memory array:

Stream stream = new MemoryStream(byteArray);
//Pass the stream to BinaryReader

Note that with this approach you need to fill the entire file in memory at once but I guess that's ok for you.



Related Topics



Leave a reply



Submit