/// <summary>
        /// Reads a BSON string from the stream.
        /// </summary>
        /// <param name="encoding">The encoding.</param>
        /// <returns>
        /// A string.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">encoding</exception>
        /// <exception cref="System.FormatException"></exception>
        /// <exception cref="System.IO.EndOfStreamException"></exception>
        string IBsonStream.ReadBsonString(UTF8Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            if (!_isOpen)
            {
                StreamIsClosed();
            }

            var length = ((IBsonStream)this).ReadBsonInt32();

            if (length <= 0)
            {
                var message = string.Format("Invalid string length: {0}.", length);
                throw new FormatException(message);
            }
            if (_buffer[_position + length - 1] != 0)
            {
                throw new FormatException("String is missing terminating null byte.");
            }

            var position = _position;

            if ((_position += length) > _length)
            {
                _position = _length;
                throw new EndOfStreamException();
            }

            return(Utf8Helper.DecodeUtf8String(_buffer, position, length - 1, encoding));
        }
Example #2
0
 /// <summary>
 /// Reads a BSON CString from the stream.
 /// </summary>
 /// <returns>A string.</returns>
 /// <exception cref="System.ArgumentNullException">
 /// stream
 /// or
 /// encoding
 /// </exception>
 /// <exception cref="System.IO.EndOfStreamException"></exception>
 public string ReadCString()
 {
     if (_bsonStream != null)
     {
         return(_bsonStream.ReadBsonCString());
     }
     else
     {
         var memoryStream = new MemoryStream(32); // override default capacity of zero
         while (true)
         {
             var b = _stream.ReadByte();
             if (b == -1)
             {
                 throw new EndOfStreamException();
             }
             else if (b == 0)
             {
                 break;
             }
             else
             {
                 memoryStream.WriteByte((byte)b);
             }
         }
         return(Utf8Helper.DecodeUtf8String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length, Utf8Helper.StrictUtf8Encoding));
     }
 }
Example #3
0
        /// <inheritdoc/>
        public override string ReadCString(UTF8Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            ThrowIfDisposed();

            var bytes = ReadCStringBytes();

            return(Utf8Helper.DecodeUtf8String(bytes.Array, bytes.Offset, bytes.Count, encoding));
        }
Example #4
0
        // explicit interface implementations
        /// <summary>
        /// Reads a BSON CString from the stream.
        /// </summary>
        /// <returns>
        /// A string.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">encoding</exception>
        string IBsonStream.ReadBsonCString()
        {
            if (!_isOpen)
            {
                StreamIsClosed();
            }

            var nullPosition = FindNullByte();
            var position     = _position;
            var length       = nullPosition - position + 1; // read null byte also

            _position = nullPosition + 1;

            return(Utf8Helper.DecodeUtf8String(_buffer, position, length - 1, Utf8Helper.StrictUtf8Encoding)); // don't decode null byte
        }
Example #5
0
        /// <inheritdoc/>
        public override string ReadString(UTF8Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            ThrowIfDisposed();

            var length = ReadInt32();

            if (length <= 0)
            {
                var message = string.Format("Invalid string length: {0}.", length);
                throw new FormatException(message);
            }

            var segment = _buffer.AccessBackingBytes(_position);

            if (segment.Count >= length)
            {
                ThrowIfEndOfStream(length);
                if (segment.Array[segment.Offset + length - 1] != 0)
                {
                    throw new FormatException("String is missing terminating null byte.");
                }
                _position += length;
                return(Utf8Helper.DecodeUtf8String(segment.Array, segment.Offset, length - 1, encoding));
            }
            else
            {
                using var rentedBuffer = ThreadStaticBuffer.RentBuffer(length);
                var bytes = rentedBuffer.Bytes;
                this.ReadBytes(bytes, 0, length);
                if (bytes[length - 1] != 0)
                {
                    throw new FormatException("String is missing terminating null byte.");
                }

                return(Utf8Helper.DecodeUtf8String(bytes, 0, length - 1, encoding));
            }
        }
        // explicit interface implementations
        /// <summary>
        /// Reads a BSON CString from the stream.
        /// </summary>
        /// <returns>
        /// A string.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">encoding</exception>
        /// <exception cref="System.FormatException">CString is missing terminating null byte.</exception>
        string IBsonStream.ReadBsonCString()
        {
            ThrowIfDisposed();
            ThrowIfEndOfStream(1);

            var nullPosition = FindNullByte();
            var length       = nullPosition - _position + 1; // read null byte also

            var segment = _byteBuffer.AccessBackingBytes(_position);

            if (segment.Count >= length)
            {
                _position += length;
                return(Utf8Helper.DecodeUtf8String(segment.Array, segment.Offset, length - 1, Utf8Helper.StrictUtf8Encoding)); // don't decode null byte
            }
            else
            {
                var bytes = ReadBytes(length);
                return(Utf8Helper.DecodeUtf8String(bytes, 0, length - 1, Utf8Helper.StrictUtf8Encoding)); // don't decode null byte
            }
        }
        /// <summary>
        /// Reads a BSON string from the stream.
        /// </summary>
        /// <param name="encoding">The encoding.</param>
        /// <returns>
        /// A string.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">encoding</exception>
        /// <exception cref="System.FormatException">
        /// String is missing terminating null byte.
        /// or
        /// String is missing terminating null byte.
        /// </exception>
        string IBsonStream.ReadBsonString(UTF8Encoding encoding)
        {
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }

            var length = ((IBsonStream)this).ReadBsonInt32();

            if (length <= 0)
            {
                var message = string.Format("Invalid string length: {0}.", length);
                throw new FormatException(message);
            }

            var segment = _byteBuffer.AccessBackingBytes(_position);

            if (segment.Count >= length)
            {
                if (segment.Array[segment.Offset + length - 1] != 0)
                {
                    throw new FormatException("String is missing terminating null byte.");
                }
                _position += length;
                return(Utf8Helper.DecodeUtf8String(segment.Array, segment.Offset, length - 1, encoding));
            }
            else
            {
                var bytes = ReadBytes(length);
                if (bytes[length - 1] != 0)
                {
                    throw new FormatException("String is missing terminating null byte.");
                }
                return(Utf8Helper.DecodeUtf8String(bytes, 0, length - 1, encoding));
            }
        }
        /// <summary>
        /// Reads a BSON string from the stream.
        /// </summary>
        ///
        /// <returns>A string.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// stream
        /// or
        /// encoding
        /// </exception>
        /// <exception cref="System.FormatException">
        /// String is missing null terminator byte.
        /// </exception>
        public string ReadString()
        {
            if (_bsonStream != null)
            {
                return(_bsonStream.ReadBsonString(_encoding));
            }
            else
            {
                var length = ReadInt32();
                if (length < 1)
                {
                    var message = string.Format("Invalid string length: {0}.", length);
                    throw new FormatException(message);
                }

                var bytes = ReadBytes(length); // read the null byte also (included in length)
                if (bytes[length - 1] != 0)
                {
                    throw new FormatException("String is missing terminating null byte.");
                }

                return(Utf8Helper.DecodeUtf8String(bytes, 0, length - 1, _encoding)); // don't decode the null byte
            }
        }
Example #9
0
        // 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));
        }
        /// <summary>
        /// Reads a BSON CString from the stream.
        /// </summary>
        /// <returns>A string.</returns>
        public string ReadCString()
        {
            var utf8 = ReadCStringBytes();

            return(Utf8Helper.DecodeUtf8String(utf8.Array, utf8.Offset, utf8.Count, Utf8Encodings.Strict));
        }
Example #11
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));
        }