下面列出了org.springframework.util.CommonsLogWriter#com.caucho.hessian.io.HessianInput 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testBeanDeserialize() throws IOException {
byte[] bs = new byte[] { 77, 116, 0, 31, 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, 83, 0, 6, 115, 116,
114, 105, 110, 103, 83, 0, 3, 115, 115, 115, 122 };
ByteArrayInputStream input = new ByteArrayInputStream(bs, 0, bs.length);
HessianInput hin = new HessianInput(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, 108, 0, 0, 0, 1, 77, 116, 0, 31, 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, 83, 0, 6, 115, 116, 114, 105, 110, 103, 83, 0, 3, 115, 115, 115, 122, 122 };
ByteArrayInputStream input = new ByteArrayInputStream(bs, 0, bs.length);
HessianInput hin = new HessianInput(input);
hin.setSerializerFactory(serializerFactory);
try {
hin.readObject();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IOException);
}
}
@Test
public void testArrayDeserialize() throws IOException {
byte[] bs = new byte[] { 86, 116, 0, 7, 91, 111, 98, 106, 101, 99, 116, 108, 0, 0, 0, 1, 77, 116, 0, 31, 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, 83, 0, 6, 115, 116, 114, 105, 110, 103, 83, 0, 3, 115, 115, 115,
122, 122 };
ByteArrayInputStream input = new ByteArrayInputStream(bs, 0, bs.length);
HessianInput hin = new HessianInput(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, 116, 0, 0, 77, 116, 0, 31, 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, 83, 0, 6, 115, 116, 114, 105, 110, 103, 83, 0, 3, 115, 115, 115, 122, 82, 0, 0, 0, 1, 122 };
ByteArrayInputStream input = new ByteArrayInputStream(bs, 0, bs.length);
HessianInput hin = new HessianInput(input);
hin.setSerializerFactory(serializerFactory);
try {
hin.readObject();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IOException);
}
}
@Test
public void testHessian1Array() 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);
HessianInput hin = new HessianInput(input);
hin.setSerializerFactory(factory);
try {
hin.readObject();
Assert.fail();
} catch (Exception e) {
Assert.assertTrue(e instanceof IOException);
}
}
public static Object read(byte[] data) {
ByteArrayInputStream is = new ByteArrayInputStream(data);
HessianInput hi = new HessianInput(is);
try {
return hi.readObject();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
hi.close();
is.close();
} catch (Exception ex) {
}
}
}
public static Object deserialize(byte[] by, ClassLoader classLoader) throws IOException {
if (by == null)
throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
ClassLoader old = null;
if (classLoader != null) {
old = Thread.currentThread().getContextClassLoader();
// 切换当前线程classloader,保证动态加载的类不会报CNF
Thread.currentThread().setContextClassLoader(classLoader);
}
HessianInput hi = new HessianInput(is);
Object obj = hi.readObject();
if (classLoader != null) {
Thread.currentThread().setContextClassLoader(old);
}
return obj;
}
public static <T> Object deserialize(byte[] bytes, Class<T> clazz) {
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
HessianInput hi = new HessianInput(is);
try {
return hi.readObject();
} catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 对象赋值
User user = new User();
user.setName("老王");
user.setAge(30);
System.out.println(user);
// -------------------------- Java 原生序列化 ----------------------------
// 创建输出流(序列化内容到磁盘)
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.out"));
// 序列化对象
oos.writeObject(user);
oos.flush();
oos.close();
// 创建输入流(从磁盘反序列化)
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.out"));
// 反序列化
User user2 = (User) ois.readObject();
ois.close();
System.out.println(user2);
// -------------------------- JSON 序列化(fastjson) ----------------------------
String jsonSerialize = JSON.toJSONString(user);
User user3 = (User) JSON.parseObject(jsonSerialize, User.class);
System.out.println(user3);
// -------------------------- Hessian 序列化 ----------------------------
// 序列化
ByteArrayOutputStream bo = new ByteArrayOutputStream();
HessianOutput hessianOutput = new HessianOutput(bo);
hessianOutput.writeObject(user);
byte[] hessianBytes = bo.toByteArray();
// 反序列化
ByteArrayInputStream bi = new ByteArrayInputStream(hessianBytes);
HessianInput hessianInput = new HessianInput(bi);
User user4 = (User) hessianInput.readObject();
System.out.println(user4);
}
@Override
public Row deserialize(byte[] bytes) throws IOException {
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
HessianInput hi = new HessianInput(is);
Object object = hi.readObject();
if (object.getClass() != clazz) {
throw new ClassCastException("expected class is:" + clazz + " but is :" + object.getClass());
}
return ConvertRowUtil.convertToRow(object, ((RowTypeInfo)typeInfo).getFieldNames());
}
private void beforeProviderInvokeForSVC(Invocation invocation, TransactionRequestImpl request,
TransactionResponseImpl response) {
CompensableBeanRegistry beanRegistry = CompensableBeanRegistry.getInstance();
CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory();
TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();
RemotingException rpcError = null;
String transactionContextContent = invocation.getAttachment(TransactionContext.class.getName());
String propagatedBy = invocation.getAttachment(RemoteCoordinator.class.getName());
if (StringUtils.isNotBlank(transactionContextContent)) {
byte[] requestByteArray = ByteUtils.stringToByteArray(transactionContextContent);
ByteArrayInputStream bais = new ByteArrayInputStream(requestByteArray);
HessianInput input = new HessianInput(bais);
try {
TransactionContext remoteTransactionContext = (TransactionContext) input.readObject();
remoteTransactionContext.setPropagatedBy(propagatedBy);
request.setTransactionContext(remoteTransactionContext);
} catch (IOException ex) {
logger.error("Error occurred in remote call!", ex);
rpcError = new RemotingException(ex.getMessage());
}
}
try {
transactionInterceptor.afterReceiveRequest(request);
} catch (RuntimeException rex) {
logger.error("Error occurred in remote call!", rex);
throw new RemotingException(rex.getMessage());
}
if (rpcError != null) {
throw rpcError;
}
}
private void afterConsumerInvokeForSVC(Invocation invocation, TransactionRequestImpl request,
TransactionResponseImpl response) {
CompensableBeanRegistry beanRegistry = CompensableBeanRegistry.getInstance();
CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory();
TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();
RemotingException rpcError = null;
try {
if (request.getTransactionContext() != null) {
String transactionContextContent = invocation.getAttachment(TransactionContext.class.getName());
byte[] byteArray = ByteUtils.stringToByteArray(transactionContextContent);
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
HessianInput input = new HessianInput(bais);
TransactionContext remoteTransactionContext = (TransactionContext) input.readObject();
response.setTransactionContext(remoteTransactionContext);
}
} catch (IOException ex) {
logger.error("Error occurred in remote call!", ex);
rpcError = new RemotingException(ex.getMessage());
}
try {
transactionInterceptor.afterReceiveResponse(response);
} catch (RuntimeException rex) {
logger.error("Error occurred in remote call!", rex);
throw new RemotingException(rex.getMessage());
}
if (rpcError != null) {
throw rpcError;
}
}
private void beforeProviderInvokeForSVC(Invocation invocation, TransactionRequestImpl request,
TransactionResponseImpl response) {
CompensableBeanRegistry beanRegistry = CompensableBeanRegistry.getInstance();
CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory();
TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();
RemotingException rpcError = null;
String transactionContextContent = invocation.getAttachment(TransactionContext.class.getName());
String propagatedBy = invocation.getAttachment(RemoteCoordinator.class.getName());
if (StringUtils.isNotBlank(transactionContextContent)) {
byte[] requestByteArray = ByteUtils.stringToByteArray(transactionContextContent);
ByteArrayInputStream bais = new ByteArrayInputStream(requestByteArray);
HessianInput input = new HessianInput(bais);
try {
TransactionContext remoteTransactionContext = (TransactionContext) input.readObject();
remoteTransactionContext.setPropagatedBy(propagatedBy);
request.setTransactionContext(remoteTransactionContext);
} catch (IOException ex) {
logger.error("Error occurred in remote call!", ex);
rpcError = new RemotingException(ex.getMessage());
}
}
try {
transactionInterceptor.afterReceiveRequest(request);
} catch (RuntimeException rex) {
logger.error("Error occurred in remote call!", rex);
throw new RemotingException(rex.getMessage());
}
if (rpcError != null) {
throw rpcError;
}
}
private void afterConsumerInvokeForSVC(Invocation invocation, TransactionRequestImpl request,
TransactionResponseImpl response) {
CompensableBeanRegistry beanRegistry = CompensableBeanRegistry.getInstance();
CompensableBeanFactory beanFactory = beanRegistry.getBeanFactory();
TransactionInterceptor transactionInterceptor = beanFactory.getTransactionInterceptor();
RemotingException rpcError = null;
try {
if (request.getTransactionContext() != null) {
String transactionContextContent = invocation.getAttachment(TransactionContext.class.getName());
byte[] byteArray = ByteUtils.stringToByteArray(transactionContextContent);
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
HessianInput input = new HessianInput(bais);
TransactionContext remoteTransactionContext = (TransactionContext) input.readObject();
response.setTransactionContext(remoteTransactionContext);
}
} catch (IOException ex) {
logger.error("Error occurred in remote call!", ex);
rpcError = new RemotingException(ex.getMessage());
}
try {
transactionInterceptor.afterReceiveResponse(response);
} catch (RuntimeException rex) {
logger.error("Error occurred in remote call!", rex);
throw new RemotingException(rex.getMessage());
}
if (rpcError != null) {
throw rpcError;
}
}
@Override
public Object decode(byte[] bytes, Class<?> returnType) throws IOException {
if (bytes == null) {
return null;
}
HessianInput input = new HessianInput(new ByteArrayInputStream(bytes));
input.setSerializerFactory(this.serializerFactory);
return input.readObject(returnType);
}
public static Serializable hessianDeserialize(byte[] bytes) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
HessianInput hi = new HessianInput(bais);
try {
Object result = hi.readObject();
return (Serializable) result;
} finally {
CommonUtils.closeQuietly(bais);
}
}
/**
* {@inheritDoc}
*
* @see marshalsec.AbstractHessianBase#createInput(java.io.ByteArrayInputStream)
*/
@Override
protected AbstractHessianInput createInput ( ByteArrayInputStream bos ) {
return new HessianInput(bos);
}