netty6---序列化与反序列化

package com.cn;

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

public class Test1 {

    public static void main(String[] args) throws IOException {
        int id = 101;  //把id转换为字节数据,一个int占4个字节,
        int age = 21;
        
        //int转换为字节数组
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        arrayOutputStream.write(int2bytes(id));
        arrayOutputStream.write(int2bytes(age));
        
        byte[] byteArray = arrayOutputStream.toByteArray();//[0, 0, 0, 101, 0, 0, 0, 21]
        
        System.out.println(Arrays.toString(byteArray));
        
        //字节数组转换为int
        ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray);
        byte[] idBytes = new byte[4];//id占4个字节
        arrayInputStream.read(idBytes);
        System.out.println("id:" + bytes2int(idBytes));//id:101
        
        byte[] ageBytes = new byte[4];
        arrayInputStream.read(ageBytes);//age占4个字节
        System.out.println("age:" + bytes2int(ageBytes));//age:21
        
    }
    
    
    /**
     int转换成字节数组,使用的是大端序列方式。
     2种方式:
     1.大端字节序列:先写高位,再写低位
     2.小端字节序列:先写低位,再写高位
     */
    public static byte[] int2bytes(int i){
        byte[] bytes = new byte[4];  //int占4个字节
        bytes[0] = (byte)(i >> 3*8);//先获取高位,总共4个字节,向右移动3个字节,一个字节8位,
        bytes[1] = (byte)(i >> 2*8);
        bytes[2] = (byte)(i >> 1*8);
        bytes[3] = (byte)(i >> 0*8);
        return bytes;//[0, 0, 0, 101],[0, 0, 0, 21]
    }
    
    
    /**
     * 把字节数组转换为int。
     * int转换为字节数组时候bytes[0]是高位,向左移动3个字节,
     * 
     * 大端序列方式:
     */
    public static int bytes2int(byte[] bytes){
        return (bytes[0] << 3*8) |
                (bytes[1] << 2*8) |
                (bytes[2] << 1*8) |
                (bytes[3] << 0*8);
    }
}
package com.cn;

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

public class Test2 {

    public static void main(String[] args) {
        int id = 101;
        int age = 21;
        
        //序列化
        //nio里面的ByteBuffer,申请8个空间,必须给定指定的大小,不会自动扩容。再写数据就会报错超出容量。
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.putInt(id);
        buffer.putInt(age);
        byte[] array = buffer.array();
        System.out.println(Arrays.toString(buffer.array()));//[0, 0, 0, 101, 0, 0, 0, 21]
        
        //反序列化
        ByteBuffer buffer2 = ByteBuffer.wrap(array);
        System.out.println("id:"+buffer2.getInt());//先写的是id
        System.out.println("age:"+buffer2.getInt());//后写的是age
        //id:101,age:21
    }

}
package com.cn;

import java.util.Arrays;

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

public class Test3 {
    public static void main(String[] args) {

        //netty里面的ChannelBuffer,封装了ByteBuffer,可以自动扩容。
        ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
        buffer.writeInt(101);
        buffer.writeDouble(80.1);

        byte[] bytes = new byte[buffer.writerIndex()];//大小根据ChannelBuffer的写指针决定。
        buffer.readBytes(bytes);
        
        System.out.println(Arrays.toString(bytes));//[0, 0, 0, 101, 64, 84, 6, 102, 102, 102, 102, 102]
        System.out.println("abc".getBytes());//[B@29453f44
        
        //
        ChannelBuffer wrappedBuffer = ChannelBuffers.wrappedBuffer(bytes);//传一个字节数组
        System.out.println(wrappedBuffer.readInt());
        System.out.println(wrappedBuffer.readDouble());
        
    }

}

 

posted @ 2018-05-18 15:42  无天666  阅读(509)  评论(0编辑  收藏  举报