Пример #1
0
        public long AbsolutePosition()
        {
            // The number of bytes that the already-read characters need when encoded.
            int numReadBytes = CurrentEncoding.GetByteCount(_charBuffer, 0, _charPos);

            return(BaseStream.Position - _byteLen + numReadBytes);
        }
        public override int Read()
        {
            var ch = base.Read();

            _position += CurrentEncoding.GetByteCount(new char[] { (char)ch });
            return(ch);
        }
    public override string ReadLine()
    {
        string line = base.ReadLine();

        if (line != null)
        {
            _position += CurrentEncoding.GetByteCount(line);
        }
        _position += CurrentEncoding.GetByteCount(Environment.NewLine);
        return(line);
    }
Пример #4
0
        /// <summary>
        /// Gets the actual position
        /// </summary>
        /// <returns></returns>
        public long GetActualPosition()
        {
            //https://stackoverflow.com/questions/5404267/streamreader-and-seeking

            var numBytesLeft = CurrentEncoding.GetByteCount(_charBuffer, _charPos, _charLen - _charPos);

            // For variable-byte encodings, deal with partial chars at the end of the buffer
            var numFragments = 0;

            if (_byteLen <= 0 || CurrentEncoding.IsSingleByte)
            {
                return(BaseStream.Position - numBytesLeft - numFragments);
            }

            switch (CurrentEncoding.CodePage)
            {
            // UTF-8
            case 65001:
            {
                byte byteCountMask = 0;

                while (_byteBuffer[_byteLen - numFragments - 1] >> 6 == 2
                       )  // if the byte is "10xx xxxx", it's a continuation-byte
                {
                    byteCountMask |=
                        (byte)(1 << ++numFragments);         // count bytes & build the "complete char" mask
                }
                if (_byteBuffer[_byteLen - numFragments - 1] >> 6 == 3
                    )     // if the byte is "11xx xxxx", it starts a multi-byte char.
                {
                    byteCountMask |=
                        (byte)(1 << ++numFragments);         // count bytes & build the "complete char" mask
                }
                // see if we found as many bytes as the leading-byte says to expect
                if (numFragments > 1 && _byteBuffer[_byteLen - numFragments] >> (7 - numFragments)
                    == byteCountMask)
                {
                    numFragments = 0;         // no partial-char in the byte-buffer to account for
                }
                break;
            }

            // UTF-16LE
            case 1200:
            {
                if (_byteBuffer[_byteLen - 1] >= 0xd8)       // high-surrogate
                {
                    numFragments = 2;                        // account for the partial character
                }
                break;
            }

            // UTF-16BE
            case 1201:
            {
                if (_byteBuffer[_byteLen - 2] >= 0xd8)       // high-surrogate
                {
                    numFragments = 2;                        // account for the partial character
                }
                break;
            }
            }

            return(BaseStream.Position - numBytesLeft - numFragments);
        }