예제 #1
0
        /// <summary>
        /// Writes a BSON CString to the stream.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <exception cref="System.ArgumentNullException">
        /// stream
        /// or
        /// encoding
        /// </exception>
        /// <exception cref="System.ArgumentException">UTF8 representation cannot contain null bytes when writing a BSON CString.;value</exception>
        public void WriteCString(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (_bsonStream != null)
            {
                _bsonStream.WriteBsonCString(value);
            }
            else
            {
                var bytes = Utf8Encodings.Strict.GetBytes(value);
                WriteCString(bytes);
            }
        }
예제 #2
0
        /// <summary>
        /// Writes a BSON CString to the stream.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <exception cref="System.ArgumentNullException">
        /// stream
        /// or
        /// encoding
        /// </exception>
        /// <exception cref="System.ArgumentException">UTF8 representation cannot contain null bytes when writing a BSON CString.;value</exception>
        public void WriteCString(string value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (_bsonStream != null)
            {
                _bsonStream.WriteBsonCString(value);
            }
            else
            {
                var bytes = Utf8Helper.StrictUtf8Encoding.GetBytes(value);
                if (bytes.Contains <byte>(0))
                {
                    throw new ArgumentException("UTF8 representation cannot contain null bytes when writing a BSON CString.", "value");
                }
                _stream.Write(bytes, 0, bytes.Length);
                _stream.WriteByte(0);
            }
        }