Example #1
0
        private void encodeDocument(MemoryStream ms, BSONObject obj)
        {
            MemoryStream dms = new MemoryStream();

            foreach (string str in obj.Keys)
            {
                this.encodeElement(dms, str, obj[str]);
            }

            BinaryWriter bw = new BinaryWriter(ms);

            bw.Write((int)(dms.Position + 4 + 1));

            byte[] buffer;
#if NETSTANDARD1_6
            ArraySegment <byte> tempBuffer;
            if (!dms.TryGetBuffer(out tempBuffer))
            {
                throw new NAMEException("This should not happen!");
            }
            buffer = tempBuffer.Array;
#else
            try
            {
                buffer = dms.GetBuffer();
            }
            catch (Exception ex)
            {
                throw new NAMEException("This should not happen!", ex);
            }
#endif
            bw.Write(buffer, 0, (int)dms.Position);
            bw.Write((byte)0);
        }
Example #2
0
        private void encodeArray(MemoryStream ms, BSONArray lst)
        {
            var obj = new BSONObject();

            for (int i = 0; i < lst.Count; ++i)
            {
                obj.Add(Convert.ToString(i), lst[i]);
            }

            this.encodeDocument(ms, obj);
        }
Example #3
0
        public static byte[] Dump(BSONObject obj)
        {
            SimpleBSON   bson = new SimpleBSON();
            MemoryStream ms   = new MemoryStream();

            bson.encodeDocument(ms, obj);

            byte[] buf = new byte[ms.Position];
            ms.Seek(0, SeekOrigin.Begin);
            ms.Read(buf, 0, buf.Length);

            return(buf);
        }
Example #4
0
        private BSONArray decodeArray()
        {
            BSONObject obj = this.decodeDocument();

            int       i     = 0;
            BSONArray array = new BSONArray();

            while (obj.ContainsKey(Convert.ToString(i)))
            {
                array.Add(obj[Convert.ToString(i)]);

                i += 1;
            }

            return(array);
        }
Example #5
0
        private BSONObject decodeDocument()
        {
            int length = this.mBinaryReader.ReadInt32() - 4;

            BSONObject obj = new BSONObject();

            int i = (int)this.mBinaryReader.BaseStream.Position;

            while (this.mBinaryReader.BaseStream.Position < i + length - 1)
            {
                BSONValue value = this.decodeElement(out string name);
                obj.Add(name, value);
            }

            this.mBinaryReader.ReadByte(); // zero
            return(obj);
        }