Example #1
0
        public BsonValue FetchCurrentValue()
        {
            CheckDisposed();
            if (_entryDataSkipped)
            {
                return(_entryDataValue);
            }
            _entryDataSkipped = true;
            switch (_ctype)
            {
            case BsonType.EOO:
            case BsonType.UNDEFINED:
            case BsonType.NULL:
            case BsonType.MAXKEY:
            case BsonType.MINKEY:
                _entryDataValue = new BsonValue(_ctype, _entryKey);
                break;

            case BsonType.OID:
                Debug.Assert(_entryLen == 12);
                _entryDataValue = new BsonValue(_ctype, _entryKey, new ObjectId(_input));
                break;

            case BsonType.STRING:
            case BsonType.CODE:
            case BsonType.SYMBOL:
            {
                Debug.Assert(_entryLen - 1 >= 0);
                string sv = Encoding.UTF8.GetString(_input.ReadBytes(_entryLen - 1));
                _entryDataValue = new BsonValue(_ctype, _entryKey, sv);
                var rb = _input.ReadByte();
                Debug.Assert(rb == 0x00);         //trailing zero byte
                break;
            }

            case BsonType.BOOL:
                _entryDataValue = new BsonValue(_ctype, _entryKey, _input.ReadBoolean());
                break;

            case BsonType.INT:
                _entryDataValue = new BsonValue(_ctype, _entryKey, _input.ReadInt32());
                break;

            case BsonType.OBJECT:
            case BsonType.ARRAY:
            {
                BsonDocument doc = (_ctype == BsonType.OBJECT ? new BsonDocument() : new BsonArray());
                BsonIterator sit = new BsonIterator(this);
                while (sit.Next() != BsonType.EOO)
                {
                    doc.Add(sit.FetchCurrentValue());
                }
                _entryDataValue = new BsonValue(_ctype, _entryKey, doc);
                break;
            }

            case BsonType.DOUBLE:
                _entryDataValue = new BsonValue(_ctype, _entryKey, _input.ReadDouble());
                break;

            case BsonType.LONG:
                _entryDataValue = new BsonValue(_ctype, _entryKey, _input.ReadInt64());
                break;

            case BsonType.DATE:
                _entryDataValue = new BsonValue(_ctype, _entryKey,
                                                BsonConstants.Epoch.AddMilliseconds(_input.ReadInt64()));
                break;

            case BsonType.TIMESTAMP:
            {
                int inc = _input.ReadInt32();
                int ts  = _input.ReadInt32();
                _entryDataValue = new BsonValue(_ctype, _entryKey,
                                                new BsonTimestamp(inc, ts));
                break;
            }

            case BsonType.REGEX:
            {
                string re   = _input.ReadCString();
                string opts = _input.ReadCString();
                _entryDataValue = new BsonValue(_ctype, _entryKey,
                                                new BsonRegexp(re, opts));
                break;
            }

            case BsonType.BINDATA:
            {
                byte        subtype = _input.ReadByte();
                BsonBinData bd      = new BsonBinData(subtype, _entryLen - 1, _input);
                _entryDataValue = new BsonValue(_ctype, _entryKey, bd);
                break;
            }

            case BsonType.DBREF:
            {
                //Unsupported DBREF!
                SkipData(true);
                _entryDataValue = new BsonValue(_ctype, _entryKey);
                break;
            }

            case BsonType.CODEWSCOPE:
            {
                int cwlen = _entryLen + 4;
                Debug.Assert(cwlen > 5);
                int            clen = _input.ReadInt32(); //code length
                string         code = Encoding.UTF8.GetString(_input.ReadBytes(clen));
                BsonCodeWScope cw   = new BsonCodeWScope(code);
                BsonIterator   sit  = new BsonIterator(_input, _input.ReadInt32());
                while (sit.Next() != BsonType.EOO)
                {
                    cw.Add(sit.FetchCurrentValue());
                }
                _entryDataValue = new BsonValue(_ctype, _entryKey, cw);
                break;
            }
            }
            return(_entryDataValue);
        }