public int Read(BsonReader reader) { int size = reader.ReadInt32(); int bytesRead = 4; while(bytesRead + 1 < size){ BsonElement be = new BsonElement(); bytesRead += be.Read(reader); this.Add(be); } byte eoo = reader.ReadByte(); bytesRead++; if(eoo != (byte)0) throw new System.IO.InvalidDataException("Document not null terminated"); if(size != bytesRead) { throw new System.IO.InvalidDataException(string.Format("Should have read {0} bytes from stream but only read {1}]", size, bytesRead)); } return bytesRead; }