/// <exception cref="System.IO.IOException"></exception> private IClassDefinition CreateNestedClassDef(IPortable portable, ClassDefinitionBuilder nestedBuilder) { var writer = new ClassDefinitionWriter(context, nestedBuilder); portable.WritePortable(writer); return(context.RegisterClassDefinition(nestedBuilder.Build())); }
public virtual void SetUp() { var builder = new ClassDefinitionBuilder(1, 2, 3); foreach (var fieldName in fieldNames) { builder.AddByteField(fieldName); } classDefinition = (ClassDefinition) builder.Build(); }
internal static IClassDefinition CreateInnerPortableClassDefinition() { var builder = new ClassDefinitionBuilder(FactoryId, TestSerializationConstants.INNER_PORTABLE); builder.AddByteArrayField("b"); builder.AddCharArrayField("c"); builder.AddShortArrayField("s"); builder.AddIntArrayField("i"); builder.AddLongArrayField("l"); builder.AddFloatArrayField("f"); builder.AddDoubleArrayField("d"); builder.AddPortableArrayField("nn", PortableSerializationTest.CreateNamedPortableClassDefinition()); return builder.Build(); }
/// <exception cref="System.IO.IOException" /> internal IClassDefinition ReadClassDefinition(IBufferObjectDataInput @in, int factoryId, int classId, int version) { var register = true; var builder = new ClassDefinitionBuilder(factoryId, classId, version); // final position after portable is read @in.ReadInt(); // field count var fieldCount = @in.ReadInt(); var offset = @in.Position(); for (var i = 0; i < fieldCount; i++) { var pos = @in.ReadInt(offset + i * Bits.IntSizeInBytes); @in.Position(pos); var len = @in.ReadShort(); var chars = new char[len]; for (var k = 0; k < len; k++) { chars[k] = (char)@in.ReadUnsignedByte(); } var type = (FieldType)(@in.ReadByte()); var name = new string(chars); var fieldFactoryId = 0; var fieldClassId = 0; if (type == FieldType.Portable) { // is null if (@in.ReadBoolean()) { register = false; } fieldFactoryId = @in.ReadInt(); fieldClassId = @in.ReadInt(); if (register) { var fieldVersion = @in.ReadInt(); ReadClassDefinition(@in, fieldFactoryId, fieldClassId, fieldVersion); } } else { if (type == FieldType.PortableArray) { var k_1 = @in.ReadInt(); fieldFactoryId = @in.ReadInt(); fieldClassId = @in.ReadInt(); if (k_1 > 0) { var p = @in.ReadInt(); @in.Position(p); var fieldVersion = @in.ReadInt(); ReadClassDefinition(@in, fieldFactoryId, fieldClassId, fieldVersion); } else { register = false; } } } builder.AddField(new FieldDefinition(i, name, type, fieldFactoryId, fieldClassId)); } var classDefinition = builder.Build(); if (register) { classDefinition = RegisterClassDefinition(classDefinition); } return(classDefinition); }
internal static IClassDefinition CreateNamedPortableClassDefinition() { var builder = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, TestSerializationConstants.NAMED_PORTABLE); builder.AddUTFField("name"); builder.AddIntField("myint"); return builder.Build(); }
public void TestWriteReadWithNullPortableArray() { var builder0 = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, 1); var builder1 = new ClassDefinitionBuilder(TestSerializationConstants.PORTABLE_FACTORY_ID, 2); builder0.AddPortableArrayField("list", builder1.Build()); var ss = new SerializationServiceBuilder() .AddClassDefinition(builder0.Build()) .AddClassDefinition(builder1.Build()) .Build(); var data = ss.ToData(new TestObject1()); var ss2 = new SerializationServiceBuilder() .AddPortableFactory(1, new PortableFactoryFunc(classId => { switch (classId) { case 1: return new TestObject1(); case 2: return new TestObject2(); } return null; })).Build(); var obj = ss2.ToObject<object>(data); Assert.IsNotNull(obj); Assert.IsInstanceOf<TestObject1>(obj); }
public void TestRawDataInvaliRead() { var serializationService = CreateSerializationService(1, ByteOrder.BigEndian); var p = new InvalidRawDataPortable2(DateTime.Now.ToFileTime(), "test chars".ToCharArray(), new NamedPortable("named portable", 34567), 9876, "Testing raw portable", new ByteArrayDataSerializable(Encoding.UTF8.GetBytes("test bytes"))); var builder = new ClassDefinitionBuilder(p.GetFactoryId(), p.GetClassId()); builder.AddLongField("l").AddCharArrayField("c").AddPortableField("p", CreateNamedPortableClassDefinition()); serializationService.GetPortableContext().RegisterClassDefinition(builder.Build()); var data = serializationService.ToData(p); Assert.AreEqual(p, serializationService.ToObject<RawDataPortable>(data)); }
internal IClassDefinition RegisterAndGet() { IClassDefinition cd = builder.Build(); return(context.RegisterClassDefinition(cd)); }
/// <exception cref="System.IO.IOException" /> internal IClassDefinition ReadClassDefinition(IBufferObjectDataInput @in, int factoryId, int classId, int version) { var register = true; var builder = new ClassDefinitionBuilder(factoryId, classId, version); // final position after portable is read @in.ReadInt(); // field count var fieldCount = @in.ReadInt(); var offset = @in.Position(); for (var i = 0; i < fieldCount; i++) { var pos = @in.ReadInt(offset + i*Bits.IntSizeInBytes); @in.Position(pos); var len = @in.ReadShort(); var chars = new char[len]; for (var k = 0; k < len; k++) { chars[k] = (char) @in.ReadUnsignedByte(); } var type = (FieldType) (@in.ReadByte()); var name = new string(chars); var fieldFactoryId = 0; var fieldClassId = 0; if (type == FieldType.Portable) { // is null if (@in.ReadBoolean()) { register = false; } fieldFactoryId = @in.ReadInt(); fieldClassId = @in.ReadInt(); if (register) { var fieldVersion = @in.ReadInt(); ReadClassDefinition(@in, fieldFactoryId, fieldClassId, fieldVersion); } } else { if (type == FieldType.PortableArray) { var k_1 = @in.ReadInt(); fieldFactoryId = @in.ReadInt(); fieldClassId = @in.ReadInt(); if (k_1 > 0) { var p = @in.ReadInt(); @in.Position(p); var fieldVersion = @in.ReadInt(); ReadClassDefinition(@in, fieldFactoryId, fieldClassId, fieldVersion); } else { register = false; } } } builder.AddField(new FieldDefinition(i, name, type, fieldFactoryId, fieldClassId)); } var classDefinition = builder.Build(); if (register) { classDefinition = RegisterClassDefinition(classDefinition); } return classDefinition; }
/// <exception cref="System.IO.IOException"></exception> private IClassDefinition CreateNestedClassDef(IPortable portable, ClassDefinitionBuilder nestedBuilder) { var writer = new ClassDefinitionWriter(_context, nestedBuilder); portable.WritePortable(writer); return _context.RegisterClassDefinition(nestedBuilder.Build()); }