Exemple #1
0
 public int Read(BsonReader reader)
 {
     int size = reader.ReadInt32();
     this.Subtype = reader.ReadByte();
     this.Val = reader.ReadBytes(size);
     return this.Size;
 }
        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 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 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;
        }
Exemple #5
0
 public int Read(BsonReader reader)
 {
     this.val = reader.ReadInt32();
     return this.Size;
 }
Exemple #6
0
 public int Read(BsonReader reader)
 {
     int len = reader.ReadInt32();
     this.Val = reader.ReadString(len);
     return 4 + len;
 }
 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;
 }
        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;
        }