Пример #1
0
        private int InternalRead(Span <byte> buffer, int fromPosition)
        {
            if (this.length - fromPosition <= 0)
            {
                return(0);
            }
            if (this.largeBuffer == null)
            {
                BlockAndOffset andRelativeOffset = this.GetBlockAndRelativeOffset(fromPosition);
                int            start             = 0;
                int            val2 = Math.Min(buffer.Length, this.length - fromPosition);
                while (val2 > 0)
                {
                    int length = Math.Min(this.blocks[andRelativeOffset.Block].Length - andRelativeOffset.Offset, val2);
                    this.blocks[andRelativeOffset.Block].AsSpan <byte>(andRelativeOffset.Offset, length).CopyTo(buffer.Slice(start));
                    start += length;
                    val2  -= length;
                    ++andRelativeOffset.Block;
                    andRelativeOffset.Offset = 0;
                }
                return(start);
            }
            int length1 = Math.Min(buffer.Length, this.length - fromPosition);

            this.largeBuffer.AsSpan <byte>(fromPosition, length1).CopyTo(buffer);
            return(length1);
        }
Пример #2
0
        private int InternalRead(byte[] buffer, int offset, int count, int fromPosition)
        {
            if (this.length - fromPosition <= 0)
            {
                return(0);
            }
            if (this.largeBuffer == null)
            {
                BlockAndOffset andRelativeOffset = this.GetBlockAndRelativeOffset(fromPosition);
                int            num  = 0;
                int            val2 = Math.Min(count, this.length - fromPosition);
                while (val2 > 0)
                {
                    int count1 = Math.Min(this.blocks[andRelativeOffset.Block].Length - andRelativeOffset.Offset, val2);
                    Buffer.BlockCopy((Array)this.blocks[andRelativeOffset.Block], andRelativeOffset.Offset, (Array)buffer, num + offset, count1);
                    num  += count1;
                    val2 -= count1;
                    ++andRelativeOffset.Block;
                    andRelativeOffset.Offset = 0;
                }
                return(num);
            }
            int count2 = Math.Min(count, this.length - fromPosition);

            Buffer.BlockCopy((Array)this.largeBuffer, fromPosition, (Array)buffer, offset, count2);
            return(count2);
        }
Пример #3
0
        public override void Write(ReadOnlySpan <byte> source)
        {
            this.CheckDisposed();
            int  blockSize = this.memoryManager.BlockSize;
            long num1      = (long)this.position + (long)source.Length;

            if (num1 > (long)int.MaxValue)
            {
                throw new IOException("Maximum capacity exceeded");
            }
            this.EnsureCapacity((int)num1);
            if (this.largeBuffer == null)
            {
                BlockAndOffset andRelativeOffset = this.GetBlockAndRelativeOffset(this.position);
                while (source.Length > 0)
                {
                    byte[] block = this.blocks[andRelativeOffset.Block];
                    int    num2  = Math.Min(blockSize - andRelativeOffset.Offset, source.Length);
                    source.Slice(0, num2).CopyTo(block.AsSpan <byte>(andRelativeOffset.Offset));
                    source = source.Slice(num2);
                    ++andRelativeOffset.Block;
                    andRelativeOffset.Offset = 0;
                }
            }
            else
            {
                source.CopyTo(this.largeBuffer.AsSpan <byte>(this.position));
            }
            this.position = (int)num1;
            this.length   = Math.Max(this.position, this.length);
        }
Пример #4
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            this.CheckDisposed();
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), (object)offset, "Offset must be in the range of 0 - buffer.Length-1");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(count), (object)count, "count must be non-negative");
            }
            if (count + offset > buffer.Length)
            {
                throw new ArgumentException("count must be greater than buffer.Length - offset");
            }
            int  blockSize = this.memoryManager.BlockSize;
            long num1      = (long)this.position + (long)count;

            if (num1 > (long)int.MaxValue)
            {
                throw new IOException("Maximum capacity exceeded");
            }
            this.EnsureCapacity((int)num1);
            if (this.largeBuffer == null)
            {
                int            val2 = count;
                int            num2 = 0;
                BlockAndOffset andRelativeOffset = this.GetBlockAndRelativeOffset(this.position);
                while (val2 > 0)
                {
                    byte[] block  = this.blocks[andRelativeOffset.Block];
                    int    count1 = Math.Min(blockSize - andRelativeOffset.Offset, val2);
                    Buffer.BlockCopy((Array)buffer, offset + num2, (Array)block, andRelativeOffset.Offset, count1);
                    val2 -= count1;
                    num2 += count1;
                    ++andRelativeOffset.Block;
                    andRelativeOffset.Offset = 0;
                }
            }
            else
            {
                Buffer.BlockCopy((Array)buffer, offset, (Array)this.largeBuffer, this.position, count);
            }
            this.position = (int)num1;
            this.length   = Math.Max(this.position, this.length);
        }
Пример #5
0
        public int SafeReadByte(ref int streamPosition)
        {
            this.CheckDisposed();
            if (streamPosition == this.length)
            {
                return(-1);
            }
            byte num;

            if (this.largeBuffer == null)
            {
                BlockAndOffset andRelativeOffset = this.GetBlockAndRelativeOffset(streamPosition);
                num = this.blocks[andRelativeOffset.Block][andRelativeOffset.Offset];
            }
            else
            {
                num = this.largeBuffer[streamPosition];
            }
            ++streamPosition;
            return((int)num);
        }