public void Read(Stream stream) { /* Used during debugging of the stream. BsonReader headerreader = new BsonReader(stream); this.Header = ReadHeader(headerreader); //buffer the whole response into a memorystream for debugging. MemoryStream buffer = new MemoryStream(); BinaryReader buffReader = new BinaryReader(stream); BinaryWriter buffWriter = new BinaryWriter(buffer); byte[] body = buffReader.ReadBytes(this.Header.MessageLength - 16); System.Console.WriteLine(BitConverter.ToString(body)); buffWriter.Write(body); buffer.Seek(0, SeekOrigin.Begin); BsonReader reader = new BsonReader(buffer);*/ BsonReader reader = new BsonReader(stream); this.Header = ReadHeader(reader); this.ResponseFlag = reader.ReadInt32(); this.CursorID = reader.ReadInt64(); this.StartingFrom = reader.ReadInt32(); this.NumberReturned = reader.ReadInt32(); List<BsonDocument> docs = new List<BsonDocument>(); for(int num = 0; num < this.NumberReturned; num++){ BsonDocument doc = new BsonDocument(); doc.Read(reader); docs.Add(doc); } this.Documents = docs.ToArray(); }
public void TestReadBigDocument() { MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); Document expected = new Document(); expected.Append("str", "test") .Append("int", 45) .Append("long", (long)46) .Append("num", 4.5) .Append("date",DateTime.Today) .Append("_id", new OidGenerator().Generate()) .Append("code", new Code("return 1;")) .Append("subdoc", new Document().Append("a",1).Append("b",2)) .Append("array", new String[]{"a","b","c","d"}) .Append("codewscope", new CodeWScope("return 2;", new Document().Append("c",1))) .Append("binary", new Binary(new byte[]{0,1,2,3})) .Append("regex", new MongoRegex("[A-Z]")) .Append("minkey", MongoMinKey.Value) .Append("maxkey", MongoMaxKey.Value) ; writer.Write(expected); writer.Flush(); ms.Seek(0,SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); Document doc = reader.Read(); Assert.IsNotNull(doc); }
public void TestArrayElements() { String hexdoc = "82000000075f6964004a78937917220000000061cf0461005d0000" + "00013000000000000000f03f013100000000000000004001320000" + "000000000008400133000000000000001040013400000000000000" + "145001350000000000000018400136000000000000001c40013700" + "00000000000020400002620005000000746573740000"; byte[] bytes = HexToBytes(hexdoc); MemoryStream buf = new MemoryStream(bytes); BsonReader reader = new BsonReader(buf); BsonDocument bdoc = new BsonDocument(); bdoc.Read(reader); Assert.AreEqual(BsonDataType.Array, (BsonDataType)bdoc["a"].Val.TypeNum); buf = new MemoryStream(); BsonWriter writer = new BsonWriter(buf); bdoc.Write(writer); String hexdump = BitConverter.ToString(buf.ToArray()); hexdump = hexdump.Replace("-","").ToLower(); Assert.AreEqual(hexdoc, hexdump); }
public int Read(BsonReader reader) { int size = reader.ReadInt32(); this.Subtype = reader.ReadByte(); this.Val = reader.ReadBytes(size); return this.Size; }
public void TestReadLongerString() { byte[] buf = HexToBytes("7465737474657374746573747465737474657374746573747465737474657374746573747465737400"); MemoryStream ms = new MemoryStream(buf); BsonReader reader = new BsonReader(ms); String s = reader.ReadString(); Assert.AreEqual("testtesttesttesttesttesttesttesttesttest",s); }
public void TestReadEmptyDocument() { byte[] buf = HexToBytes("0500000000"); MemoryStream ms = new MemoryStream(buf); BsonReader reader = new BsonReader(ms); Document doc = reader.ReadDocument(); Assert.IsNotNull(doc); }
public int Read(BsonReader reader) { sbyte typeNum = (sbyte)reader.ReadByte(); int bytesRead; this.Name = reader.ReadString(); this.Val = BsonConvert.Create((BsonDataType)typeNum); bytesRead = this.Val.Read(reader); bytesRead += (1 + this.Name.Length + 1); //type byte & name + term return bytesRead; }
public int Read(BsonReader reader) { int len = 0; this.Expression = reader.ReadString(); len += this.Expression.Length + 1; this.Options = reader.ReadString(); len += this.Options.Length + 1; return len; }
public void TestReadDocWithDocs() { // Document doc = new Document().Append("a", new Document().Append("b", new Document().Append("c",new Document()))); // Console.WriteLine(ConvertDocToHex(doc)); byte[] buf = HexToBytes("1D000000036100150000000362000D0000000363000500000000000000"); MemoryStream ms = new MemoryStream(buf); BsonReader reader = new BsonReader(ms); Document doc = reader.ReadDocument(); Assert.IsNotNull(doc, "Document was null"); Assert.AreEqual(buf.Length, reader.Position); Assert.IsTrue(doc.Contains("a")); }
public void Read(Stream stream) { stream = new BufferedStream(stream, 256); BinaryReader reader = new BinaryReader(stream); this.Header = ReadHeader(reader); this.ResponseFlag = reader.ReadInt32(); this.CursorID = reader.ReadInt64(); this.StartingFrom = reader.ReadInt32(); this.NumberReturned = reader.ReadInt32(); BsonReader breader = new BsonReader(documentFactory, stream); List<Document> docs = new List<Document>(); for(int num = 0; num < this.NumberReturned; num++){ docs.Add(breader.Read()); } this.Documents = docs.ToArray(); }
public void TestBinary() { byte[] data = File.ReadAllBytes(@"test-data\tests.binary.txt"); BsonBinary binaryIn = new BsonBinary(new Binary(data)); MemoryStream stream = new MemoryStream(); BsonWriter bsonWriter = new BsonWriter(stream); binaryIn.Write(bsonWriter); stream.Position = 0; BsonReader reader = new BsonReader(stream); BsonBinary binaryOut = new BsonBinary(); int size = reader.ReadInt32(); binaryOut.Subtype = reader.ReadByte(); binaryOut.Val = reader.ReadBytes(size); Assert.AreEqual(binaryIn.Val, binaryOut.Val); Assert.AreEqual(binaryIn.Subtype, binaryOut.Subtype); Assert.AreEqual(data.Length, binaryOut.Size); }
public void TestDateUTC() { DateTime now = DateTime.UtcNow; MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); Document source = new Document(); source.Append("d",now); writer.Write(source); writer.Flush(); ms.Seek(0,SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); Document copy = reader.Read(); DateTime then = (DateTime)copy["d"]; Assert.AreEqual(now.Hour,then.Hour, "Date did not round trip right."); }
public void TestArraysWithHoles() { String hexdoc = "46000000075F6964004A79BFD517220000000061D304617272617900" + "29000000023000020000006100023100020000006200023200020000" + "0063000234000200000065000000"; byte[] bytes = HexToBytes(hexdoc); MemoryStream buf = new MemoryStream(bytes); BsonReader reader = new BsonReader(buf); BsonDocument bdoc = new BsonDocument(); bdoc.Read(reader); Assert.AreEqual(BsonDataType.Array, (BsonDataType)bdoc["array"].Val.TypeNum); buf = new MemoryStream(); BsonWriter writer = new BsonWriter(buf); bdoc.Write(writer); String hexdump = BitConverter.ToString(buf.ToArray()); hexdump = hexdump.Replace("-",""); Assert.AreEqual(hexdoc, hexdump); }
public void TestDBRef() { MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); Document source = new Document(); source.Append("x",1).Append("ref",new DBRef("refs","ref1")); writer.Write(source); writer.Flush(); ms.Seek(0,SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); Document copy = reader.Read(); Assert.IsTrue(copy.Contains("ref")); Assert.IsTrue(copy["ref"].GetType() == typeof(DBRef)); DBRef sref = (DBRef)source["ref"]; DBRef cref = (DBRef)copy["ref"]; Assert.AreEqual(sref.Id, cref.Id); }
public int Read(BsonReader reader) { this.Val = reader.ReadInt64(); return this.Size; }
public int Read(BsonReader reader) { this.val = reader.ReadInt32(); return this.Size; }
public int Read(BsonReader reader) { this.val = reader.ReadDouble(); return this.Size; }
public int Read(BsonReader reader) { return 0; }
public void TestReadStringValue() { byte[] buf = HexToBytes("050000007465737400"); MemoryStream ms = new MemoryStream(buf); BsonReader reader = new BsonReader(ms); String str = reader.ReadLenString(); Assert.AreEqual(buf.Length, reader.Position); Assert.AreEqual("test", (String)str); }
public void TestReadSimpleDocument() { byte[] buf = HexToBytes("1400000002746573740005000000746573740000"); MemoryStream ms = new MemoryStream(buf); BsonReader reader = new BsonReader(ms); Document doc = reader.Read(); Assert.IsNotNull(doc, "Document was null"); Assert.IsTrue(doc.Contains("test")); Assert.AreEqual("test", (String)doc["test"]); }
protected MessageHeader ReadHeader(BsonReader reader) { MessageHeader hdr = new MessageHeader(OpCode.Reply); hdr.MessageLength = reader.ReadInt32(); hdr.RequestId = reader.ReadInt32(); hdr.ResponseTo = reader.ReadInt32(); int op = reader.ReadInt32(); if((OpCode)op != OpCode.Reply) throw new InvalidDataException("Should have been a reply but wasn't"); return hdr; }
private string WriteAndReadString(string val) { byte[] buf = Encoding.UTF8.GetBytes(val + '\0'); MemoryStream ms = new MemoryStream(buf); BsonReader reader = new BsonReader(ms); return reader.ReadString(); }
public void TestReadString() { byte[] buf = HexToBytes("7465737400"); MemoryStream ms = new MemoryStream(buf); BsonReader reader = new BsonReader(ms); String s = reader.ReadString(); Assert.AreEqual("test",s); Assert.AreEqual(4,Encoding.UTF8.GetByteCount(s)); }
public void TestReadStringElement() { byte[] buf = HexToBytes("027465737400050000007465737400"); MemoryStream ms = new MemoryStream(buf); BsonReader reader = new BsonReader(ms); Document doc = new Document(); reader.ReadElement(doc); //Assert.AreEqual(buf.Length,read); Assert.IsTrue(doc.Contains("test")); Assert.AreEqual("test",(String)doc["test"]); Assert.AreEqual(buf.Length,reader.Position); }
// [Test] // public void TestBsonInteger(){ // InitStreams(); // BsonInteger w = new BsonInteger(5); // w.Write(writer); // // FlushAndGotoBegin(); // // BsonInteger r = new BsonInteger(); // r.Read(reader); // // Assert.AreEqual(w.Val, r.Val); // } protected void InitStreams() { mem = new MemoryStream(); reader = new BsonReader(mem); writer = new BsonWriter(mem); }
public int Read(BsonReader reader) { int len = reader.ReadInt32(); this.Val = reader.ReadString(len); return 4 + len; }
public void TestReadMultiElementDocument() { byte[] buf = HexToBytes("2D000000075F6964004A753AD8FAC16EA58B290351016100000000000000F03F02620005000000746573740000"); MemoryStream ms = new MemoryStream(buf); BsonReader reader = new BsonReader(ms); Document doc = reader.ReadDocument(); Assert.IsNotNull(doc, "Document was null"); Assert.IsTrue(doc.Contains("_id")); Assert.IsTrue(doc.Contains("a")); Assert.IsTrue(doc.Contains("b")); Assert.AreEqual("\"4a753ad8fac16ea58b290351\"", ((Oid)doc["_id"]).ToString()); Assert.AreEqual(1, Convert.ToInt32(doc["a"])); Assert.AreEqual("test", (String)doc["b"]); }
public void TestReadStringWithUKPound() { byte[] buf = HexToBytes("31323334C2A3353600"); MemoryStream ms = new MemoryStream(buf); BsonReader reader = new BsonReader(ms); String s = reader.ReadString(); Assert.AreEqual("1234£56",s); Assert.AreEqual(8,Encoding.UTF8.GetByteCount(s)); Assert.AreEqual(9,reader.Position); }
protected Document WriteAndRead(Document source) { MemoryStream ms = new MemoryStream(); BsonWriter writer = new BsonWriter(ms); writer.Write(source); writer.Flush(); ms.Seek(0, SeekOrigin.Begin); BsonReader reader = new BsonReader(ms); return reader.Read(); }
public int Read(BsonReader reader) { int size = reader.ReadInt32(); int bytesRead = 4; int codeLen = reader.ReadInt32(); this.Val = reader.ReadString(codeLen); bytesRead += 4 + codeLen; this.Scope = new BsonDocument(); bytesRead += this.Scope.Read(reader); if(bytesRead != size){ throw new System.IO.InvalidDataException(string.Format("Should have read {0} bytes from stream but only read {1}]", size, bytesRead)); } return bytesRead; }