下面列出了org.springframework.util.CommonsLogWriter#com.caucho.hessian.io.Hessian2Input 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public Hessian2Input unwrapHeaders(Hessian2Input in)
throws IOException
{
if (_privateKey == null)
throw new IOException("X509Encryption.unwrap requires a private key");
if (_cert == null)
throw new IOException("X509Encryption.unwrap requires a certificate");
InputStream is = new EncryptInputStream(in);
Hessian2Input filter = new Hessian2Input(is);
filter.setCloseStreamOnClose(true);
return filter;
}
public void close()
throws IOException
{
Hessian2Input in = _in;
_in = null;
if (in != null) {
_cipherIn.close();
_bodyIn.close();
int len = in.readInt();
if (len != 0)
throw new IOException("Unexpected footer");
in.completeEnvelope();
in.close();
}
}
@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);
}
@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();
}
@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 SerializerFactory());
assertEquals(true, hin.readObject());
assertEquals(false, hin.readObject());
assertEquals(true, hin.readObject());
hin.close();
}
@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 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();
}
@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 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 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 testBeanDeserialize() throws IOException {
byte[] bs = new byte[] { 79, -81, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 115, 116, 99, 46, 98
, 108, 46, 84, 101, 115, 116, 66, 108, 97, 99, 107, 66, 101, 97, 110,
-111, 6, 115, 116, 114, 105, 110, 103, 111, -112, 3, 115, 115, 115 };
ByteArrayInputStream input = new ByteArrayInputStream(bs, 0, bs.length);
Hessian2Input hin = new Hessian2Input(input);
hin.setSerializerFactory(serializerFactory);
try {
hin.readObject();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IOException);
}
}
@Test
public void testListDeserialize() throws IOException {
byte[] bs = new byte[] { 86, 110, 1, 79, -81, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 115,
116, 99, 46, 98, 108, 46, 84, 101, 115, 116, 66, 108, 97, 99, 107, 66, 101, 97, 110, -111,
6, 115, 116, 114, 105, 110, 103, 111, -112, 3, 115, 115, 115, 122 };
ByteArrayInputStream input = new ByteArrayInputStream(bs, 0, bs.length);
Hessian2Input hin = new Hessian2Input(input);
hin.setSerializerFactory(serializerFactory);
try {
hin.readObject();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IOException);
}
}
@Test
public void testMapDeserialize() throws IOException {
byte[] bs = new byte[] { 77, 79, -81, 99, 111, 109, 46, 97, 108, 105, 112, 97, 121, 46, 115, 116, 99, 46, 98,
108, 46, 84, 101, 115, 116, 66, 108, 97, 99, 107, 66, 101, 97, 110, -111, 6, 115, 116, 114, 105,
110, 103, 111, -112, 3, 115, 115, 115, 74, 1, 122 };
ByteArrayInputStream input = new ByteArrayInputStream(bs, 0, bs.length);
Hessian2Input hin = new Hessian2Input(input);
hin.setSerializerFactory(serializerFactory);
try {
hin.readObject();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IOException);
}
}
@Test
public void testHessian2Array() throws IOException {
SerializerFactory factory = new SerializerFactory();
String s = "567400075b6f626a6563746e025674001e5b636f6d2e73756e2e726f777365742e4a646263526f77536574496d706c6e014fad786f6d2e73756e2e726f777365742e4a646263526f77536574496d706cac07636f6d6d616e640355524c0a64617461536f757263650a726f77536574547970650b73686f7744656c657465640c717565727954696d656f7574076d6178526f77730c6d61784669656c6453697a650b636f6e63757272656e637908726561644f6e6c791065736361706550726f63657373696e670969736f6c6174696f6e08666574636844697209666574636853697a6504636f6e6e02707302727306726f77734d44057265734d440d694d61746368436f6c756d6e730f7374724d61746368436f6c756d6e730c62696e61727953747265616d0d756e69636f646553747265616d0b617363696953747265616d0a6368617253747265616d036d6170096c697374656e65727306706172616d736f904e4e4ecbec46909090cbf0545492cbe8904e4e4e4e4e567400106a6176612e7574696c2e566563746f726e0a8f8f8f8f8f8f8f8f8f8f7a76929a4e4e4e4e4e4e4e4e4e4e4e4e4e4e4e7692904d7400136a6176612e7574696c2e486173687461626c657a7a4a027a";
byte[] bs = hex2byte(s.getBytes());
Assert.assertTrue(s.equalsIgnoreCase(byte2hex(bs)));
ByteArrayInputStream input = new ByteArrayInputStream(bs, 0, bs.length);
Hessian2Input hin = new Hessian2Input(input);
hin.setSerializerFactory(factory);
try {
hin.readObject();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IOException);
}
}
/**
* 这个用例说明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> T readObject(InputBuf inputBuf, Class<T> clazz) {
Hessian2Input input = Inputs.getInput(inputBuf);
try {
Object obj = input.readObject(clazz);
return clazz.cast(obj);
} catch (IOException e) {
ThrowUtil.throwException(e);
} finally {
try {
input.close();
} catch (IOException ignored) {}
inputBuf.release();
}
return null; // never get here
}
/**
* 这个用例说明hessian写入的Enumeration,反序列化时时是ArrayList, 不兼容
* @throws Exception
*/
@Test
public void testEnumeration() throws Exception {
MyEnumerator myEnumerator = new MyEnumerator(0, 5, new Object[] { 1, 2, 3, 4, 5 });
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Hessian2Output hout = new Hessian2Output(bout);
hout.setSerializerFactory(new SerializerFactory());
hout.writeObject(myEnumerator);
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.getName(), o.getClass().getName());
}
@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]);
}
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();
}
@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 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]);
}
@Override
public void onApply(Iterator iter) {
while (iter.hasNext()) {
Closure done = iter.done();
ByteBuffer data = iter.getData();
ProcessRequest request;
LeaderTaskClosure closure = null;
if (done != null) {
closure = (LeaderTaskClosure) done;
request = closure.getRequest();
} else {
Hessian2Input input = new Hessian2Input(new ByteArrayInputStream(data.array()));
SerializerFactory serializerFactory = new SerializerFactory();
input.setSerializerFactory(serializerFactory);
try {
request = (ProcessRequest) input.readObject();
input.close();
} catch (IOException e) {
throw new RuntimeException(
"IOException occurred when Hessian serializer decode!", e);
}
}
ProcessResponse response = Processor.getInstance().process(request);
if (closure != null) {
closure.setResponse(response);
closure.run(Status.OK());
}
iter.next();
}
}
@Override
public GrpcResponse deserialize(GrpcService.Response response) {
byte[] bytes = response.getResponse().toByteArray();
UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(bytes);
try {
Hessian2Input input = new Hessian2Input(inputStream);
input.setSerializerFactory(serializerFactory);
GrpcResponse grpcResponse = (GrpcResponse) input.readObject();
input.close();
return grpcResponse;
} catch (IOException e) {
throw new GrpcException("sofa-hessian deserialize fail: " + e.getMessage());
}
}
@Override
public SofaResponse decodeObject(AbstractByteBuf data, Map<String, String> context) throws SofaRpcException {
try {
UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
Hessian2Input input = new Hessian2Input(inputStream);
// 根据SerializeType信息决定序列化器
Object object;
boolean genericSerialize = context != null && isGenericResponse(
context.get(RemotingConstants.HEAD_GENERIC_TYPE));
if (genericSerialize) {
input.setSerializerFactory(genericSerializerFactory);
GenericObject genericObject = (GenericObject) input.readObject();
SofaResponse sofaResponse = new SofaResponse();
sofaResponse.setErrorMsg((String) genericObject.getField("errorMsg"));
sofaResponse.setAppResponse(genericObject.getField("appResponse"));
sofaResponse.setResponseProps((Map<String, String>) genericObject.getField("responseProps"));
object = sofaResponse;
} else {
input.setSerializerFactory(serializerFactory);
object = input.readObject();
}
input.close();
return (SofaResponse) object;
} catch (IOException e) {
throw buildDeserializeError(e.getMessage(), e);
}
}
@Override
public void decodeObjectByTemplate(AbstractByteBuf data, Map<String, String> context, SofaResponse template)
throws SofaRpcException {
try {
UnsafeByteArrayInputStream inputStream = new UnsafeByteArrayInputStream(data.array());
Hessian2Input input = new Hessian2Input(inputStream);
// 根据SerializeType信息决定序列化器
boolean genericSerialize = context != null && isGenericResponse(
context.get(RemotingConstants.HEAD_GENERIC_TYPE));
if (genericSerialize) {
input.setSerializerFactory(genericSerializerFactory);
GenericObject genericObject = (GenericObject) input.readObject();
template.setErrorMsg((String) genericObject.getField("errorMsg"));
template.setAppResponse(genericObject.getField("appResponse"));
template.setResponseProps((Map<String, String>) genericObject.getField("responseProps"));
} else {
input.setSerializerFactory(serializerFactory);
SofaResponse tmp = (SofaResponse) input.readObject();
// copy values to template
template.setErrorMsg(tmp.getErrorMsg());
template.setAppResponse(tmp.getAppResponse());
template.setResponseProps(tmp.getResponseProps());
}
input.close();
} catch (IOException e) {
throw buildDeserializeError(e.getMessage(), e);
}
}
@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 SerializerFactory());
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();
}