寻找FileStream,然后使用StreamReader从那里读取

时间:2016-07-15 14:08:03

标签: c# filestream streamreader

所以我希望能够在fileStream中寻找一个点,然后使用StreamReader向前读取。然后再次向前搜索,并使用StreamReader读取另一块数据。

const int BufferSize = 4096;
var buffer = new char[BufferSize];

var endpoints = new List<long>();

using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{ 
    var fileLength = fileStream.Length;

    var seekPositionCount = fileLength / concurrentReads;

    long currentOffset = 0;
    for (var i = 0; i < concurrentReads; i++)
    {
        var seekPosition = seekPositionCount + currentOffset;

        // seek the file forward
        fileStream.Seek(seekPosition, SeekOrigin.Current);

        // setting true at the end is very important, keeps the underlying fileStream open.
        using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true, BufferSize, true))
        {
            // this also seeks the file forward the amount in the buffer...
            int bytesRead;
            var totalBytesRead = 0;
            while ((bytesRead = await streamReader.ReadAsync(buffer, 0, buffer.Length)) > 0)
            {
                totalBytesRead += bytesRead;

                var found = false;

                var gotR = false;

                for (var j = 0; j < buffer.Length; j++)
                {
                    if (buffer[j] == '\r')
                    {
                        gotR = true;
                        continue;
                    }

                    if (buffer[j] == '\n' && gotR)
                    {
                        // so we add the total bytes read, minus the current buffer amount read, then add how far into the buffer we actually read.
                        seekPosition += totalBytesRead - BufferSize + j;
                        endpoints.Add(seekPosition);
                        found = true;
                        break;
                    }
                }

                if (found) break;
            }
        }

        // we need to seek to the position we got to in the StreamReader (but not going by how much was read).
        fileStream.Seek(seekPosition, SeekOrigin.Current);

        currentOffset += seekPosition;
    }
}

return endpoints;

但是,我在endpoints中找到了两个条目并退出了。

(bytesRead = await streamReader.ReadAsync(buffer, 0, buffer.Length)) > 0

您传递给ReadAsync的参数我认为仅与缓冲区有关,因此我想要的index参数说明,填写buffer index

我无法从Reference Source了解如何使用此值。

我假设(并且无法找到要备份的证据),当您打开StreamReader时,它会使用基础Stream作为指导,所以当您要求读取一些字节时,它将从基础Stream所在的位置开始......

但是我正在做的结果并没有显示出来,他们似乎表明StreamReader每次都在Stream的开头 - 但是,我可以'找到证据来支持它是如何做到的......

我对寻求正确的理解是,如果我称之为寻求

fileStream.Seek(seekPosition, SeekOrigin.Current);

如果文件位于300,我想寻求600,上述变量seekPosition应为600 ??

ReferenceSource会另外说:

else if (origin == SeekOrigin.Current) {
    // Don't call FlushRead here, which would have caused an infinite
    // loop.  Simply adjust the seek origin.  This isn't necessary
    // if we're seeking relative to the beginning or end of the stream.
    offset -= (_readLen - _readPos);
}

1 个答案:

答案 0 :(得分:0)

感谢Hans Passant,我得到了答案:

var buffer = new char[BufferSize];

var endpoints = new List<long>();

using (var fileStream = this.CreateMultipleReadAccessFileStream(fileName))
{
    var fileLength = fileStream.Length;

    var seekPositionCount = fileLength / concurrentReads;

    long currentOffset = 0;
    for (var i = 0; i < concurrentReads; i++)
    {
        var seekPosition = seekPositionCount + currentOffset;

        // seek the file forward
        // fileStream.Seek(seekPosition, SeekOrigin.Current);

        // setting true at the end is very important, keeps the underlying fileStream open.
        using (var streamReader = this.CreateTemporaryStreamReader(fileStream))
        {
            // this is poor on performance, hence why you split the file here and read in new threads.
            streamReader.DiscardBufferedData();
            // you have to advance the fileStream here, because of the previous line
            streamReader.BaseStream.Seek(seekPosition, SeekOrigin.Begin);
            // this also seeks the file forward the amount in the buffer...
            int bytesRead;
            var totalBytesRead = 0;
            while ((bytesRead = await streamReader.ReadAsync(buffer, 0, buffer.Length)) > 0)
            {
                totalBytesRead += bytesRead;

                var found = false;

                var gotR = false;

                for (var j = 0; j < buffer.Length; j++)
                {
                    if (buffer[j] == '\r')
                    {
                        gotR = true;
                        continue;
                    }

                    if (buffer[j] == '\n' && gotR)
                    {
                        // so we add the total bytes read, minus the current buffer amount read, then add how far into the buffer we actually read.
                        seekPosition += totalBytesRead - BufferSize + j;
                        endpoints.Add(seekPosition);
                        found = true;
                        break;
                    }
                    // if we have found new line then move the position to 
                }

                if (found) break;
            }
        }

        currentOffset = seekPosition;
    }
}

return endpoints;

注意新部分,而不是两次

fileStream.Seek(seekPosition, SeekOrigin.Current);

我现在使用SeekOrigin.Begin并使用StreamReader来推进基础基础流:

// this is poor on performance, hence why you split the file here and read in new threads.
streamReader.DiscardBufferedData();
// you have to advance the fileStream here, because of the previous line
streamReader.BaseStream.Seek(seekPosition, SeekOrigin.Begin);

DiscardBufferedData意味着我总是使用基础流位置。