下面列出了org.springframework.util.CommonsLogWriter#com.caucho.hessian.io.Hessian2Output 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected Task createTask(LeaderTaskClosure closure, ProcessRequest request) {
final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
Hessian2Output hessianOutput = new Hessian2Output(byteStream);
SerializerFactory serializerFactory = new SerializerFactory();
hessianOutput.setSerializerFactory(serializerFactory);
try {
hessianOutput.writeObject(request);
hessianOutput.close();
} catch (IOException e) {
LOGGER.error("Raft receive message serialize error!", e);
}
byte[] cmdBytes = byteStream.toByteArray();
ByteBuffer data = ByteBuffer.allocate(cmdBytes.length);
data.put(cmdBytes);
data.flip();
return new Task(data, closure);
}
@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]);
}
@org.junit.Test
public void testNull() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new GenericSerializerFactory());
hout.writeObject(dg.generateNull());
hout.close();
byte[] body = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
Hessian2Input hin = new Hessian2Input(bin);
hin.setSerializerFactory(new SerializerFactory());
assertNull(hin.readObject());
hin.close();
}
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();
}
@org.junit.Test
public void testDate() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new GenericSerializerFactory());
hout.writeObject(dg.generateDate_0());
hout.writeObject(dg.generateDate_1());
hout.writeObject(dg.generateDate_2());
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.generateDate_0(), hin.readObject());
assertEquals(dg.generateDate_1(), hin.readObject());
assertEquals(dg.generateDate_2(), hin.readObject());
hin.close();
}
@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]);
}
@org.junit.Test
public void testNull() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new SerializerFactory());
hout.writeObject(dg.generateNull());
hout.close();
byte[] body = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
Hessian2Input hin = new Hessian2Input(bin);
hin.setSerializerFactory(new GenericSerializerFactory());
assertNull(hin.readObject());
hin.close();
}
@org.junit.Test
public void testBoolean() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new SerializerFactory());
hout.writeObject(dg.generateTrue());
hout.writeObject(dg.generateFalse());
hout.writeObject(dg.generateTrue());
hout.close();
byte[] body = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
Hessian2Input hin = new Hessian2Input(bin);
hin.setSerializerFactory(new GenericSerializerFactory());
assertEquals(true, hin.readObject());
assertEquals(false, hin.readObject());
assertEquals(true, hin.readObject());
hin.close();
}
@org.junit.Test
public void testDate() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new SerializerFactory());
hout.writeObject(dg.generateDate_0());
hout.writeObject(dg.generateDate_1());
hout.writeObject(dg.generateDate_2());
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.generateDate_0(), hin.readObject());
assertEquals(dg.generateDate_1(), hin.readObject());
assertEquals(dg.generateDate_2(), hin.readObject());
hin.close();
}
@Override
public <T> byte[] writeObject(T obj) {
ByteArrayOutputStream buf = OutputStreams.getByteArrayOutputStream();
Hessian2Output output = Outputs.getOutput(buf);
try {
output.writeObject(obj);
output.flush();
return buf.toByteArray();
} catch (IOException e) {
ThrowUtil.throwException(e);
} finally {
try {
output.close();
} catch (IOException ignored) {}
OutputStreams.resetBuf(buf);
}
return null; // never get here
}
@org.junit.Test
public void testBoolean() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new GenericSerializerFactory());
hout.writeObject(dg.generateTrue());
hout.writeObject(dg.generateFalse());
hout.writeObject(dg.generateTrue());
hout.close();
byte[] body = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
Hessian2Input hin = new Hessian2Input(bin);
hin.setSerializerFactory(new GenericSerializerFactory());
assertEquals(true, hin.readObject());
assertEquals(false, hin.readObject());
assertEquals(true, hin.readObject());
hin.close();
}
@org.junit.Test
public void testDate() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new GenericSerializerFactory());
hout.writeObject(dg.generateDate_0());
hout.writeObject(dg.generateDate_1());
hout.writeObject(dg.generateDate_2());
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.generateDate_0(), hin.readObject());
assertEquals(dg.generateDate_1(), hin.readObject());
assertEquals(dg.generateDate_2(), hin.readObject());
hin.close();
}
@Test
public void testArraySerialize() throws IOException {
TestBlackBean blackBean = new TestBlackBean().setString("sss");
Object[] array = new Object[] { blackBean };
ByteArrayOutputStream output = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(output);
hout.setSerializerFactory(serializerFactory);
try {
hout.writeObject(array);
hout.flush();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IOException);
}
}
@Test
public void testMapSerialize() throws IOException {
TestBlackBean blackBean = new TestBlackBean().setString("sss");
Map<TestBlackBean, TestBlackBean> map = new HashMap<TestBlackBean, TestBlackBean>();
map.put(blackBean, blackBean);
ByteArrayOutputStream output = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(output);
hout.setSerializerFactory(serializerFactory);
try {
hout.writeObject(map);
hout.flush();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IOException);
}
}
public static void main(String[] args) throws IOException {
// Initial SerializerFactory
// It is highly recommended to cache this factory for every serialization and deserialization.
SerializerFactory serializerFactory = new SerializerFactory();
// Do serializer
String src = "xxx";
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(serializerFactory);
hout.writeObject(src);
hout.close();
byte[] data = bout.toByteArray();
// Do deserializer
ByteArrayInputStream bin = new ByteArrayInputStream(data, 0, data.length);
Hessian2Input hin = new Hessian2Input(bin);
hin.setSerializerFactory(new SerializerFactory());
String dst = (String) hin.readObject();
hin.close();
System.out.println(dst);
}
/**
* 这个用例说明hessian写入的InputStram,反序列化时是byte[], 不兼容
* @throws Exception
*/
@Test
public void testInputStream() throws Exception {
InputStream inputStream = new FileInputStream("pom.xml");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new SerializerFactory());
hout.writeObject(inputStream);
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(byte[].class, o.getClass());
}
/**
* 这个用例说明目前的GenericObject结构是支持Throwable的
* @throws Exception
*/
@Test
public void testThrowable() throws Exception {
MyException ex = new MyException("hello exception!");
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new SerializerFactory());
hout.writeObject(ex);
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();
assertEquals(GenericObject.class, o.getClass());
assertEquals(MyException.class.getName(), ((GenericObject) o).getType());
MyException myException = GenericUtils.convertToObject(o);
assertEquals(myException.getMessage(), "hello exception!");
}
@Override
public <T> OutputBuf writeObject(OutputBuf outputBuf, T obj) {
Hessian2Output output = Outputs.getOutput(outputBuf);
try {
output.writeObject(obj);
output.flush();
return outputBuf;
} catch (IOException e) {
ThrowUtil.throwException(e);
} finally {
try {
output.close();
} catch (IOException ignored) {}
}
return null; // never get here
}
@Override
public byte[] serialize(final Object obj) throws Exception {
if (obj == null) {
return null;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
AbstractHessianOutput output = new Hessian2Output(outputStream);
output.setSerializerFactory(SERIALIZER_FACTORY);
// 将对象写到流里
output.writeObject(obj);
output.flush();
byte[] val = outputStream.toByteArray();
output.close();
return val;
}
@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]);
}
@Test
public void testStringArray() throws Exception {
Object[] strs = new String[3];
strs[0] = "11111";
strs[1] = "22222";
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new GenericSerializerFactory());
hout.writeObject(strs);
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() == String[].class);
String[] s = (String[]) o;
assertTrue(3 == s.length);
assertEquals("11111", s[0]);
assertEquals("22222", s[1]);
assertEquals(null, s[2]);
}
@Test
public void test2StringArray() throws Exception {
Object[][] strs = new String[2][3];
strs[0][0] = "11111";
strs[1][1] = "22222";
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new GenericSerializerFactory());
hout.writeObject(strs);
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() == String[][].class);
String[][] s = (String[][]) o;
assertEquals("11111", s[0][0]);
assertEquals("22222", s[1][1]);
}
@Test
public void testGenericObjectArray() throws IOException {
GenericObject[] gpArr = dg.generateGenericObjectArray();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new GenericSerializerFactory());
hout.writeObject(gpArr);
hout.close();
byte[] body = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
Hessian2Input hin = new Hessian2Input(bin);
hin.setSerializerFactory(new SerializerFactory());
Object[] pArr = (Object[]) hin.readObject();
assertEquals(gpArr.length, pArr.length);
cmpGPersonEqualPerson(gpArr[0], (Person) pArr[0]);
cmpGPersonEqualPerson(gpArr[1], (Person) pArr[1]);
cmpGPersonEqualPerson(gpArr[2], (Person) pArr[2]);
}
@Test
public void testGenericArray() throws IOException {
GenericArray ga = dg.generateGenericArray();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new GenericSerializerFactory());
hout.writeObject(ga);
hout.close();
byte[] body = bout.toByteArray();
ByteArrayInputStream bin = new ByteArrayInputStream(body, 0, body.length);
Hessian2Input hin = new Hessian2Input(bin);
hin.setSerializerFactory(new SerializerFactory());
Person[] pArr = (Person[]) hin.readObject();
assertEquals(ga.getLength(), pArr.length);
cmpGPersonEqualPerson((GenericObject) ga.get(0), (Person) pArr[0]);
cmpGPersonEqualPerson((GenericObject) ga.get(1), (Person) pArr[1]);
cmpGPersonEqualPerson((GenericObject) ga.get(2), (Person) pArr[2]);
}
@Test
public void testHessian2Serialize() throws IOException {
Object person = dg.generateMixObject();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
long startTime = System.currentTimeMillis();
for (int i = 0; i < TEST_TIMES; ++i) {
bout.reset();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new SerializerFactory());
hout.writeObject(person);
hout.close();
}
long endTime = System.currentTimeMillis();
bout.close();
System.out.println("time of hessian2 serialization: " + (endTime - startTime) / 1000);
}
@Test
public void testGenericHessianSerialize() throws IOException {
Object person = dg.generateMixGenericObject();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
long startTime = System.currentTimeMillis();
for (int i = 0; i < TEST_TIMES; ++i) {
bout.reset();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new GenericSerializerFactory());
hout.writeObject(person);
hout.close();
}
long endTime = System.currentTimeMillis();
bout.close();
System.out.println("time of generic hessian: " + (endTime - startTime) / 1000);
}
public Hessian2Output wrap(Hessian2Output out)
throws IOException
{
if (_cert == null)
throw new IOException("X509Encryption.wrap requires a certificate");
OutputStream os = new EncryptOutputStream(out);
Hessian2Output filterOut = new Hessian2Output(os);
filterOut.setCloseStreamOnClose(true);
return filterOut;
}
public void close()
throws IOException
{
Hessian2Output out = _out;
_out = null;
if (out != null) {
_cipherOut.close();
_bodyOut.close();
out.writeInt(0);
out.completeEnvelope();
out.close();
}
}
private byte[] encode(Object object) {
UnsafeByteArrayOutputStream byteArray = new UnsafeByteArrayOutputStream();
Hessian2Output output = new Hessian2Output(byteArray);
try {
output.setSerializerFactory(serializerFactory);
output.writeObject(object);
output.close();
return byteArray.toByteArray();
} catch (Exception e) {
throw new GrpcException("sofa-hessian serialize fail: " + e.getMessage());
}
}
/**
* 将对象序列化为字节数组
*
* @param obj 待序列化对象
* @return
*/
public static byte[] serialize(Object obj) {
ByteArrayOutputStream ops = new ByteArrayOutputStream();
AbstractHessianOutput out = new Hessian2Output(ops);
out.setSerializerFactory(new SerializerFactory());
try {
out.writeObject(obj);
out.close();
} catch (IOException e) {
LOGGER.error("Hessian serialize failed.", e);
throw new RuntimeException("Hessian serialize failed");
}
return ops.toByteArray();
}