public static BsonDocument FromStream(Stream stream)
{
//read the first byte to determine the type
BinaryReader reader = new BinaryReader(stream);
//get the length of this document and the amount
//that should be read into the parsing stream.
//removes 4 bytes to account for the length value
//and an additional 1 byte to account for the
//terminator value
int length = reader.ReadInt32();
int read = length - (DOCUMENT_LENGTH_COUNT + DOCUMENT_TERMINATOR_COUNT);
//read out the bytes to use and the terminator
byte[] bytes = reader.ReadBytes(read);
reader.ReadByte();
//use the bytes to generate the document
using (MemoryStream content = new MemoryStream(bytes)) {
//read the content
Dictionary<string, object> values = BsonTranslator.FromStream(content);
//fill and return the object
BsonDocument document = new BsonDocument();
document.Merge(values);
return document;
}
}