Exemple #1
0
        // static methods
        /// <summary>
        /// Backpatches the size.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="startPosition">The start position.</param>
        public static void BackpatchSize(this BsonStream stream, long startPosition)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (startPosition < 0 || startPosition > stream.Length)
            {
                throw new ArgumentOutOfRangeException("startPosition");
            }

            var size = stream.Position - startPosition;

            if (size > int.MaxValue)
            {
                var message = string.Format("Size {0} is larger than {1} (Int32.MaxValue).", size, int.MaxValue);
                throw new FormatException(message);
            }

            var endPosition = stream.Position;

            stream.Position = startPosition;
            stream.WriteInt32((int)size);
            stream.Position = endPosition;
        }
        /// <summary>
        /// Writes bytes to the stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="buffer">The buffer.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="count">The count.</param>
        public static void WriteBytes(this BsonStream stream, byte[] buffer, int offset, int count)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (count < 0 || offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            if (count == 1)
            {
                stream.WriteByte(buffer[offset]);
            }
            else
            {
                stream.Write(buffer, offset, count);
            }
        }
        /// <summary>
        /// Writes a BsonType to the stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="value">The value.</param>
        public static void WriteBsonType(this BsonStream stream, BsonType value)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            stream.WriteByte((byte)value);
        }
        /// <summary>
        /// Writes a boolean to the stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="value">The value.</param>
        public static void WriteBoolean(this BsonStream stream, bool value)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            stream.WriteByte(value ? (byte)1 : (byte)0);
        }
        /// <summary>
        /// Reads a boolean from the stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>A boolean.</returns>
        public static bool ReadBoolean(this BsonStream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var b = stream.ReadByte();

            if (b == -1)
            {
                throw new EndOfStreamException();
            }
            return(b != 0);
        }
        /// <summary>
        /// Reads the binary sub type.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>The binary sub type.</returns>
        public static BsonBinarySubType ReadBinarySubType(this BsonStream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var b = stream.ReadByte();

            if (b == -1)
            {
                throw new EndOfStreamException();
            }
            return((BsonBinarySubType)b);
        }
        /// <summary>
        /// Reads bytes from the stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="count">The count.</param>
        /// <returns>The bytes.</returns>
        public static byte[] ReadBytes(this BsonStream stream, int count)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            var bytes = new byte[count];

            stream.ReadBytes(bytes, 0, count);
            return(bytes);
        }
Exemple #8
0
        // public methods
        /// <summary>
        /// Reads the name.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="encoding">The encoding.</param>
        /// <returns>
        /// The name.
        /// </returns>
        public string Decode(BsonStream stream, UTF8Encoding encoding)
        {
            BsonTrieNode <TValue> node;

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

            return(stream.ReadCString(encoding));
        }
Exemple #9
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");
            }

            _baseStream = stream;
            _bsonStream = (stream as BsonStream) ?? new BsonStreamAdapter(stream);

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
        // static methods
        /// <summary>
        /// Backpatches the size.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="startPosition">The start position.</param>
        public static void BackpatchSize(this BsonStream stream, long startPosition)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (startPosition < 0 || startPosition > stream.Length)
            {
                throw new ArgumentOutOfRangeException("startPosition");
            }

            var endPosition = stream.Position;
            var size        = (int)(endPosition - startPosition);

            stream.Position = startPosition;
            stream.WriteInt32(size);
            stream.Position = endPosition;
        }
        /// <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");
            }

            _baseStream = stream;
            _bsonStream = (stream as BsonStream) ?? new BsonStreamAdapter(stream);
            _settings = settings; // already frozen by base class

            _context = new BsonBinaryReaderContext(null, ContextType.TopLevel, 0, 0);
        }
        /// <summary>
        /// Reads bytes from the stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="buffer">The buffer.</param>
        /// <param name="offset">The offset.</param>
        /// <param name="count">The count.</param>
        public static void ReadBytes(this BsonStream stream, byte[] buffer, int offset, int count)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 || offset > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (count < 0 || offset + count > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("count");
            }

            if (count == 1)
            {
                var b = stream.ReadByte();
                if (b == -1)
                {
                    throw new EndOfStreamException();
                }
                buffer[offset] = (byte)b;
            }
            else
            {
                while (count > 0)
                {
                    var bytesRead = stream.Read(buffer, offset, count);
                    if (bytesRead == 0)
                    {
                        throw new EndOfStreamException();
                    }
                    offset += bytesRead;
                    count  -= bytesRead;
                }
            }
        }
        /// <summary>
        /// Reads the BSON type.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>The BSON type.</returns>
        public static BsonType ReadBsonType(this BsonStream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var b = stream.ReadByte();

            if (b == -1)
            {
                throw new EndOfStreamException();
            }
            if (!__validBsonTypes[b])
            {
                string message = string.Format("Invalid BsonType: {0}.", b);
                throw new FormatException(message);
            }
            return((BsonType)b);
        }
        /// <summary>
        /// Initializes a new instance of the BsonBinaryWriter class.
        /// </summary>
        /// <param name="stream">A stream. The BsonBinaryWriter does not own the stream and will not Dispose it.</param>
        /// <param name="settings">The BsonBinaryWriter settings.</param>
        public BsonBinaryWriter(Stream stream, BsonBinaryWriterSettings settings)
            : base(settings)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (!stream.CanSeek)
            {
                throw new ArgumentException("The stream must be capable of seeking.", "stream");
            }

            _baseStream = stream;
            _bsonStream = (stream as BsonStream) ?? new BsonStreamAdapter(stream);
            _settings = settings; // already frozen by base class
            _maxDocumentSizeStack.Push(_settings.MaxDocumentSize);

            _context = null;
            State = BsonWriterState.Initial;
        }
Exemple #15
0
        /// <summary>
        /// Reads the BSON type.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>The BSON type.</returns>
        public static BsonType ReadBsonType(this BsonStream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var b = stream.ReadByte();

            if (b == -1)
            {
                throw new EndOfStreamException();
            }
            if (!__validBsonTypes[b])
            {
                var message = string.Format("Detected unknown BSON type \"\\x{0:x2}\". Are you using the latest driver version?", b);
                throw new FormatException(message);
            }
            return((BsonType)b);
        }
        /// <summary>
        /// Writes a slice to the stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="slice">The slice.</param>
        public static void WriteSlice(this BsonStream stream, IByteBuffer slice)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (slice == null)
            {
                throw new ArgumentNullException("slice");
            }

            var position = 0;
            var count    = slice.Length;

            while (count > 0)
            {
                var segment      = slice.AccessBackingBytes(position);
                var partialCount = Math.Min(count, segment.Count);
                stream.WriteBytes(segment.Array, segment.Offset, partialCount);
                position += count;
                count    -= partialCount;
            }
        }
Exemple #17
0
        /// <summary>
        /// Reads a boolean from the stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <returns>A boolean.</returns>
        public static bool ReadBoolean(this BsonStream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            var b = stream.ReadByte();

            switch (b)
            {
            case -1:
                throw new EndOfStreamException();

            case 0:
                return(false);

            case 1:
                return(true);

            default:
                throw new FormatException($"Invalid BsonBoolean value: {b}.");
            }
        }
 // public methods
 /// <summary>
 /// Decodes the name.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="encoding">The encoding.</param>
 /// <returns>
 /// The name.
 /// </returns>
 public string Decode(BsonStream stream, UTF8Encoding encoding)
 {
     var utf8 = stream.ReadCStringBytes();
     return Utf8Helper.DecodeUtf8String(utf8.Array, utf8.Offset, utf8.Count, encoding);
 }