当前位置:网站首页>Analysis of Hessian serialization principle
Analysis of Hessian serialization principle
2022-07-07 08:57:00 【bboyzqh】
List of articles
1. hessian Serialization example
1.1 hessian Protocol Brief
1.1.1 characteristic
Refer to the description in the official document , Here are some brief descriptions ,http://hessian.caucho.com/doc/hessian-serialization.html.
- It must self describe the serialization type , That is, no external schema or interface definition is required
- It must be language independent , Including support for scripting languages
- It must be able to read and write in a single way
- It must be as compact as possible
- It must be simple , Only in this way can we effectively test and implement
- Must be as fast as possible
- It has to support Unicode character string
- It has to support 8 Bit binary data , There is no need to escape or use attachments
- It must support encryption 、 Compress 、 Signature and transaction context envelopes
1.1.2 hessian Syntactic introduction
The following examples , More reference :http://hessian.caucho.com/doc/hessian-serialization.html.
#boolean true/false
boolean ::= 'T'
::= 'F'
# list/vector
list ::= 'V' type? length? value* 'z'
::= 'v' int int value* # first int Represents a type reference , the second int Length
#32-bit signed int ( such as 0x90 Encoded as 0)
int ::= 'I' b3 b2 b1 b0
::= [x80-xbf] #-x10 to x3f
::= [xc0-xcf] b0 #-x800 to x7ff
::= [xd0-xd7] b1 b0 #-x40000 to x3ffff
1.2 hessian Examples of serialization and deserialization
public static void serialize1(Student student){
FileOutputStream fileOutputStream;
try {
fileOutputStream = new FileOutputStream("/Users/zhuqiuhui/Desktop/studentHession.txt");
// Get byte stream from object
ByteArrayOutputStream os = new ByteArrayOutputStream();
Hessian2Output output = new Hessian2Output(os);
output.writeObject(student);
output.getBytesOutputStream().flush();
output.completeMessage();
output.close();
// Write to a file
fileOutputStream.write(os.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
}
public static Student deserialize1(){
FileInputStream fileInputStream;
Object result = null;
try {
fileInputStream = new FileInputStream("/Users/zhuqiuhui/Desktop/studentHession.txt");
byte[] data = new byte[1024];
int len = fileInputStream.read(data);
System.out.println("read byte length:" + len);
// Read the object from the stream
ByteArrayInputStream is = new ByteArrayInputStream(data);
Hessian2Input input = new Hessian2Input(is);
result = input.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return (Student)result;
}
public static void main(String[] args) {
Student student = new Student();
student.setId("123");
student.setName(" Fang Chen ");
// serialize
// serialize1(student);
// Deserialization
Student serializestudent = deserialize1();
System.out.println("deserialize result entity id is "+serializestudent.getId());
System.out.println("deserialize result entity name is "+serializestudent.getName());
}
1.3 hessian Deal with the jdk difference
- Difference one :java Serialization cannot cross language
- Difference two : Versions of old and new objects Java Through one serialVersionUID To relate , Developers need to pay attention to the semantics of serialization
- Difference three :java Serialization does not support encryption
- Difference 4 :Java Serialized content ratio hessian Big
2. hessian Serialization analysis
2.1 hessian Serialization must serialVersionUID Do you ?
hessian Serialized objects do not need to be added serialVersionUID,hessian When serializing, write the description information of the class to byte[] in
2.2 hessian Serialization and deserialization source code analysis
2.2.1 ( back ) The corresponding relationship of serializers
| type | Serializer | Deserializer |
|---|---|---|
| Collection | CollectionSerializer | CollectionDeserializer |
| Map | MapSerializer | MapDeserializer |
| Iterator | IteratorSerializer | IteratorDeserializer |
| Annotation | AnnotationSerializer | AnnotationDeserializer |
| Interface | ObjectSerializer | ObjectDeserializer |
| Array | ArraySerializer | ArrayDeserializer |
| Enumeration | EnumerationSerializer | EnumerationDeserializer |
| Enum | EnumSerializer | EnumDeserializer |
| Class | ClassSerializer | ClassDeserializer |
| Default | JavaSerializer | JavaDeserializer |
| Throwable | ThrowableSerializer | |
| InputStream | InputStreamSerializer | InputStreamDeserializer |
| InetAddress | InetAddressSerializer |
2.2.2 Why serialize objects implements Serializable Interface ?
// com.caucho.hessian.io.SerializerFactory The default serializer will be obtained when serializing
protected Serializer getDefaultSerializer(Class cl) {
if (_defaultSerializer != null)
return _defaultSerializer;
// If the serialized object is not implemented Serializable Interface , It will be thrown out. IllegalStateException
if (! Serializable.class.isAssignableFrom(cl)
&& ! _isAllowNonSerializable) {
throw new IllegalStateException("Serialized class " + cl.getName() + " must implement java.io.Serializable");
}
if (_isEnableUnsafeSerializer
&& JavaSerializer.getWriteReplace(cl) == null) {
return UnsafeSerializer.create(cl);
}
else
return JavaSerializer.create(cl);
}
2.2.3 Serialization process and deserialization process
- Serialization process :Hessian2Output -> SerializerFactory -> Serializer
- Deserialization process :Hessian2Input -> SerializerFactory -> Deserializer
2.3 summary
- Serialize objects to achieve Serializable Interface , Otherwise, it will report “must implement java.io.Serializable” abnormal
- If the serialized object passes hessian After serialization , Serialized object without serialVersionUID when , Re change ( Add object properties 、 Delete object properties ) Will not generate deserialization exceptions , namely hessian Serializing objects no longer requires serialVersionUID.
- hessian It stores all the attributes of a complex object in one Map Serialization in . So in the parent class 、 When a subclass has a member variable with the same name , Hessian Serialization , Serialize subclasses first , Then serialize the parent class , Therefore, the result of deserialization will cause the member variable with the same name of the child class to be overwritten by the value of the parent class .
- hessian Medium writeReplace Methods and readResolve The method works the same , If the serialized object has this method , The instance returned by this method will be used to replace the serialized instance , To ensure the singleness of the object .
边栏推荐
- Troublesome problem of image resizing when using typora to edit markdown to upload CSDN
- C language for calculating the product of two matrices
- 面试题:高速PCB一般布局、布线原则
- go mod module declares its path as: gtihub. com/xxx-xx but was required as:xx-xx
- LeetCode 736. Lisp 语法解析
- Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
- [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
- let const
- OpenGL三维图形绘制
- ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
猜你喜欢

【踩坑】nacos注册一直连接localhost:8848,no available server

Interpolation lookup (two methods)

Unityshader introduction essentials personal summary -- Basic chapter (I)

Greenplum6.x-版本变化记录-常用手册
![[step on the pit] Nacos registration has been connected to localhost:8848, no available server](/img/ee/ab4d62745929acec2f5ba57155b3fa.png)
[step on the pit] Nacos registration has been connected to localhost:8848, no available server

Quick sorting (detailed illustration of single way, double way, three way)

面试题:高速PCB一般布局、布线原则
![Data analysis methodology and previous experience summary 2 [notes dry goods]](/img/e1/643e847a777e1effcbd39f8b009e2b.png)
Data analysis methodology and previous experience summary 2 [notes dry goods]

Routing information protocol rip

Output all composite numbers between 6 and 1000
随机推荐
年薪50w阿裏P8親自下場,教你如何從測試進階
Greenplum 6.x version change record common manual
Enterprise manager cannot connect to the database instance
[MySQL] detailed explanation of trigger content of database advanced
Mountaineering team (DFS)
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - package based on thinkphp5 - detailed guidance of the author - July 6, 2022 - Novice
[Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
go mod module declares its path as: gtihub. com/xxx-xx but was required as:xx-xx
OpenGL三维图形绘制
selenium自动化集成,八年测试经验软测工程师,一篇文章带你学懂
MAC OSX php dyld: Library not loaded: /usr/local/xxxx. dylib
QT charts use (rewrite qchartview to realize some custom functions)
ncs成都新電面試經驗
NCS Chengdu Xindian interview experience
Greenplum 6.x reinitialization
Routing information protocol rip
Nanjing commercial housing sales enabled electronic contracts, and Junzi sign assisted in the online signing and filing of housing transactions
2022-06-30 unity core 8 - model import
UnityShader入门精要个人总结--基础篇(一)
Output all composite numbers between 6 and 1000