/// <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="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> /// 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> /// 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}."); } }