Пример #1
0
 // internal methods
 internal virtual void ReadHeaderFrom(BsonStreamReader streamReader)
 {
     _messageLength = streamReader.ReadInt32();
     _requestId = streamReader.ReadInt32();
     _responseTo = streamReader.ReadInt32();
     if ((MessageOpcode)streamReader.ReadInt32() != _opcode)
     {
         throw new FileFormatException("Message header opcode is not the expected one.");
     }
 }
Пример #2
0
        /// <summary>
        /// Tries to get the node associated with a name read from a stream.
        /// </summary>
        /// <param name="streamReader">The stream.</param>
        /// <param name="node">The node.</param>
        /// <returns>
        /// True if the node was found.
        /// If the node was found the stream is advanced over the name, otherwise
        /// the stream is repositioned to the beginning of the name.
        /// </returns>
        public bool TryGetNode(BsonStreamReader streamReader, out BsonTrieNode <TValue> node)
        {
            var position = streamReader.Position;
            var utf8     = streamReader.ReadCStringBytes();

            if (TryGetNode(utf8, out node))
            {
                return(true);
            }

            streamReader.Position = position;
            return(false);
        }
        // public methods
        /// <summary>
        /// Reads the name.
        /// </summary>
        /// <param name="streamReader">The stream reader.</param>
        /// <returns>
        /// The name.
        /// </returns>
        public string Decode(BsonStreamReader streamReader)
        {
            BsonTrieNode <TValue> node;

            if (_trie.TryGetNode(streamReader, out node))
            {
                if (node.HasValue)
                {
                    _found = true;
                    _value = node.Value;
                    return(node.ElementName);
                }
            }

            return(streamReader.ReadCString());
        }
        /// <summary>
        /// Initializes a new instance of the BsonBinaryReader class.
        /// </summary>
        /// <param name="stream">A stream (BsonBinary does not own the stream and will not Dispose it).</param>
        /// <param name="settings">A BsonBinaryReaderSettings.</param>
        public BsonBinaryReader(Stream stream, BsonBinaryReaderSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _streamReader = new BsonStreamReader(stream, settings.Encoding);
            _settings = settings; // already frozen by base class

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
        /// <summary>
        /// Initializes a new instance of the BsonBinaryReader class.
        /// </summary>
        /// <param name="stream">A stream (BsonBinary does not own the stream and will not Dispose it).</param>
        /// <param name="settings">A BsonBinaryReaderSettings.</param>
        public BsonBinaryReader(Stream stream, BsonBinaryReaderSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _streamReader = new BsonStreamReader(stream, settings.Encoding);
            _settings     = settings; // already frozen by base class

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
        /// <summary>
        /// Loads a byte buffer from a stream (the first 4 bytes in the stream are the length of the data).
        /// Depending on the required capacity, either a SingleChunkBuffer or a MultiChunkBuffer will be created.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>A buffer.</returns>
        /// <exception cref="System.ArgumentNullException">stream</exception>
        public static IByteBuffer LoadLengthPrefixedDataFrom(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var streamReader = new BsonStreamReader(stream, Utf8Helper.StrictUtf8Encoding);
            var length = streamReader.ReadInt32();

            var byteBuffer = Create(BsonChunkPool.Default, length);
            byteBuffer.Length = length;
            byteBuffer.WriteBytes(0, BitConverter.GetBytes(length), 0, 4);
            byteBuffer.LoadFrom(stream, 4, length - 4);
            byteBuffer.MakeReadOnly();

            return byteBuffer;
        }
        /// <summary>
        /// Loads a byte buffer from a stream (the first 4 bytes in the stream are the length of the data).
        /// Depending on the required capacity, either a SingleChunkBuffer or a MultiChunkBuffer will be created.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>A buffer.</returns>
        /// <exception cref="System.ArgumentNullException">stream</exception>
        public static IByteBuffer LoadLengthPrefixedDataFrom(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var streamReader = new BsonStreamReader(stream, Utf8Helper.StrictUtf8Encoding);
            var length       = streamReader.ReadInt32();

            var byteBuffer = Create(BsonChunkPool.Default, length);

            byteBuffer.Length = length;
            byteBuffer.WriteBytes(0, BitConverter.GetBytes(length), 0, 4);
            byteBuffer.LoadFrom(stream, 4, length - 4);
            byteBuffer.MakeReadOnly();

            return(byteBuffer);
        }
 // public methods
 /// <summary>
 /// Decodes the name.
 /// </summary>
 /// <param name="streamReader">The stream reader.</param>
 /// <returns>
 /// The name.
 /// </returns>
 public string Decode(BsonStreamReader streamReader)
 {
     var utf8 = streamReader.ReadCStringBytes();
     return Utf8Helper.DecodeUtf8String(utf8.Array, utf8.Offset, utf8.Count, Utf8Helper.StrictUtf8Encoding);
 }
Пример #9
0
        // public methods
        /// <summary>
        /// Decodes the name.
        /// </summary>
        /// <param name="streamReader">The stream reader.</param>
        /// <returns>
        /// The name.
        /// </returns>
        public string Decode(BsonStreamReader streamReader)
        {
            var utf8 = streamReader.ReadCStringBytes();

            return(Utf8Helper.DecodeUtf8String(utf8.Array, utf8.Offset, utf8.Count, Utf8Helper.StrictUtf8Encoding));
        }
        private byte[] RemoveLastDocument(Stream stream)
        {
            var streamReader = new BsonStreamReader(stream, WriterSettings.Encoding);
            var lastDocumentLength = (int)streamReader.Position - _lastDocumentStartPosition;
            streamReader.Position = _lastDocumentStartPosition;
            var lastDocument = streamReader.ReadBytes(lastDocumentLength);
            streamReader.Position = _lastDocumentStartPosition;
            streamReader.BaseStream.SetLength(_lastDocumentStartPosition);

            _batchCount -= 1;
            _batchLength = (int)streamReader.Position - _batchStartPosition;

            return lastDocument;
        }