java序列化缺点

  • 无法跨语言
  • 序列化后的码流太大
  • 序列化性能太低
import java.io.Serializable;
import java.nio.ByteBuffer;

public class UserInfo implements Serializable {
    private String userName;
    private int userId;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    /**
     * 二进制编码
      */
    public byte[] codeC(){
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        byte[] value = this.userName.getBytes();
        buffer.put(value);
        buffer.putInt(value.length);
        buffer.putInt(this.userId);
        buffer.flip();
        value = null;
        byte[] res = new byte[buffer.remaining()];
        buffer.get(res);
        return res;
    }

    public byte[] codeC(ByteBuffer buffer){
        buffer.clear();
        byte[] value = this.userName.getBytes();
        buffer.put(value);
        buffer.putInt(value.length);
        buffer.putInt(this.userId);
        buffer.flip();
        value = null;
        byte[] res = new byte[buffer.remaining()];
        buffer.get(res);
        return res;
    }
}

import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;

public class JavaSerialDemo {

    /**
     * java序列化编码长度与二进制编码对比
     */
    @Test
    public void test1() throws IOException {
        UserInfo info = new UserInfo();
        info.setUserId(100);
        info.setUserName("fly");
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream os = new ObjectOutputStream(bos);
        os.writeObject(info);
        os.flush();
        os.close();
        int length = bos.toByteArray().length; // 00
        // jdk序列化编码
        System.out.println(length);
        // 二进制编码
        System.out.println(info.codeC().length); //11
    }

    /**
     * java序列化性能与二进制编码对比
     */
    @Test
    public void test2() throws IOException {
        UserInfo info = new UserInfo();
        info.setUserId(100);
        info.setUserName("fly");
        int loop = 100000;
        ByteArrayOutputStream bos = null;
        ObjectOutputStream os = null;
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            bos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(bos);
            os.writeObject(info);
            os.flush();
            bos.toByteArray();
            os.close();
        }
        long end = System.currentTimeMillis();
        System.out.println(end - startTime); //631
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        startTime = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            info.codeC(buffer);
        }
        end = System.currentTimeMillis();
        System.out.println(end - startTime); //57
    }
}

posted @ 2021-09-26 15:27  fly_bk  阅读(153)  评论(0)    收藏  举报