/// <summary> /// Reads BSON binary data from the reader. /// </summary> /// <returns>A BsonBinaryData.</returns> #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary public override BsonBinaryData ReadBinaryData() { if (Disposed) { ThrowObjectDisposedException(); } VerifyBsonType("ReadBinaryData", BsonType.Binary); int size = ReadSize(); var subType = _bsonStream.ReadBinarySubType(); if (subType == BsonBinarySubType.OldBinary) { // sub type OldBinary has two sizes (for historical reasons) int size2 = ReadSize(); if (size2 != size - 4) { throw new FormatException("Binary sub type OldBinary has inconsistent sizes"); } size = size2; if (Settings.FixOldBinarySubTypeOnInput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } } var bytes = _bsonStream.ReadBytes(size); BsonBinaryData binaryData; if (BsonDefaults.GuidRepresentationMode == GuidRepresentationMode.V2) { GuidRepresentation guidRepresentation; switch (subType) { case BsonBinarySubType.UuidLegacy: guidRepresentation = Settings.GuidRepresentation; break; case BsonBinarySubType.UuidStandard: guidRepresentation = GuidRepresentation.Standard; break; default: guidRepresentation = GuidRepresentation.Unspecified; break; } binaryData = new BsonBinaryData(bytes, subType, guidRepresentation); } else { binaryData = new BsonBinaryData(bytes, subType); } State = GetNextState(); return(binaryData); }
/// <summary> /// Reads BSON binary data from the reader. /// </summary> /// <returns>A BsonBinaryData.</returns> #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary public override BsonBinaryData ReadBinaryData() { if (Disposed) { ThrowObjectDisposedException(); } VerifyBsonType("ReadBinaryData", BsonType.Binary); int size = ReadSize(); var subType = _bsonStream.ReadBinarySubType(); if (subType == BsonBinarySubType.OldBinary) { // sub type OldBinary has two sizes (for historical reasons) int size2 = ReadSize(); if (size2 != size - 4) { throw new FormatException("Binary sub type OldBinary has inconsistent sizes"); } size = size2; if (_settings.FixOldBinarySubTypeOnInput) { subType = BsonBinarySubType.Binary; // replace obsolete OldBinary with new Binary sub type } } var bytes = _bsonStream.ReadBytes(size); var guidRepresentation = GuidRepresentation.Unspecified; if (subType == BsonBinarySubType.UuidLegacy || subType == BsonBinarySubType.UuidStandard) { if (_settings.GuidRepresentation != GuidRepresentation.Unspecified) { var expectedSubType = (_settings.GuidRepresentation == GuidRepresentation.Standard) ? BsonBinarySubType.UuidStandard : BsonBinarySubType.UuidLegacy; if (subType != expectedSubType) { var message = string.Format( "The GuidRepresentation for the reader is {0}, which requires the binary sub type to be {1}, not {2}.", _settings.GuidRepresentation, expectedSubType, subType); throw new FormatException(message); } } guidRepresentation = (subType == BsonBinarySubType.UuidStandard) ? GuidRepresentation.Standard : _settings.GuidRepresentation; } State = GetNextState(); return(new BsonBinaryData(bytes, subType, guidRepresentation)); }
static int _m_ReadBytes(RealStatePtr L) { try { ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L); MongoDB.Bson.IO.BsonStream gen_to_be_invoked = (MongoDB.Bson.IO.BsonStream)translator.FastGetCSObj(L, 1); int gen_param_count = LuaAPI.lua_gettop(L); if (gen_param_count == 2 && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 2)) { int _count = LuaAPI.xlua_tointeger(L, 2); byte[] gen_ret = gen_to_be_invoked.ReadBytes(_count); LuaAPI.lua_pushstring(L, gen_ret); return(1); } if (gen_param_count == 4 && (LuaAPI.lua_isnil(L, 2) || LuaAPI.lua_type(L, 2) == LuaTypes.LUA_TSTRING) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 3) && LuaTypes.LUA_TNUMBER == LuaAPI.lua_type(L, 4)) { byte[] _buffer = LuaAPI.lua_tobytes(L, 2); int _offset = LuaAPI.xlua_tointeger(L, 3); int _count = LuaAPI.xlua_tointeger(L, 4); gen_to_be_invoked.ReadBytes(_buffer, _offset, _count); return(0); } } catch (System.Exception gen_e) { return(LuaAPI.luaL_error(L, "c# exception:" + gen_e)); } return(LuaAPI.luaL_error(L, "invalid arguments to MongoDB.Bson.IO.BsonStream.ReadBytes!")); }
/// <summary> /// Reads bytes from the stream. /// </summary> /// <param name="stream">The stream.</param> /// <param name="count">The count.</param> /// <returns>The bytes.</returns> public static byte[] ReadBytes(this BsonStream stream, int count) { if (stream == null) { throw new ArgumentNullException("stream"); } if (count < 0) { throw new ArgumentOutOfRangeException("count"); } var bytes = new byte[count]; stream.ReadBytes(bytes, 0, count); return(bytes); }