/// <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; } } }