序列化,反序列化 实现的本质_fei33423 反序列化_个人渣记录仅为自己搜索用的博客-CSDN博客
/*
* Ant Group
* Copyright (c) 2004-2023 All Rights Reserved.
*/
package HessianSerializer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import com.caucho.hessian.io.SerializerFactory;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Test;
/**
* @author admin
* @version d.java, v 0.1 2023年04月27日 下午3:28 admin
*/
public class HessianSerializer {
static HessianSerializer hessianSerializer = new HessianSerializer();
SerializerFactory serializerFactory = new SerializerFactory();
public static abstract class ToStringBase implements Serializable {
private static final long serialVersionUID = 2160996531438393638L;
public ToStringBase() {
}
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
public static class TestParent extends ToStringBase implements Serializable {
TestChild testChild = new TestChild();
}
public static class TestChild extends ToStringBase implements Serializable {
public String name = "1";
}
@Test
public void test() throws IOException {
//TestParent obj = new TestParent();
//Map<String,Object> map=new HashMap();
//map.put("kksd",obj);
//
//byte[] bytes = hessianSerializer.serializeObject(map);
//ArrayList<Object> objects1 = new ArrayList<>();
//
//for (byte aByte : bytes) {
// objects1.add(aByte);
//}
//String x = JSON.toJSONString(objects1);
String x = "[77,4,107,107,115,100,79,-56,85,99,111,109,46,97,108,105,112,97,121,46,102,105,110,109,111,100,101,108,99,111,114,101,"
+ "46,97,99,116,115,46,116,101,115,116,46,97,108,103,111,114,105,116,104,109,46,119,104,111,108,101,112,114,111,99,101,"
+ "115,115,46,72,101,115,115,105,97,110,83,101,114,105,97,108,105,122,101,114,36,84,101,115,116,80,97,114,101,110,116,"
+ "-111,9,116,101,115,116,67,104,105,108,100,111,-112,79,-56,84,99,111,109,46,97,108,105,112,97,121,46,102,105,110,109,"
+ "111,100,101,108,99,111,114,101,46,97,99,116,115,46,116,101,115,116,46,97,108,103,111,114,105,116,104,109,46,119,104,"
+ "111,108,101,112,114,111,99,101,115,115,46,72,101,115,115,105,97,110,83,101,114,105,97,108,105,122,101,114,36,84,101,"
+ "115,116,67,104,105,108,100,-111,4,110,97,109,101,111,-111,1,49,122]";
System.out.println(x);
List<Byte> maps = JSON.parseArray(x, Byte.class);
byte[] bytes = new byte[maps.size()];
for (int i = 0; i < maps.size(); i++) {
bytes[i] = maps.get(i);
}
System.out.println(Arrays.toString(bytes));
Map<String,Object> o = (Map<String,Object>) hessianSerializer.deserializeObject(bytes);
System.out.println(o);
}
/**
* 反序列化
*
* @param bytes
* @return
*/
public Object deserializeObject(byte[] bytes) {
InputStream is = new ByteArrayInputStream(bytes);
Hessian2Input h2in = new Hessian2Input(is);
h2in.setSerializerFactory(serializerFactory);
try {
return h2in.readObject();
} catch (Exception e) {
// 简单异常处理,把CheckedException转换为RuntimeException
throw new RuntimeException(e);
} finally {
try {
h2in.close();
} catch (IOException e) {
// ignore
}
}
}
/**
* 序列化
*
* @param obj
* @return
*/
public byte[] serializeObject(Object obj) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
Hessian2Output h2out = new Hessian2Output(os);
h2out.setSerializerFactory(serializerFactory);
try {
h2out.writeObject(obj);
h2out.flush();
} catch (Exception e) {
// 简单异常处理,把CheckedException转换为RuntimeException
throw new RuntimeException(e);
} finally {
try {
h2out.close();
} catch (IOException e) {
// ignore
}
}
return os.toByteArray();
}
}
第一遍执行
{kksd=HessianSerializer.TestParent[testChild=HessianSerializer.TestChild[name=1]]}
第二遍,把类注释掉,x变成 "之前打印出来的byte流"
{kksd={testChild={name=1}}}