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);
        }
Пример #2
0
        public void TestRoundTrip()
        {
            Document idoc = new Document();
            idoc.Add("b",new Binary(new byte[]{(byte)1,(byte)2}));
            BsonDocument bidoc = BsonConvert.From(idoc);

            MemoryStream stream = new MemoryStream();
            BsonWriter writer = new BsonWriter(stream);
            bidoc.Write(writer);

            stream.Seek(0,SeekOrigin.Begin);
            BsonReader reader = new BsonReader(stream);
            BsonDocument bodoc = new BsonDocument();
            bodoc.Read(reader);
            Document odoc = (Document) bodoc.ToNative();

            Assert.AreEqual(idoc.ToString(), odoc.ToString());
        }
Пример #3
0
        public void TestBinaryRead()
        {
            string hex = "28000000075f6964004b1971811d8b0f00c0000000056461746100070000000203000000e188b400";

            byte[] data = DecodeHex (hex);
            MemoryStream inmem = new MemoryStream (data);
            BsonReader inreader = new BsonReader (inmem);
            BsonDocument indoc = new BsonDocument ();
            indoc.Read (inreader);

            MemoryStream outmem = new MemoryStream ();
            BsonWriter outwriter = new BsonWriter (outmem);
            indoc.Write (outwriter);
            byte[] outdata = outmem.ToArray ();
            String outhex = BitConverter.ToString (outdata);
            outhex = outhex.Replace ("-", "");

            Assert.AreEqual (hex, outhex.ToLower());
        }
        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);
        }
Пример #5
0
        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();
        }