private void Update(bool triggerEvent) { lock (lockObject) { if (disposed) { return; } //System.Diagnostics.Debug.WriteLine("Execute check"); lastUpdate = DateTime.UtcNow; updateScheduled = false; if (position >= 0 && origin == Origin.Begin || position < 0 && origin == Origin.End) { LineRange range = reader.ReadRange(position, origin, maxPrev, maxFoll, maxPrevExtent, maxFollExtent, fullUpdate ? null : curState); fullUpdate = false; if (origin == Origin.Begin && watchOrigin == Origin.End) { position -= range.StreamLength; origin = Origin.End; } else if (origin == Origin.End && watchOrigin == Origin.Begin) { position += range.StreamLength; origin = Origin.Begin; } if (range != curState || updateForced) { updateForced = false; curState = range; //System.Diagnostics.Debug.WriteLine("Invoke listener"); if (triggerEvent) { WatchedRangeChanged?.Invoke(this, curState); } } } else { curState = null; } } }
public virtual LineRange ReadRange(long position, Origin origin, int maxPrev, int maxNext, int maxPrevExt, int maxNextExt, LineRange currentState = null) { Stream stream = null; try { int cp = CurEncoding.CodePage; stream = GetStream(); if (stream == null) { HandleStreamUnavailable(); return(new LineRange()); } LineReader lineReader = new LineReader(stream, GetEncoding(stream)); if (origin == Origin.End) { position = stream.Length + position; if (position < 0) { position = 0; } } Line curLine = null; List <Line> prev = new List <Line>(); List <Line> next = new List <Line>(); List <Extent> prevExtent = new List <Extent>(); List <Extent> nextExtent = new List <Extent>(); Action <RawLine> feedDetector = (line) => { if (line != null) { FeedDetector(line, lineReader); } }; curLine = ReadRange(lineReader, position, maxPrev, maxNext, maxPrevExt, maxNextExt, prev, next, prevExtent, nextExtent, currentState, feedDetector); int enc = CurEncoding.CodePage; //foreach (Line line in prev) //{ // FeedDetector(line, lineReader); //} //if (curLine != null) FeedDetector(curLine, lineReader); //foreach (Line line in next) //{ // FeedDetector(line, lineReader); //} if (CurEncoding.CodePage != enc) { // Read line again with new encoding lineReader = new LineReader(stream, CurEncoding); curLine = ReadRange(lineReader, position, maxPrev, maxNext, maxPrevExt, maxNextExt, prev, next, prevExtent, nextExtent); } if (CurEncoding.CodePage != cp) { EcondingChanged?.Invoke(this, CurEncoding); } LineRange lineRange = new LineRange(curLine, prev.ConvertAll <Line>(l => (Line)l), next.ConvertAll <Line>(l => (Line)l), prevExtent, nextExtent, lineReader.StreamLength); CloseStream(stream); return(lineRange); } catch (Exception e) { HandleError(stream, e); return(new LineRange()); } }
public virtual Line GetLine(long position) { LineRange range = ReadRange(position, Origin.Begin, 0, 0, 0, 0); return(range.RequestedLine); }
private static Line ReadRange(LineReader lineReader, long position, int maxPrev, int maxNext, int maxPrevExtent, int maxNextExtent, List <Line> prev, List <Line> next, List <Extent> prevExtent, List <Extent> nextExtent, LineRange currentState = null, Action <RawLine> feedDetector = null) { prev.Clear(); next.Clear(); prevExtent.Clear(); nextExtent.Clear(); currentState = lineReader.StreamLength >= currentState?.StreamLength ? currentState : null; CachedReader reader = new CachedReader(lineReader, currentState, feedDetector); Line curLine = reader.Read(position); if (curLine != null) { Line l = curLine; for (int prevRead = 0; prevRead < maxPrev; ++prevRead) { l = reader.ReadPrevious(l); if (l != null) { prev.Insert(0, l); } else { break; } } if (l != null) { Extent e = l.Extent; for (int prevRead = 0; prevRead < maxPrevExtent; ++prevRead) { e = reader.GetLineExtent(e.Begin - 1); if (e != null) { prevExtent.Insert(0, e); } else { break; } } } l = curLine; for (int nextRead = 0; nextRead < maxNext; ++nextRead) { l = reader.ReadNext(l); if (l != null) { next.Add(l); } else { break; } } if (l != null) { Extent e = l.Extent; for (int nextRead = 0; nextRead < maxNextExtent; ++nextRead) { e = reader.GetLineExtent(e.End); if (e != null) { nextExtent.Add(e); } else { break; } } } } return(curLine); }
public CachedReader(LineReader lineReader, LineRange cache, Action <RawLine> feedDetector) { this.lineReader = lineReader; this.cache = cache; this.feedDetector = feedDetector; }
public override LineRange ReadRange(long position, Origin origin, int maxPrev, int maxNext, int maxPrevExt, int maxNExtExt, LineRange currentState = null) { CheckForFileModifications(); return(base.ReadRange(position, origin, maxPrev, maxNext, maxPrevExt, maxNExtExt, currentState)); }