Reputation: 17
private bool ReadMagic(BinaryReader reader)
{
try
{
ini:
byte b0 = reader.ReadByte();
if (b0 != 0xF9) goto ini;
b0 = reader.ReadByte();
if (b0 != 0xbe) goto ini;
b0 = reader.ReadByte();
if (b0 != 0xb4) goto ini;
b0 = reader.ReadByte();
if (b0 != 0xd9) goto ini;
return true;
}
catch (EndOfStreamException)
{
return false;
}
}
I found this piece of code on github and was wondering why someone would use the ReadMagic function instead of the usual reader.Read() function? Also could someone explain how the ReadMagic function works?
using(var reader = command.ExecuteReader())
{
while(ReadMagic(reader));
}
Upvotes: 0
Views: 242
Reputation: 32202
A SqlDataReader
is very much not the same thing as a BinaryReader
, and you wouldn't use it as such. The BinaryReader
is for reading streams (eg reading from a file). It has methods such as ReadByte
, used here, for reading actual bytes from the stream, whereas SqlDataReader
uses Read
to advance to the next returned row, if there is one.
It looks like the ReadMagic
function is consuming bytes from the stream until it gets a series of 0xF9, 0xBE, 0xB4, 0xD9 - presumably some sort of marker in whatever stream it happens to be reading, then returning true
upon finding it.
Upvotes: 4