Пример #1
0
        private void FeedDetector(RawLine line, LineReader lineReader)
        {
            if (detector != null && !detector.IsDone() && line.End >= encodingBuffer.Length)
            {
                // The encoding buffer could have a part of a character as last byte --> Read the
                // rest of this line
                if (!lineAtBufferEndCompleted)
                {
                    RawLine bufferEndLine = lineReader.Read(encodingBuffer.Length - 1);
                    if (bufferEndLine.End > encodingBuffer.Length)
                    {
                        FeedDetector(bufferEndLine.Bytes, (int)(encodingBuffer.Length - bufferEndLine.Begin), (int)(bufferEndLine.End - encodingBuffer.Length));
                    }
                    if (bufferEndLine.End < lineReader.StreamLength)
                    {
                        lineAtBufferEndCompleted = true;
                    }
                }

                if (line.Begin >= encodingBuffer.Length)
                {
                    FeedDetector(line.Bytes, 0, line.Bytes.Length);
                }
                detector.DataEnd();
                encoding = EncodingNameConversion(detector.Charset);
            }
        }
Пример #2
0
            public Line Read(long position)
            {
                Line line = null;

                if (cache?.RequestedLine != null)
                {
                    if (cache.RequestedLine.Begin <= position && cache.RequestedLine.End > position)
                    {
                        line = cache.RequestedLine;
                    }
                    else if (cache.PreviousLines.Count > 0 && cache.PreviousLines[0].Begin <= position && cache.PreviousLines[cache.PreviousLines.Count - 1].End > position)
                    {
                        line = Find(cache.PreviousLines, position);
                    }
                    else if (cache.NextLines.Count > 0 && cache.NextLines[0].Begin <= position && cache.NextLines[cache.NextLines.Count - 1].End > position)
                    {
                        line = Find(cache.PreviousLines, position);
                    }
                }
                if (line != null && line.End < cache.StreamLength)
                {
                    return(line);
                }

                RawLine rawLine = lineReader.Read(position);

                feedDetector?.Invoke(rawLine);
                return(rawLine);
            }
Пример #3
0
        public string FetchRange(long beginPosFirstLine, int beginCol, long endPosLastLine, int endCol)
        {
            Stream stream = null;

            try
            {
                stream = GetStream();
                if (stream == null)
                {
                    HandleStreamUnavailable();
                    return(String.Empty);
                }

                LineReader lineReader = new LineReader(stream, GetEncoding(stream));
                RawLine    firstLine  = lineReader.Read(beginPosFirstLine);
                RawLine    lastLine   = lineReader.Read(endPosLastLine - 1);

                // Special case: Begin and end in the same line
                if (firstLine.Extent == lastLine.Extent)
                {
                    if (firstLine.Content.Length > beginCol && endCol > beginCol)
                    {
                        return(firstLine.Content.Substring(beginCol, endCol - beginCol));
                    }
                    else
                    {
                        return(String.Empty);
                    }
                }

                StringBuilder sb = new StringBuilder();

                // First line:
                if (firstLine.Content.Length > beginCol)
                {
                    sb.Append(firstLine.Content.Substring(beginCol));
                }

                // Lines between
                if (firstLine.End < lastLine.Begin)
                {
                    byte[] bytes = lineReader.Read(firstLine.End, lastLine.Begin);
                    sb.Append(lineReader.Encoding.GetString(bytes));
                }

                // Last line:
                if (lastLine.Content.Length > endCol)
                {
                    sb.Append(lastLine.Content.Substring(0, endCol));
                }
                else
                {
                    sb.Append(lastLine.Content);
                }

                return(sb.ToString());
            }
            catch (Exception e)
            {
                HandleError(stream, e);
                return(String.Empty);
            }
        }