Skip to content

Read Twice #1

@iaiuiuw

Description

@iaiuiuw

I am looking for some methods help me to read log files from tail to head.
your project is great!
but int this code https://github.com/blakepell/Argus.IO.ReverseFileReader/blob/master/src/Argus.IO.ReverseFileReader/IO/ReverseFileReader.cs

byte[] buf = new byte[1];
            long position = _stream.Position;

            while (_stream.Position > 0)
            {
                buf.Initialize();

                _stream.Seek(-1, SeekOrigin.Current);

                // Read one char
                _stream.Read(buf, 0, 1);

                // Move it back
                _stream.Seek(-1, SeekOrigin.Current);

                if (buf[0] == 10)
                {
                    // Move it back again.
                    _stream.Seek(-1, SeekOrigin.Current);

                    _stream.Read(buf, 0, 1);

                    if (buf[0] == 13)
                    {
                        break;
                    }
                }
            }

            int count = (int)(position - _stream.Position);
            byte[] line = new byte[count];

            _stream.Read(line, 0, count);
            _stream.Seek(-count, SeekOrigin.Current);

            return this.Encoding.GetString(line).Trim(new[] { '\r', '\n' });

Read same char twice, you can improve it.
By the way, _stream.Read(buf, 0, 1); is slow.
_stream.Read(buf, 0, bufferSize); char ch = buf[wantedPosition - bufferStartPosition]; may be better

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions