第九课 自定义序列化协议

9 篇文章 0 订阅
public class Test1 {

    public static void main(String[] args) throws IOException {
        int id= 101;
        int age= 21;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(int2byte(id));
        byteArrayOutputStream.write(int2byte(age));

        byte[] byteArray = byteArrayOutputStream.toByteArray();

        System.out.println(Arrays.toString(byteArray));
        System.out.println("==================================");
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArray);
        byte[] idbytes = new byte[4];
        byteArrayInputStream.read(idbytes);
        System.out.println("id:"+byte2int(idbytes));

        byte[]  agebytes = new byte[4];
        byteArrayInputStream.read(agebytes);
        System.out.println("age:"+byte2int(agebytes));

    }
    /**
     * @param i
     * @return bytes
     * @category 大端字节序列(先写高位,在写低位)=====小端字节序列(先写低位,在写高位)
     */
    public static byte[] int2byte(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;
    }
    /**
     * @param b
     */
    public static int byte2int(byte[] b){
        return  b[0]<<3*8|
                b[1]<<2*8|
                b[2]<<1*8|
                b[3]<<0*8;
    }
}

public class Test2 {

    public static void main(String[] args) {
        int id= 101;
        int age= 21;
        //指定大小  固定不变
        ByteBuffer byteBuffer = ByteBuffer.allocate(8);
        byteBuffer.putInt(id);
        byteBuffer.putInt(age);

        //java.nio.BufferOverflowException  buffer溢出  无法动态调整大小
        //byteBuffer.putLong(1L);

        byte[] array = byteBuffer.array();
        System.out.println(Arrays.toString(array));


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


        System.out.println();
    }
}

public class Test3 {

    public static void main(String[] args) {

        ChannelBuffer dynamicBuffer = ChannelBuffers.dynamicBuffer();
        dynamicBuffer.writeInt(101);
        dynamicBuffer.writeDouble(80.1);


        //byte[] stringByte = "abc".getBytes();

        byte[] bytes = new byte[dynamicBuffer.writerIndex()];
        dynamicBuffer.readBytes(bytes);

        System.out.println(Arrays.toString(bytes));

        //反序列化
        ChannelBuffer wrappedBuffer = ChannelBuffers.wrappedBuffer(bytes);
        System.out.println(wrappedBuffer.readInt());
        System.out.println(wrappedBuffer.readDouble());
    }
}

public class Test4 {

    public static void main(String[] args) {
        //自定义序列化接口 该类继承了自定义的Serializer类
        Player player = new Player();
        player.setAge(20);
        player.setPlayId(100);
        player.getSkills().add(1);
        byte[] bytes = player.getBytes();
        System.out.println(Arrays.toString(bytes));


        Player player2 = new Player();
        player2.readFromBytes(bytes);
        System.out.println(player2);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值