private IEnumerable<Line> ReadLinesByPosition(ScrollRequest scroll) { //TODO: Calculate initial index of first item. //scroll from specified position using (var stream = File.Open(Info.FullName, FileMode.Open, FileAccess.Read,FileShare.Delete | FileShare.ReadWrite)) { int taken = 0; using (var reader = new StreamReaderExtended(stream, Encoding, false)) { var startPosition = scroll.Position; var first = (int)CalculateIndexByPositon(startPosition); reader.BaseStream.Seek(startPosition, SeekOrigin.Begin); do { var line = reader.ReadLine(); if (line==null) yield break; var endPosition = reader.AbsolutePosition(); var info = new LineInfo(first + taken + 1, first + taken, startPosition, endPosition); var ontail = endPosition >= TailInfo.TailStartsAt && DateTime.Now.Subtract(TailInfo.LastTail).TotalSeconds < 1 ? DateTime.Now : (DateTime?)null; yield return new Line(info, line, ontail); startPosition = endPosition; taken++; } while (taken < scroll.PageSize); } } }
private IEnumerable<Line> ReadLinesByIndex(ScrollRequest scroll) { var page = GetPage(scroll); var relativeIndex = CalculateRelativeIndex(page.Start); if (relativeIndex == null) yield break; var offset = relativeIndex.LinesOffset; using (var stream = File.Open(Info.FullName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite)) { using (var reader = new StreamReaderExtended(stream, Encoding, false)) { //go to starting point stream.Seek(relativeIndex.Start, SeekOrigin.Begin); if (offset > 0) { //skip number of lines offset for (int i = 0; i < offset; i++) reader.ReadLine(); } //if estimate move to the next start of line if (relativeIndex.IsEstimate && relativeIndex.Start != 0) reader.ReadLine(); foreach (var i in Enumerable.Range(page.Start, page.Size)) { var startPosition = reader.AbsolutePosition(); var line = reader.ReadLine(); var endPosition = reader.AbsolutePosition(); var info = new LineInfo(i + 1, i, startPosition, endPosition); var ontail = startPosition >= TailInfo.TailStartsAt && DateTime.Now.Subtract(TailInfo.LastTail).TotalSeconds < 1 ? DateTime.Now : (DateTime?)null; yield return new Line(info, line, ontail); } } } }
public static IEnumerable<Line> ReadLinesByPosition(this FileInfo source, long[] positions, Func<int, bool> isEndOfTail = null) { using (var stream = File.Open(source.FullName, FileMode.Open, FileAccess.Read, FileShare.Delete | FileShare.ReadWrite)) { using (var reader = new StreamReaderExtended(stream, Encoding.Default, true)) { foreach (var position in positions) { if (reader.AbsolutePosition() != position) { reader.DiscardBufferedData(); stream.Seek(position, SeekOrigin.Begin); } var line = reader.ReadLine(); yield return new Line((int)position, line,null); } } } }