Beispiel #1
0
        /// <inheritdoc/>
        public IByteBuffer GetSlice(int position, int length)
        {
            EnsureValidPositionAndLength(position, length);
            ThrowIfDisposed();

            return(_buffer.GetSlice(position + _offset, length));
        }
        private IByteBuffer CompressMessages(
            IEnumerable <RequestMessage> messages,
            IByteBuffer uncompressedBuffer,
            MessageEncoderSettings messageEncoderSettings)
        {
            var outputBufferChunkSource = new OutputBufferChunkSource(BsonChunkPool.Default);
            var compressedBuffer        = new MultiChunkBuffer(outputBufferChunkSource);

            using (var uncompressedStream = new ByteBufferStream(uncompressedBuffer, ownsBuffer: false))
                using (var compressedStream = new ByteBufferStream(compressedBuffer, ownsBuffer: false))
                {
                    foreach (var message in messages)
                    {
                        var uncompressedMessageLength = uncompressedStream.ReadInt32();
                        uncompressedStream.Position -= 4;

                        using (var uncompressedMessageSlice = uncompressedBuffer.GetSlice((int)uncompressedStream.Position, uncompressedMessageLength))
                            using (var uncompressedMessageStream = new ByteBufferStream(uncompressedMessageSlice, ownsBuffer: false))
                            {
                                if (message.MayBeCompressed)
                                {
                                    CompressMessage(message, uncompressedMessageStream, compressedStream, messageEncoderSettings);
                                }
                                else
                                {
                                    uncompressedMessageStream.EfficientCopyTo(compressedStream);
                                }
                            }
                    }
                    compressedBuffer.Length = (int)compressedStream.Length;
                }

            return(compressedBuffer);
        }
Beispiel #3
0
        /// <inheritdoc/>
        public override IByteBuffer ReadSlice()
        {
            ThrowIfDisposed();

            var position = _position;
            var length   = ReadInt32();

            ThrowIfEndOfStream(length - 4);
            Position = position + length;

            return(_buffer.GetSlice(position, length));
        }
        /// <summary>
        /// Gets the slice.
        /// </summary>
        /// <param name="position">The position.</param>
        /// <param name="length">The length.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentOutOfRangeException">position;Position is outside the stream.</exception>
        /// <exception cref="System.ArgumentException">
        /// Length is negative.;length
        /// or
        /// Length extends beyond the end of the stream.
        /// </exception>
        public IByteBuffer GetSlice(int position, int length)
        {
            if (position < 0 || position > _length)
            {
                throw new ArgumentOutOfRangeException("position", "Position is outside the stream.");
            }
            if (length < 0)
            {
                throw new ArgumentException("Length is negative.", "length");
            }
            if (position + length > _length)
            {
                throw new ArgumentException("Length extends beyond the end of the stream.");
            }

            return(_byteBuffer.GetSlice(position, length));
        }
Beispiel #5
0
        public override void WriteRawBsonDocument(IByteBuffer slice)
        {
            WriteStartDocument();

            if (Wrapped is BsonBinaryWriter binaryWriter)
            {
                // just copy the bytes (without the length and terminating null)
                var lengthBytes = new byte[4];
                slice.GetBytes(0, lengthBytes, 0, 4);
                var length = BitConverter.ToInt32(lengthBytes, 0);
                using (var elements = slice.GetSlice(4, length - 5))
                {
                    var stream = binaryWriter.BsonStream;
                    stream.WriteSlice(elements);
                }
            }
            else
            {
                throw new NotSupportedException("WriteRawBsonDocument supports only BsonBinaryWriter.");
            }

            WriteEndDocument();
        }
Beispiel #6
0
 // private methods
 private IByteBuffer CloneSlice()
 {
     return(_slice.GetSlice(0, _slice.Length));
 }