views:

38

answers:

3

... that I can read into a BlockingCollection<byte[]> in the same way that System.IO.File.ReadLines() can be read into a BlockingCollection<string> in .net4.0?

+1  A: 
    public IEnumerable<Byte[]> ReadFile(String fileName)
    {
        using (FileStream file = new FileStream(fileName, FileMode.Open))
        {
            using (StreamReader reader = new StreamReader(file))
            {
                while (reader.Peek() >= 0)
                {
                    String line = reader.ReadLine();
                    yield return System.Text.Encoding.Default.GetBytes(line);
                }
            }
        }
    }
decyclone
I guess this is the basis, but the OQ is about byte[], not byte.
Henk Holterman
This will return IEnumerable<byte>, not IEnumerable<byte[]>...
Reed Copsey
Thanks guys, corrected the code!
decyclone
+1  A: 

You could use File.Open to get a FileStream, and then use FileStream.Read:

IEnumerable<byte[]> GetFileBytes(string filename)
{
    var fsSource = File.Open(filename, FileMode.Open);
    const int bytesToReadPerIteration = 100;
    int numBytesToRead = (int)fsSource.Length;
    int numBytesRead = 0;
    while (numBytesToRead > 0)
    {
        byte[] bytes = new byte[Math.Min(bytesToReadPerIteration, numBytesToRead)];
        // Read may return anything from 0 to numBytesToRead.
        int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

        // Break when the end of the file is reached.
        if (n == 0)
            break;

        if (n != bytes.Length)
        {
             byte[] tmp = new byte[n];
             Array.Copy(bytes, tmp, n);
             bytes = tmp;
        }

        yield return bytes;

        numBytesRead += n;
        numBytesToRead -= n;
    }

    fsSource.Close();
}
Reed Copsey
A: 

Sounds like "yield return" is what you're looking for:

    static IEnumerable<byte> GetBytes()
    {
        byte[] bytes = new byte[10];
        // simple initialization of the array, replace with your I/O here if blocking
        for (byte x = 0; x < bytes.Length; x++)
        {
            bytes[x] = x;
        }
        // this generates the IEnumerable<byte>.
        // Replace "bytes[x]" with an I/O operation
        // that returns one byte if you want to allow data to flow as available through
        // the IEnumerable<byte>
        for (int x = 0; x < bytes.Length; x++)
        {
            yield return bytes[x];
        }
    }
David Gladfelter