< Summary

Information
Class: ValidateLib.TabularData.Parsing.CustomStreamReader
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Parsing\CustomStreamReader.cs
Line coverage
100%
Covered lines: 41
Uncovered lines: 0
Coverable lines: 41
Total lines: 120
Line coverage: 100%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Parsing\CustomStreamReader.cs

#LineLine coverage
 1namespace ValidateLib.TabularData.Parsing
 2{
 3    using System;
 4    using System.IO;
 5    using System.Runtime.CompilerServices;
 6    using System.Text;
 7
 8    /// <summary>
 9    /// Abstraction above classical <see cref="StreamReader"/> so that we buff some of the
 10    /// characters and can move and forward in the stream in O(1) time.
 11    /// </summary>
 12    public class CustomStreamReader : StreamReader
 13    {
 14        private char[] buffer;
 15        private int bufferSize;
 16        private int charsCount;
 17        private int position;
 18        private int charsRead;
 19        private int maxIndex;
 120        private bool firstFill = true;
 21        private bool endOfStream = false;
 22
 123        public CustomStreamReader(Stream stream, int bufferSize, int charsCount) : base(stream, Encoding.UTF8, true, buf
 24        {
 125            this.bufferSize = bufferSize;
 126            this.charsCount = charsCount;
 127            buffer = new char[bufferSize];
 128            position = 0;
 129            maxIndex = bufferSize - 1;
 30
 31
 32            // Read the initial characters into the buffer
 133            FillBuffer();
 134        }
 35
 136        private bool IsPositionInBounds() => position <= maxIndex;
 137        private bool IsPositionOutOfBounds() => position > maxIndex;
 38
 39        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 40        // Read the next character in the buffer and move the pointer
 41        public int ReadChar()
 42        {
 143            if (IsPositionInBounds())
 44            {
 145                char nextChar = buffer[position++];
 146                if (IsPositionOutOfBounds())
 47                {
 148                    FillBuffer();
 49                }
 150                return nextChar; // Return the character as an integer
 51            }
 52
 153            return ReaderConstants.END_OF_STREAM_CONSTANT; // End of stream
 54        }
 55
 56        /// <summary>
 57        /// Read the specified number of characters into the provided array and move the pointer
 58        /// </summary>
 59        /// <param name="count">number of chars to read</param>
 60        /// <param name="resultArray"> array where to put the read chars</param>
 61        /// <returns> number of chars actually read</returns>
 62        public int ReadChars(int count, char[] resultArray)
 63        {
 164            int charsRead = 0;
 65
 166            while (charsRead < count)
 67            {
 168                int nextSymbol = ReadChar();
 169                if (nextSymbol == ReaderConstants.END_OF_STREAM_CONSTANT)
 70                    break;
 71
 172                resultArray[charsRead] = (char)nextSymbol;
 173                charsRead++;
 74            }
 75
 176            return charsRead;
 77        }
 78
 79        // Move back on the pointer inside the buffer
 80        public void MoveBack(int count)
 81        {
 182            position = Math.Max(0, position - count);
 183        }
 84
 85        /// <summary>
 86        /// Fill the buffer by reading from the underlying StreamReader
 87        /// </summary>
 88        /// <returns> True if there were any chars left in the underlying stream</returns>
 89        private bool FillBuffer()
 90        {
 191            if (firstFill)
 92            {
 193                charsRead = base.Read(buffer, 0, bufferSize);
 194                maxIndex = charsRead - 1;
 195                position = 0;
 196                firstFill = false;
 197                if (charsRead < bufferSize)
 198                    endOfStream = true;
 99            }
 100            else
 101            {
 102                // we rotate the end of the buffer to the front
 1103                if (!endOfStream)
 104                {
 1105                    Array.Copy(buffer, maxIndex + 1 - charsCount, buffer, 0, charsCount);
 1106                    charsRead = base.Read(buffer, charsCount, bufferSize - charsCount);
 1107                    maxIndex = charsCount + charsRead - 1;
 1108                    position = charsCount;
 109                }
 110            }
 111
 1112            if (charsRead == 0)
 113            {
 1114                endOfStream = true;
 115            }
 1116            return charsRead > 0;
 117        }
 118
 119    }
 120}