In remote calls, parameters and return values need to be transmitted over the network. This use requires serialization to convert the object into a byte stream, and then deserialize it back into an object from one end to the other.
Since there is a previous article mentioning hessian, here we will briefly talk about the difference between Java serialization and hessian serialization.
First of all, hessian serialization is much more efficient than Java serialization, and the generated byte stream is much shorter. But relatively speaking, Java serialization is not reliable, and it is not as comprehensive as Java serialization support. The reason for such a difference is to look at their implementation.
Let’s talk about Java serialization first. I won’t talk about the specific working principle. Java serialization will serialize the metadata and business data of the object class to be serialized from the byte stream, and will serialize all the things in the entire inheritance relationship. The byte stream it serializes is a complete description of the object structure to the content, containing all the information, so it is less efficient and the byte stream is relatively large. But since everything is indeed serialized, it can be said that everything can be transferred, so it is also more usable and reliable.
As for hessian serialization, its implementation mechanism is to focus on data and accompany simple type information. Just like Integer a = 1, hessian will serialize into a stream like I 1. I represents int or Integer, and 1 is the data content. For complex objects, through the Java reflection mechanism, hessian serializes all the properties of the object as a Map, producing a stream like M className propertyName1 I 1 propertyName S stringValue (probably, forget it exactly), which contains the basic type description and data content. During the serialization process, if an object appears before, hessian will directly insert a block such as R index to represent a reference position, thus eliminating the time of serialization and deserialization again. The price of doing this is that hessian needs to handle different types differently (so hessian is lazy and does not support short), and he also needs to handle special operations when encountering certain special objects (such as StackTraceElement). And at the same time, because it does not go deep into the implementation to perform serialization, there will be certain inconsistencies in some occasions, such as maps obtained through Collections.synchronizedMap.
Serialization refers to serializing an object into a byte stream for easy storage or network transmission; while deserialization is just the opposite, converting the byte stream back to an object. What we usually use is the hessian serialization method and the java serialization method. The efficiency and serialization size of the two serialization methods are different. Judging from the test results, hessian is better. Here is a hessian serialization example (no file IO and network IO, pure serialization and deserialization):
/** * Pure hessian serialization* @param object * @return * @throws Exception */public static byte[] serialize(Object object) throws Exception{if(object==null){throw new NullPointerException();}ByteArrayOutputStream os = new ByteArrayOutputStream();HessianSerializerOutput hessianOutput=new HessianSerializerOutput(os);hessianOutput.writeObject(object);return os.toByteArray();}/** * Pure hessian deserialization* @param bytes * @return * @throws Exception */public static Object deserialize(byte[] bytes) throws Exception{if(bytes==null){throw new NullPointerException();}ByteArrayInputStream is = new ByteArrayInputStream(bytes);HessianSerializerInput hessianInput=new HessianSerializerInput(is);Object object = hessianInput.readObject();return object;}Another common method is java serialization:
/** * java serialization* @param obj * @return * @throws Exception */public static byte[] serialize(Object obj) throws Exception {if (obj == null)throw new NullPointerException();ByteArrayOutputStream os = new ByteArrayOutputStream();ObjectOutputStream out = new ObjectOutputStream(os);out.writeObject(obj);return os.toByteArray();}/** * java deserialization* @param by * @return * @throws Exception */public static Object deserialize(byte[] by) throws Exception {if (by == null)throw new NullPointerException();ByteArrayInputStream is = new ByteArrayInputStream(by);ObjectInputStream in = new ObjectInputStream(is);return in.readObject();}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.