Skip to content

Instantly share code, notes, and snippets.

@maslevx
Created June 17, 2015 17:58
Show Gist options
  • Save maslevx/b25cfaa4a5d35b3a66bd to your computer and use it in GitHub Desktop.
Save maslevx/b25cfaa4a5d35b3a66bd to your computer and use it in GitHub Desktop.
public static IEnumerable<byte[]> LazyReadBinaryFile(string fileName, int recordLen)
{
byte[] buffer = new byte[recordLen];
using (var stream = File.OpenRead(fileName))
{
long remaining = stream.Length;
int read = 0;
while (remaining > 0)
{
int c = stream.Read(buffer, read, recordLen - read);
remaining -= c;
read += c;
if (read == recordLen)
{
read = 0;
byte[] rtn = new byte[recordLen];
Buffer.BlockCopy(buffer, 0, rtn, 0, recordLen);
yield return rtn;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment