An IBsonBuffer that only has a single chunk.
Inheritance: ByteArrayBuffer
        /// <inheritdoc/>
        public IByteBuffer GetSlice(int position, int length)
        {
            ThrowIfDisposed();
            if (position < 0 || position > _length)
            {
                throw new ArgumentOutOfRangeException("position");
            }
            if (length < 0 || position + length > _length)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            EnsureIsReadOnly();

            var forkedBuffer = new SingleChunkBuffer(_chunk.Fork(), _length, isReadOnly: true);

            return(new ByteBufferSlice(forkedBuffer, position, length));
        }
Exemple #2
0
        /// <inheritdoc/>
        public IByteBuffer GetSlice(int position, int length)
        {
            ThrowIfDisposed();
            if (position < 0 || position > _length)
            {
                throw new ArgumentOutOfRangeException("position");
            }
            if (length < 0 || position + length > _length)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            EnsureIsReadOnly();

            if (length == 0)
            {
                return(new ByteArrayBuffer(new byte[0]));
            }

            var firstChunkIndex = GetChunkIndex(position);
            var lastChunkIndex  = GetChunkIndex(position + length - 1);

            IByteBuffer forkedBuffer;

            if (firstChunkIndex == lastChunkIndex)
            {
                var forkedChunk = _chunks[firstChunkIndex].Fork();
                forkedBuffer = new SingleChunkBuffer(forkedChunk, forkedChunk.Bytes.Count, isReadOnly: true);
            }
            else
            {
                var forkedChunks       = _chunks.Skip(firstChunkIndex).Take(lastChunkIndex - firstChunkIndex + 1).Select(c => c.Fork());
                var forkedBufferLength = _positions[lastChunkIndex + 1] - _positions[firstChunkIndex];
                forkedBuffer = new MultiChunkBuffer(forkedChunks, forkedBufferLength, isReadOnly: true);
            }

            var offset = position - _positions[firstChunkIndex];

            return(new ByteBufferSlice(forkedBuffer, offset, length));
        }
        /// <inheritdoc/>
        public IByteBuffer GetSlice(int position, int length)
        {
            ThrowIfDisposed();
            if (position < 0 || position > _length)
            {
                throw new ArgumentOutOfRangeException("position");
            }
            if (length < 0 || position + length > _length)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            EnsureIsReadOnly();

            if (length == 0)
            {
                return new ByteArrayBuffer(new byte[0]);
            }

            var firstChunkIndex = GetChunkIndex(position);
            var lastChunkIndex = GetChunkIndex(position + length - 1);

            IByteBuffer forkedBuffer;
            if (firstChunkIndex == lastChunkIndex)
            {
                var forkedChunk = _chunks[firstChunkIndex].Fork();
                forkedBuffer = new SingleChunkBuffer(forkedChunk, forkedChunk.Bytes.Count, isReadOnly: true);
            }
            else
            {
                var forkedChunks = _chunks.Skip(firstChunkIndex).Take(lastChunkIndex - firstChunkIndex + 1).Select(c => c.Fork());
                var forkedBufferLength = _positions[lastChunkIndex + 1] - _positions[firstChunkIndex];
                forkedBuffer = new MultiChunkBuffer(forkedChunks, forkedBufferLength, isReadOnly: true);
            }

            var offset = position - _positions[firstChunkIndex];
            return new ByteBufferSlice(forkedBuffer, offset, length);
        }
        private ByteBufferStream CreateSubject(int length, IEnumerable<IBsonChunk> chunks)
        {
            IByteBuffer buffer;
            if (chunks.Count() == 1)
            {
                var chunk = chunks.First();
                buffer = new SingleChunkBuffer(chunk, length);
            }
            else
            {
                buffer = new MultiChunkBuffer(chunks, length);
            }

            return new ByteBufferStream(buffer, ownsBuffer: true);
        }
        /// <inheritdoc/>
        public IByteBuffer GetSlice(int position, int length)
        {
            ThrowIfDisposed();
            if (position < 0 || position > _length)
            {
                throw new ArgumentOutOfRangeException("position");
            }
            if (length < 0 || position + length > _length)
            {
                throw new ArgumentOutOfRangeException("length");
            }
            EnsureIsReadOnly();

            var forkedBuffer = new SingleChunkBuffer(_chunk.Fork(), _length, isReadOnly: true);
            return new ByteBufferSlice(forkedBuffer, position, length);
        }
 public Reflector(SingleChunkBuffer instance)
 {
     _instance = instance;
 }
        public void constructor_should_initialize_subject(
            [Values(1, 2)]
            int length,
            [Values(false, true)]
            bool isReadOnly)
        {
            var chunk = CreateFakeChunk(length);

            var subject = new SingleChunkBuffer(chunk, length, isReadOnly);

            var reflector = new Reflector(subject);
            subject.IsReadOnly.Should().Be(isReadOnly);
            subject.Length.Should().Be(length);
            reflector._chunk.Should().BeSameAs(chunk);
            reflector._disposed.Should().BeFalse();
        }