Java的序列化和反序列化

package com.itshidu.io;
 
import java.nio.charset.Charset;
import java.util.Arrays;
 
import com.itshidu.util.BitConverter;
 
/**
 * 发送的请求数据
 * 数据类型:String_255=0;String_65535=1;Int8=2;Int16=3;Int32=4;Int64=5;Float=6;Double=7;Boolean=8;Char=9;Byte[]=10;
 * String类型数值最大65535字节(UTF8字符集,每个汉字3字节)
 * @author Master.Xia
 */
public class NetMessage{
     
    public static byte[] Concat(byte[]... bs) {
        int len = 0,idx=0;
        for(byte[] b:bs)len+=b.length;
        byte[] buffer = new byte[len];
        for(byte[] b:bs) {
            System.arraycopy(b,0, buffer, idx, b.length);
            idx+=b.length;
        }
        return buffer;
    }
     
    public int code;  //0-255
    public int subCode;   //0-255
    public Object[] data;
    public byte[] GetBytes() {
        byte[] buffer = new byte[]{(byte)(code-128),(byte)(subCode-128)};
        buffer = Concat(buffer,new byte[] {(byte)data.length});
        for(int i=0;i<data.length;i++) {
            if(data[i] instanceof String) {          //String
                byte[] typeBytes = null;
                byte[] lenBytes = null;
                byte[] strBytes=((String)data[i]).getBytes(Charset.forName("UTF-8"));
                if(strBytes.length<=255) {
                    typeBytes = new byte[]{(byte)0};
                    lenBytes=new byte[]{(byte)(strBytes.length-128)};
                }else if(strBytes.length<=65535) {
                    typeBytes = new byte[]{(byte)1};
                    lenBytes=BitConverter.GetBytes((short)(strBytes.length-32768));
                }else {
                    throw new RuntimeException("String Byte Length Out of Bounds:"+strBytes.length);
                }
                buffer = Concat(buffer,typeBytes,lenBytes,strBytes);
            }else if(data[i] instanceof Byte) {     //Int8
                byte[] typeBytes = new byte[]{(byte)2};
                byte[] dataBytes = new byte[]{(byte)data[i]};
                buffer = Concat(buffer,typeBytes,dataBytes);
            }else if(data[i] instanceof Short) {    //Int16
                byte[] typeBytes = new byte[]{(byte)3};
                byte[] dataBytes=BitConverter.GetBytes((short)data[i]);
                buffer = Concat(buffer,typeBytes,dataBytes);
            }else if(data[i] instanceof Integer) {  //Int32
                byte[] typeBytes = new byte[]{(byte)4};
                byte[] dataBytes=BitConverter.GetBytes((int)data[i]);
                buffer = Concat(buffer,typeBytes,dataBytes);
            }else if(data[i] instanceof Long) {     //Int64
                byte[] typeBytes = new byte[]{(byte)5};
                byte[] dataBytes=BitConverter.GetBytes((long)data[i]);
                buffer = Concat(buffer,typeBytes,dataBytes);
            }else if(data[i] instanceof Float) {    //Float
                byte[] typeBytes = new byte[]{(byte)6};
                byte[] dataBytes = BitConverter.GetBytes((float)data[i]);
                buffer = Concat(buffer,typeBytes,dataBytes);
            }else if(data[i] instanceof Double) {   //Double
                byte[] typeBytes = new byte[]{(byte)7};
                byte[] dataBytes = BitConverter.GetBytes(Double.doubleToLongBits((double)data[i]));
                buffer = Concat(buffer,typeBytes,dataBytes);
            }else if(data[i] instanceof Boolean) {  //Boolean
                byte[] typeBytes = new byte[]{(byte)8};
                byte[] dataBytes = new byte[]{(byte)(((boolean)data[i])? 1:0)};
                buffer = Concat(buffer,typeBytes,dataBytes);
            }else if(data[i] instanceof Character) {//Char
                byte[] typeBytes = new byte[]{(byte)9};
                byte[] dataBytes = new String(new char[] {(char)data[i]}).getBytes(Charset.forName("UTF-8"));
                byte[] lenBytes = new byte[] { (byte)dataBytes.length };
                buffer = Concat(buffer,typeBytes,lenBytes,dataBytes);
            }else if(data[i] instanceof byte[]||data[i] instanceof Byte[]) {  //byte[]:|Type:1byte|DataLen:4byte|Data|
                byte[] typeBytes = new byte[]{(byte)10};
                byte[] dataBytes = null;
                if(data[i] instanceof Byte[]){
                    Byte[] bs1 = (Byte[])data[i];
                    dataBytes = new byte[bs1.length];
                    for(int m=0;m<bs1.length;m++){
                        dataBytes[m]=(byte)bs1[m];
                    }
                }else {
                    dataBytes = (byte[])data[i];
                }
                byte[] lenBytes=BitConverter.GetBytes(dataBytes.length);
                buffer = Concat(buffer,typeBytes,lenBytes,dataBytes);
            }
        }
        return buffer;
    }
    public NetMessage(byte[] bytes){
        this.code = bytes[0]+128;
        this.subCode = bytes[1]+128;
        int count = BitConverter.ByteToInt(bytes[2]);
        int index = 3;
        this.data = new Object[count];
        for(int i=0;i<count;i++) {
            int argType = BitConverter.ByteToInt(bytes[index++]);
            if(argType==0) { //String_255
                int len = BitConverter.ByteToInt((byte)(128+bytes[index++]));
                data[i] = new String(bytes,index,len,Charset.forName("UTF-8"));
                index += len;
            }else if(argType==1) { //String_65535
                int len = BitConverter.ToInt16(bytes, index)+32768;
                index += 2;
                data[i] = new String(bytes,index,len,Charset.forName("UTF-8"));
                index += len;
            }else if(argType==2) { //Byte
                data[i] = BitConverter.ByteToInt(bytes[index++]);
            }else if(argType==3) { //Short
                data[i] = BitConverter.ToInt16(bytes, index);
                index += 2;
            }else if(argType==4) { //Int
                data[i] = BitConverter.ToInt32(bytes, index);
                index += 4;
            }else if(argType==5) { //Long
                data[i] = BitConverter.ToInt64(bytes, index);
                index += 8;
            }else if(argType==6) { //Float
                data[i] = BitConverter.ToFloat(bytes, index);
                index += 4;
            }else if(argType==7) { //Double
                data[i] = Double.longBitsToDouble(BitConverter.ToInt64(bytes, index));
                index+=8;
            }else if(argType==8) { //Boolean
                data[i]=(bytes[index++]==0x00)? false:true;
            }else if(argType==9) { //Char
                Charset charset = Charset.forName("UTF-8");
                int len = BitConverter.ByteToInt(bytes[index++]);
                data[i] = new String(bytes,index,len,charset).toCharArray()[0];
                index += len;
            }else if(argType==10) { //Byte[]
                int len6 = BitConverter.ToInt32(bytes, index);
                index += 4;
                data[i] = new byte[len6];
                System.arraycopy(bytes, index, data[i], 0, len6);
                index += len6;
            }else {
                System.out.println("无法识别的类型码:"+argType);
            }
             
        }
    }
    public byte[] pack() {
        byte[] dataBytes = this.GetBytes();
        byte[] lenBytes = BitConverter.GetBytes(dataBytes.length);
        return Concat(lenBytes,dataBytes);
    }
    public NetMessage(byte[] bytes,int offset,int length) {
        this(Arrays.copyOfRange(bytes, offset, offset+length));
    }
 
    public NetMessage(int reqCode,int subCode,Object...objects) {
        this.code = reqCode;
        this.subCode = subCode;
        this.data = objects;
    }
    public NetMessage(int reqCode,int subCode) {
        this.code = reqCode;
        this.subCode = subCode;
    }
    public NetMessage(int reqCode) {
        this.code = reqCode;
    }
     
     
    public static void main(String[] args) {
        NetMessage a = new NetMessage(
                (byte)0,(byte)200,(byte)100,(short)1000,(int)10000,(long)100000,1.414f,3.141592654,true,false,'@','哈'
                ,"HelloWorld".getBytes()
                ,"中华人民共和国万岁".getBytes()
                ,123
                );
         
        byte[] bs = a.GetBytes();
         
        NetMessage b = new NetMessage(bs);
        System.out.println("ReqCode:"+b.code);
        System.out.println("SubCode:"+b.subCode);
        for(Object o : b.data) {
            System.out.println(o);
        }
        System.out.println(new String((byte[])b.data[b.data.length-2]));
         
    }
     
     

}

点击打开

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值