/** {@inheritDoc} */ @Override protected void marshal0(@Nullable Object obj, OutputStream out) throws IgniteCheckedException { byte[] arr = marshal(obj); try { out.write(arr); } catch (Exception e) { throw new BinaryObjectException("Failed to marshal the object: " + obj, e); } }
/** * @param obj Original object. * @param marsh Marshaller. * @return Result object. */ private <T> T marshalUnmarshal(Object obj, BinaryMarshaller marsh) throws IgniteCheckedException { byte[] bytes = marsh.marshal(obj); return marsh.unmarshal(bytes, null); }
/** {@inheritDoc} */ @Override protected BinaryObjectExImpl toBinary(BinaryMarshaller marsh, Object obj) throws Exception { byte[] bytes = marsh.marshal(obj); return new BinaryObjectImpl(ctx, bytes, 0); } }
@Override public void applyx(TestObject obj) throws IgniteCheckedException { tuple.set(marsh.marshal(obj)); } };
/** {@inheritDoc} */ @Override protected BinaryObjectExImpl toBinary(BinaryMarshaller marsh, Object obj) throws Exception { byte[] bytes = marsh.marshal(obj); return new BinaryObjectImpl(binaryContext(marsh), bytes, 0); } }
/** * @param obj Object to transform to a binary object. * @param marsh Binary marshaller. * @return Binary object. */ protected BinaryObjectImpl toBinary(Object obj, BinaryMarshaller marsh) throws Exception { byte[] bytes = marsh.marshal(obj); return new BinaryObjectImpl(binaryContext(marsh), bytes, 0); }
/** * @param obj Object. * @param marsh Marshaller. * @return Binary object. */ private <T> BinaryObjectImpl marshal(T obj, BinaryMarshaller marsh) throws IgniteCheckedException { byte[] bytes = marsh.marshal(obj); return new BinaryObjectImpl(U.<GridBinaryMarshaller>field(marsh, "impl").context(), bytes, 0); }
/** * @throws Exception If failed. */ @Test public void testUnregisteredClass() throws Exception { BinaryMarshaller m = binaryMarshaller(null, Collections.singletonList(Value.class.getName())); ClassFieldObject res = m.unmarshal(m.marshal(new ClassFieldObject(Value.class)), null); assertEquals(Value.class, res.cls); }
/** {@inheritDoc} */ @Override protected BinaryObjectExImpl toBinary(BinaryMarshaller marsh, Object obj) throws Exception { byte[] arr = marsh.marshal(obj); long ptr = GridUnsafe.allocateMemory(arr.length); ptrs.add(ptr); GridUnsafe.copyHeapOffheap(arr, GridUnsafe.BYTE_ARR_OFF, ptr, arr.length); return new BinaryObjectOffheapImpl(ctx, ptr, 0, arr.length); } }
/** * @throws Exception If failed. */ @Test public void testMarshallingThroughJdk() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); InetSocketAddress addr = new InetSocketAddress("192.168.0.2", 4545); byte[] arr = marsh.marshal(addr); InetSocketAddress addr2 = marsh.unmarshal(arr, null); assertEquals(addr.getHostString(), addr2.getHostString()); assertEquals(addr.getPort(), addr2.getPort()); TestAddress testAddr = new TestAddress(); testAddr.addr = addr; testAddr.str1 = "Hello World"; SimpleObject simpleObj = new SimpleObject(); simpleObj.c = 'g'; simpleObj.date = new Date(); testAddr.obj = simpleObj; arr = marsh.marshal(testAddr); TestAddress testAddr2 = marsh.unmarshal(arr, null); assertEquals(testAddr.addr.getHostString(), testAddr2.addr.getHostString()); assertEquals(testAddr.addr.getPort(), testAddr2.addr.getPort()); assertEquals(testAddr.str1, testAddr2.str1); assertEquals(testAddr.obj.c, testAddr2.obj.c); assertEquals(testAddr.obj.date, testAddr2.obj.date); }
/** * Test verbose logging of object marshalling. * * @throws Exception If failed. */ @Test public void testFailedMarshallingLogging() throws Exception { BinaryMarshaller marshaller = createStandaloneBinaryMarshaller(); try { marshaller.marshal(new Value1()); } catch (BinaryObjectException ex) { assertTrue(ex.getMessage().contains( "object [typeName=org.apache.ignite.internal.binary.BinaryObjectExceptionSelfTest$Value1")); assertTrue(ex.getCause().getMessage().contains("field [name=objVal")); } }
/** * @throws Exception If failed. */ @Test public void testProxy() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); SomeItf inItf = (SomeItf)Proxy.newProxyInstance( BinaryMarshallerSelfTest.class.getClassLoader(), new Class[] {SomeItf.class}, new InvocationHandler() { private NonSerializable obj = new NonSerializable(null); @Override public Object invoke(Object proxy, Method mtd, Object[] args) throws Throwable { if ("hashCode".equals(mtd.getName())) return obj.hashCode(); obj.checkAfterUnmarshalled(); return 17; } } ); SomeItf outItf = marsh.unmarshal(marsh.marshal(inItf), null); assertEquals(outItf.checkAfterUnmarshalled(), 17); }
/** {@inheritDoc} */ @Override protected BinaryObjectExImpl toBinary(BinaryMarshaller marsh, Object obj) throws Exception { byte[] arr = marsh.marshal(obj); long ptr = GridUnsafe.allocateMemory(arr.length); ptrs.add(ptr); GridUnsafe.copyHeapOffheap(arr, GridUnsafe.BYTE_ARR_OFF, ptr, arr.length); return new BinaryObjectOffheapImpl(binaryContext(marsh), ptr, 0, arr.length); } }
/** * Test object with {@link Proxy} field. * * @throws Exception If fails. */ @Test public void testObjectContainingProxy() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); SomeItf inItf = (SomeItf)Proxy.newProxyInstance( BinaryMarshallerSelfTest.class.getClassLoader(), new Class[] {SomeItf.class}, new InvocationHandler() { private NonSerializable obj = new NonSerializable(null); @Override public Object invoke(Object proxy, Method mtd, Object[] args) throws Throwable { if ("hashCode".equals(mtd.getName())) return obj.hashCode(); obj.checkAfterUnmarshalled(); return 17; } } ); SomeItf outItf = marsh.unmarshal(marsh.marshal(inItf), null); assertEquals(outItf.checkAfterUnmarshalled(), 17); }
/** * @throws Exception If failed. */ @Test public void testDuplicateNameFullNameMapper() throws Exception { BinaryMarshaller marsh = binaryMarshaller(new BinaryBasicNameMapper(false), new BinaryBasicIdMapper(false), null, null, null); Test1.Job job1 = new Test1().new Job(); Test2.Job job2 = new Test2().new Job(); marsh.marshal(job1); marsh.marshal(job2); }
/** * @throws Exception If failed. */ @Test public void testDuplicateNameSimpleNameMapper() throws Exception { BinaryMarshaller marsh = binaryMarshaller(new BinaryBasicNameMapper(true), new BinaryBasicIdMapper(true), null, null, null); Test1.Job job1 = new Test1().new Job(); Test2.Job job2 = new Test2().new Job(); marsh.marshal(job1); try { marsh.marshal(job2); } catch (BinaryObjectException e) { assertEquals(true, e.getMessage().contains("Failed to register class")); return; } assert false; }
assertEquals(simpleObj, marsh.unmarshal(marsh.marshal(simpleObj), null)); simpleBinary.arr = new long[] {100, 200, 300}; assertEquals(simpleBinary, marsh.unmarshal(marsh.marshal(simpleBinary), null)); simpleExtr.arr = new long[] {20000, 300000, 400000}; assertEquals(simpleExtr, marsh.unmarshal(marsh.marshal(simpleExtr), null));
/** * @throws Exception If failed. */ @Test public void testDeclaredBodyEnum() throws Exception { final MarshallerContextTestImpl ctx = new MarshallerContextTestImpl(); ctx.registerClassName((byte)0, 1, EnumObject.class.getName()); ctx.registerClassName((byte)0, 2, DeclaredBodyEnum.class.getName()); BinaryMarshaller marsh = binaryMarshaller(); marsh.setContext(ctx); EnumObject obj = new EnumObject(1L, "test 1", DeclaredBodyEnum.TWO); final byte[] marshal = marsh.marshal(obj); final Object restored = marsh.unmarshal(marshal, null); assertTrue(restored instanceof EnumObject); obj = (EnumObject)restored; assertEquals(1, obj.id); assertEquals(DeclaredBodyEnum.TWO.ordinal(), obj.type.ordinal()); assertEquals(DeclaredBodyEnum.TWO, obj.type); assertTrue(obj.type == DeclaredBodyEnum.TWO); }
/** * @throws Exception If failed. */ @Test public void testClassFieldsMarshalling() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); ObjectWithClassFields obj = new ObjectWithClassFields(); obj.cls1 = BinaryMarshallerSelfTest.class; byte[] marshal = marsh.marshal(obj); ObjectWithClassFields obj2 = marsh.unmarshal(marshal, null); assertEquals(obj.cls1, obj2.cls1); assertNull(obj2.cls2); BinaryObject portObj = marshal(obj, marsh); Class cls1 = portObj.field("cls1"); assertEquals(obj.cls1, cls1); }
/** * @throws IgniteCheckedException If failed. */ @Test public void testThreadLocalArrayReleased() throws Exception { // Checking the writer directly. assertEquals(false, INSTANCE.isAcquired()); BinaryMarshaller marsh = binaryMarshaller(); try (BinaryWriterExImpl writer = new BinaryWriterExImpl(binaryContext(marsh))) { assertEquals(true, INSTANCE.isAcquired()); writer.writeString("Thread local test"); writer.array(); assertEquals(true, INSTANCE.isAcquired()); } // Checking the binary marshaller. assertEquals(false, INSTANCE.isAcquired()); marsh = binaryMarshaller(); marsh.marshal(new SimpleObject()); assertEquals(false, INSTANCE.isAcquired()); marsh = binaryMarshaller(); // Checking the builder. BinaryObjectBuilder builder = new BinaryObjectBuilderImpl(binaryContext(marsh), "org.gridgain.foo.bar.TestClass"); builder.setField("a", "1"); BinaryObject binaryObj = builder.build(); assertEquals(false, INSTANCE.isAcquired()); }