NVorbis.StreamReadBuffer.CalcBuffer C# (CSharp) Method

CalcBuffer() static private method

static private CalcBuffer ( long offset, int count, int &readStart, int &readEnd ) : void
offset long
count int
readStart int
readEnd int
return void
        void CalcBuffer(long offset, int count, out int readStart, out int readEnd)
        {
            readStart = 0;
            readEnd = 0;
            if (offset < _baseOffset)
            {
                // try to overlap the end...
                if (offset + _maxSize <= _baseOffset)
                {
                    // nope...
                    if (_baseOffset - (offset + _maxSize) > _maxSize)
                    {
                        // it's probably best to cache this buffer for a bit
                        CreateNewBuffer(offset, count);
                    }
                    else
                    {
                        // don't worry about caching...
                        EnsureBufferSize(count, false, 0);
                    }
                    _baseOffset = offset;
                    readEnd = count;
                }
                else
                {
                    // we have at least some overlap
                    readEnd = (int)(offset - _baseOffset);
                    EnsureBufferSize(Math.Min((int)(offset + _maxSize - _baseOffset), _end) - readEnd, true, readEnd);
                    readEnd = (int)(offset - _baseOffset) - readEnd;
                }
            }
            else
            {
                // try to overlap the beginning...
                if (offset >= _baseOffset + _maxSize)
                {
                    // nope...
                    if (offset - (_baseOffset + _maxSize) > _maxSize)
                    {
                        CreateNewBuffer(offset, count);
                    }
                    else
                    {
                        EnsureBufferSize(count, false, 0);
                    }
                    _baseOffset = offset;
                    readEnd = count;
                }
                else
                {
                    // we have at least some overlap
                    readEnd = (int)(offset + count - _baseOffset);
                    var ofs = Math.Max(readEnd - _maxSize, 0);
                    EnsureBufferSize(readEnd - ofs, true, ofs);
                    readStart = _end;
                    // re-pull in case EnsureBufferSize had to discard...
                    readEnd = (int)(offset + count - _baseOffset);
                }
            }
        }