Hession2 序列化 vs Jdk 序列化

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qfzhangwei/article/details/79675058

1 对一个对象序列化

import com.alibaba.dubbo.common.serialize.support.hessian.Hessian2ObjectInput;
import com.alibaba.dubbo.remoting.transport.netty4.ByteBufInputStream;
import com.google.common.io.Files;
import com.qunar.tc.core.info.api.JacksonSupport;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.io.*;

/**
 * Created by xiayin on 2018/3/23.
 */
public class codc {
    public static void main(String[] args) throws ClassNotFoundException {
        try {

            byte[] data = Files.asByteSource(new File("/Users/xiayin/123.txt")).read();
            System.out.println("hession2---" + String.valueOf(data)+"---"+data.length);//59

            ByteBuf buf = Unpooled.directBuffer();
            ByteBuf byteBuf = buf.writeBytes(data);
            ByteBufInputStream stream = new ByteBufInputStream(byteBuf);

            Hessian2ObjectInput objectInput = new Hessian2ObjectInput(null, stream);
            Object o = objectInput.readObject();
            System.out.println(JacksonSupport.toJson(o));
            System.out.println(o.getClass());


            /**jdk*/
            
            byte[] data2 = Files.asByteSource(new File("/Users/xiayin/456.txt")).read();
            System.out.println("jdk---" + String.valueOf(data2)+"---"+data2.length);//114

            InputStream inputStream = new ByteArrayInputStream(data2);

            ObjectInputStream oo = new ObjectInputStream(inputStream);
            Object o1 = oo.readObject();
            System.out.println(JacksonSupport.toJson(o1));
            System.out.println(o1.getClass());


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2 文件大小:网络传输大小 hession2 是 jdk的一半

[B@7bb11784
sun.misc.Launcher$AppClassLoader@18b4aac2
09:46:57.134 [main] DEBUG i.n.u.i.l.InternalLoggerFactory - Using SLF4J as the default logging framework
09:46:57.144 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.nio.ch.DirectBuffer.cleaner(): available
09:46:57.145 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
09:46:57.145 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
09:46:57.146 [main] DEBUG i.n.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
09:46:57.146 [main] DEBUG i.n.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: true
09:46:57.228 [main] DEBUG i.n.util.internal.PlatformDependent - UID: 501
09:46:57.232 [main] DEBUG i.n.util.internal.PlatformDependent - Java version: 8
09:46:57.232 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noUnsafe: false
09:46:57.232 [main] DEBUG i.n.util.internal.PlatformDependent - sun.misc.Unsafe: available
09:46:57.233 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noJavassist: false
09:46:57.358 [main] DEBUG i.n.util.internal.PlatformDependent - Javassist: available
09:46:57.359 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.tmpdir: /var/folders/tb/yhdkpm_s0k32b2q7g7t9hqx00000gn/T (java.io.tmpdir)
09:46:57.359 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
09:46:57.359 [main] DEBUG i.n.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
09:46:57.374 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetectionLevel: simple
{
  "aa" : "山东",
  "bb" : "滨州",
  "cc" : 1002
}
class com.xxx.genericpojo$mmm
jdk---[B@72d1ad2e---114
{
  "aa" : "山东",
  "bb" : "滨州",
  "cc" : 1002
}
class com.xxx.genericpojo$mmm

3 反序列化

import com.alibaba.dubbo.common.serialize.support.hessian.Hessian2ObjectInput;
import com.alibaba.dubbo.remoting.transport.netty4.ByteBufInputStream;
import com.google.common.io.Files;
import com.qunar.tc.core.info.api.JacksonSupport;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.io.*;

/**
 * Created by xiayin on 2018/3/23.
 */
public class codc {
    public static void main(String[] args) throws ClassNotFoundException {
        try {

            byte[] data = Files.asByteSource(new File("/Users/xiayin/123.txt")).read();
            System.out.println("hession2---" + String.valueOf(data)+"---"+data.length);

            ByteBuf buf = Unpooled.directBuffer();
            ByteBuf byteBuf = buf.writeBytes(data);
            ByteBufInputStream stream = new ByteBufInputStream(byteBuf);

            Hessian2ObjectInput objectInput = new Hessian2ObjectInput(null, stream);
            Object o = objectInput.readObject();
            System.out.println(JacksonSupport.toJson(o));
            System.out.println(o.getClass());


            /**jdk*/

            byte[] data2 = Files.asByteSource(new File("/Users/xiayin/456.txt")).read();
            System.out.println("jdk---" + String.valueOf(data2)+"---"+data2.length);

            InputStream inputStream = new ByteArrayInputStream(data2);

            ObjectInputStream oo = new ObjectInputStream(inputStream);
            Object o1 = oo.readObject();
            System.out.println(JacksonSupport.toJson(o1));
            System.out.println(o1.getClass());


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4 反序列化结果 hession2-- POJO map形式,jdk 不支持泛型

SLF4J: The following loggers will not work because they were created
SLF4J: during the default configuration phase of the underlying logging system.
SLF4J: See also http://www.slf4j.org/codes.html#substituteLogger
SLF4J: com.qunar.logback.flume.bean.RemoteFlumeAgent
三月 24, 2018 9:47:12 上午 com.alibaba.com.caucho.hessian.io.SerializerFactory getDeserializer
警告: Hessian/Burlap: 'com.xxx.genericpojo$mmm' is an unknown class in sun.misc.Launcher$AppClassLoader@18b4aac2:
java.lang.ClassNotFoundException: com.xxx.genericpojo$mmm
{
  "AA" : "山东",
  "BB" : "滨州",
  "CC" : 1002
}
class java.util.HashMap
jdk---[B@3246fb96---114
Exception in thread "main" java.lang.ClassNotFoundException: com.xxx.genericpojo$mmm
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
	at java.lang.Class.forName0(Native Method)
	at java.lang.Class.forName(Class.java:348)
	at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:677)
	at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1826)
	at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1713)
	at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2000)
	at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1535)
	at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)

猜你喜欢

转载自blog.csdn.net/qfzhangwei/article/details/79675058