When working with binary streams (i.e. byte[]
arrays), the main point of using BinaryReader
or BinaryWriter
seems to be simplified reading/writing of primitive data types from a stream, using methods such as ReadBoolean()
and taking encoding into account. Is that the whole story? Is there an inherent advantage or disadvantage if one works directly with a Stream
, without using BinaryReader/BinaryWriter
? Most methods, such as Read()
, seem to be the same in both classes, and my guess is that they work identically underneath.
Consider a simple example of processing a binary file in two different ways (edit: I realize this way is ineffective and that a buffer can be used, it's just a sample):
// Using FileStream directly
using (FileStream stream = new FileStream("file.dat", FileMode.Open))
{
// Read bytes from stream and interpret them as ints
int value = 0;
while ((value = stream.ReadByte()) != -1)
{
Console.WriteLine(value);
}
}
// Using BinaryReader
using (BinaryReader reader = new BinaryReader(FileStream fs = new FileStream("file.dat", FileMode.Open)))
{
// Read bytes and interpret them as ints
byte value = 0;
while (reader.BaseStream.Position < reader.BaseStream.Length)
{
value = reader.ReadByte();
Console.WriteLine(Convert.ToInt32(value));
}
}
The output will be the same, but what's happening internally (e.g. from OS perspective)? Is it - generally speaking - important which implementation is used? Is there any purpose to using BinaryReader/BinaryWriter
if you don't need the extra methods that they provide?
For this specific case, MSDN says this in regard to Stream.ReadByte()
:
The default implementation on Stream creates a new single-byte array and then calls Read. While this is formally correct, it is inefficient.
Using GC.GetTotalMemory()
, this first approach does seem to allocate 2x as much space as the second one, but AFAIK this shouldn't be the case if a more general Stream.Read()
method is used (e.g. for reading in chunks using a buffer). Still, it seems to me that these methods/interfaces could be unified easily...