コード例 #1
0
        private void PrepareFileInfoForCorrectFile()
        {
            byte first = Convert.ToByte(m_Stream.ReadByte());

            m_Info = new NumberFileInfo()
            {
                IsInvalid  = false,
                IsNegative = first == Minus_ASCI
            };

            m_Info.RealPartPosition = new FileRangeInfo()
            {
                From = (first == Minus_ASCI || first == Plus_ASCI) ? 1 : 0,
                To   = m_Stream.Length - 1
            };
        }
コード例 #2
0
        public async Task PrepareFileInfoAsync(bool isFileCorrect)
        {
            if (m_Stream.Position != 0)
            {
                m_Stream.Seek(0, SeekOrigin.Begin);
            }

            if (isFileCorrect)
            {
                PrepareFileInfoForCorrectFile();
                return;
            }

            m_Info = null;

            int  readCount;
            bool toPositionWasFound = false;

            byte[] buffer = new byte[Math.Min(Buffer, m_Stream.Length)];
            bool   crWasDetectedBefore    = false;
            long   lastNumberIndex        = -1;
            int?   lastZeroIndexFromStart = null;

            // Need to Check if file info is ok.
            while ((readCount = await m_Stream.ReadAsync(buffer, 0, buffer.Length)) != 0)
            {
                bool isFirstRead = m_Info == null;

                if (buffer.Length > 0 && isFirstRead)
                {
                    var first = buffer[0];

                    if (first != Plus_ASCI && first != Minus_ASCI && !IsNumber(first))
                    {
                        m_Info = new NumberFileInfo()
                        {
                            IsInvalid = true
                        };

                        break;
                    }
                    else
                    {
                        m_Info = new NumberFileInfo()
                        {
                            IsNegative = first == Minus_ASCI
                        };

                        m_Info.RealPartPosition = new FileRangeInfo()
                        {
                            From = first == Minus_ASCI || first == Plus_ASCI ? 1 : 0
                        };

                        if (IsNumber(first))
                        {
                            lastNumberIndex = 0;
                        }

                        if (first == Number_Zero)
                        {
                            lastZeroIndexFromStart = 0;
                        }
                    }
                }

                byte value;
                var  streamPosition = m_Stream.Position;
                var  bufferLength   = buffer.Length;

                for (int i = isFirstRead ? 1 : 0; i < bufferLength; i++)
                {
                    value = buffer[i];

                    // ignore cr - can exist before new line
                    if (value == CR)
                    {
                        crWasDetectedBefore = true;
                        continue;
                    }

                    if (lastZeroIndexFromStart.HasValue || (lastNumberIndex == -1))
                    {
                        if (value == Number_Zero)
                        {
                            lastZeroIndexFromStart = i;
                        }
                        else
                        {
                            // skip 0
                            if (lastZeroIndexFromStart.HasValue)
                            {
                                m_Info.RealPartPosition.From = lastZeroIndexFromStart.Value + 1;
                            }

                            lastZeroIndexFromStart = null;
                        }
                    }

                    // Take real position from begin of the file.
                    var realPosition = streamPosition - readCount + i;

                    if (value == NewLine)
                    {
                        if (m_Info.RealPartPosition.To == 0)
                        {
                            toPositionWasFound         = true;
                            m_Info.RealPartPosition.To = realPosition - (crWasDetectedBefore ? 2 : 1);
                        }

                        crWasDetectedBefore = false;
                        break;
                    }
                    else if (!IsNumber(value))
                    {
                        m_Info.IsInvalid = true;
                        break;
                    }
                    else
                    {
                        lastNumberIndex = realPosition;
                    }
                }
            }

            if (m_Info.RealPartPosition != null && m_Info.RealPartPosition.To == 0 && !toPositionWasFound)
            {
                if (lastNumberIndex >= m_Info.RealPartPosition.From)
                {
                    m_Info.RealPartPosition.To = m_Stream.Length - 1;
                }
                else
                {
                    m_Info.RealPartPosition = null;
                }
            }

            if (m_Info.RealPartPosition == null || m_Info.RealPartPosition.GetLength() <= 0)
            {
                m_Info.IsInvalid = true;
            }
        }