java协议通信

 

/**
 * 处理数据
 *
 * @author    刘海伦
 * @version   V0.1 2015年6月15日[版本号, YYYY-MM-DD]
 * @see       [相关类/方法]
 * @since     transfer
 */
public class DataUtils {

    /**
     * 
     * int转byte数组
     * 
     * @param i
     * @return byte[]
     * @exception/throws
     */
    public static byte[] toByteArr(int i) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) i;
        bytes[1] = (byte) (i >> 8);
        bytes[2] = (byte) (i >> 16);
        bytes[3] = (byte) (i >> 24);
        return bytes;
    }
}
 


package com.quekua.transfer.protocol;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import com.quekua.iCommons.convert.ByteConvert;
import com.quekua.transfer.TransferContext;
import com.quekua.transfer.utils.DataUtils;

/**
 * 封装要传输(接收和发送)的数据
 * 传输协议:类型(1byte) + 命令长度(4byte) + 数据长度(4byte)+ 命令 + 数据。
 * 
 * @author    刘海伦
 * @version   V0.1 2015年6月15日[版本号, YYYY-MM-DD]
 * @see       [相关类/方法]
 * @since     transfer
 */
public class TransferData implements Serializable{

    /**
     * 属性serialVersionUID
     */
    private static final long serialVersionUID = -3345315849544712547L;

    /**
     * 类型(传输时,只占一个字节)
     * 为了用户使用方便,这里使用整型表示,在传输时,才强转为字节
     */
    private int type;
    
    /**
     * 命令长度(传输时,占4个字节,即一个整型)
     */
    private int commandLength;
    
    /**
     * 数据长度(传输时,占4个字节,即一个整型)
     */
    private int dataLength;
    
    /**
     * 命令
     * COMMAND [SETTING,SITEINFO,CONFIGINFO,ASSIGN,DATA,MONITOR,LOG]
     */
    private String command = "";
    
    /**
     * 数据
     * List<UrlInfo>或List<DataInfo>转成Json字符串
     */
    private byte[] data;

    /**
     * @return 属性 dataLength
     */
    public int getDataLength() {
        return dataLength;
    }
    
    /**
     * @return 属性 command
     */
    public String getCommand() {
        return command;
    }

    /**
     * 设置属性 command 值
     * @throws UnsupportedEncodingException 
     */
    public void setCommand(String command) throws UnsupportedEncodingException {
        this.command = command;
        
        if(command == null){
            this.commandLength = 0;
        } else {
            this.commandLength = command.getBytes(TransferContext.DEFAUT_ENCODE).length;
        }
    }

    /**
     * 设置属性 data 值
     */
    public void setData(byte[] data) {
        this.data = data;
        
        if(data == null){
            this.dataLength = 0;
        } else {
            this.dataLength = data.length;
        }
    }

    /**
     * @return 属性 data
     */
    public byte[] getData() {
        return data;
    }

    /**
     * @return 属性 commandLength
     */
    public int getCommandLength() {
        return commandLength;
    }

    /**
     * @return 属性 type
     */
    public int getType() {
        return type;
    }

    /**
     * 设置属性 type 值
     */
    public void setType(int type) {
        this.type = type;
    }
    
    /**
     * 
     * 将TransferData转换成byte[]
     *
     * @return 
     * byte[]
     * @throws UnsupportedEncodingException 
     * @exception/throws
     */
    public byte[] toByte() throws UnsupportedEncodingException {
        byte[] _commandLength = DataUtils.toByteArr(commandLength);
        byte[] _dataLength = DataUtils.toByteArr(dataLength);
        byte[] _command = null;
        if(command != null){
            _command = command.getBytes(TransferContext.DEFAUT_ENCODE);
        }
        //根据约定的传输协议组合传输数据
        ByteConvert bC = new ByteConvert();
        bC.append((byte)type);
        bC.append(_commandLength);
        bC.append(_dataLength);
        bC.append(_command);
        bC.append(data);
        return bC.toArray();
    }
}

/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package com.quekua.iCommons.convert;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

public class ByteConvert {
    private static final int BUFFER_SIZE = 32768;
    private byte[] buffer = new byte[32768];

    private int index = 0;

    public byte[] toArray() {
        byte[] result = new byte[this.index];
        if (this.index > 0) {
            System.arraycopy(this.buffer, 0, result, 0, this.index);
            this.buffer = new byte[32768];
        }
        this.index = 0;
        return result;
    }

    public String toString() {
        return new String(toArray());
    }

    public String toString(String charset) throws UnsupportedEncodingException {
        return new String(toArray(), charset);
    }

    public void append(byte[] data) {
        if (data == null) {
            return;
        }

        int max = this.index + data.length;
        byte[] temp = new byte[max];
        if (this.index > 0) {
            System.arraycopy(this.buffer, 0, temp, 0, this.buffer.length);
        }
        System.arraycopy(data, 0, temp, this.index, data.length);
        this.buffer = temp;
        this.index = max;
    }

    public void append(byte[] data, int length) {
        if ((data == null) || (length <= 0)) {
            return;
        }
        byte[] temp = new byte[length];
        System.arraycopy(data, 0, temp, 0, length);
        append(temp);
    }

    public void append(byte b) {
        append(new byte[] { b });
    }

    public void read(InputStream in, int max, boolean isFileStream) throws IOException {
        int len = 0;
        byte[] temp = new byte[max];
        int minLength = (isFileStream) ? 0 : max;
        do {
            len = in.read(temp);
            if (len > 0) {
                append(temp, len);
            }
        } while (len >= minLength);
    }

    public void read(InputStream in, boolean isFileStream) throws IOException {
        read(in, 32768, isFileStream);
    }

    public void read(InputStream in) throws IOException {
        read(in, 32768, true);
    }

    public static byte[] cut(byte[] source, int startPos, int length) {
        byte[] temp = new byte[0];
        if ((length > 0) && (startPos < source.length)) {
            int max = (length > source.length - startPos) ? source.length - startPos : length;
            temp = new byte[max];
            System.arraycopy(source, startPos, temp, 0, max);
        }
        return temp;
    }

    public void clear() {
        this.index = 0;
        this.buffer = new byte[this.index];
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值