序列化和反序列化的三种方法 顶

1、字节位移 要穷尽所有的类型进行位移。

package com.guanjian.serialize;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;

/**
 * Created by Administrator on 2018-05-20.
 */
public class Test1 {
    public static void main(String[] args) throws IOException {
        int id = 101;
        int age = 21;
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        arrayOutputStream.write(int2bytes(id));
        arrayOutputStream.write(int2bytes(age));
        byte[] byteArray = arrayOutputStream.toByteArray();
        System.out.println(Arrays.toString(byteArray));

        ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray);
        byte[] idBytes = new byte[4];
        arrayInputStream.read(idBytes);
        System.out.println("id:" + bytes2int(idBytes));
        byte[] ageBytes = new byte[4];
        arrayInputStream.read(ageBytes);
        System.out.println("age:" + bytes2int(ageBytes));
    }
    public static byte[] int2bytes(int i) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (i >> 3*8);
        bytes[1] = (byte) (i >> 2*8);
        bytes[2] = (byte) (i >> 1*8);
        bytes[3] = (byte) (i >> 0*8);
        return bytes;
    }
    public static int bytes2int(byte[] bytes) {
        return (bytes[0] << 3*8) | (bytes[1] << 2*8) | (bytes[2] << 1*8) | (bytes[3] << 0*8);
    }
}

2、NIO buffer 要自己设定buffer的大小。

package com.guanjian.serialize;

import java.nio.ByteBuffer;
import java.util.Arrays;

/**
 * Created by Administrator on 2018-05-20.
 */
public class Test2 {
    public static void main(String[] args) {
        int id = 101;
        int age = 21;
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putInt(id);
        buffer.putInt(age);
        byte[] array = buffer.array();
        System.out.println(Arrays.toString(buffer.array()));

        ByteBuffer buffer2 = ByteBuffer.wrap(array);
        System.out.println("id:" + buffer2.getInt());
        System.out.println("age:" + buffer2.getInt());
    }
}

3、Netty3 buffer

package com.netty.test3;

import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;

import java.util.Arrays;

/**
 * Created by Administrator on 2018-05-20.
 */
public class Test3 {
    public static void main(String[] args) {
        int id = 101;
        int age = 21;
        double xp = 20.1;
        ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
        buffer.writeInt(id);
        buffer.writeInt(age);
        buffer.writeDouble(xp);
        byte[] bytes = new byte[buffer.writerIndex()];
        buffer.readBytes(bytes);
        System.out.println(Arrays.toString(bytes));

        ChannelBuffer warrpedBuffer = ChannelBuffers.wrappedBuffer(bytes);
        System.out.println("id:" + warrpedBuffer.readInt());
        System.out.println("age:" + warrpedBuffer.readInt());
        System.out.println("xp:" + warrpedBuffer.readDouble());
    }

}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券