JLChen
2021-11-15 44155b50cbb4f6ad78474f40331ed8838a3b0d49
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.hdl.sdk.connect.bean;
 
import android.text.TextUtils;
import android.util.Log;
 
import androidx.annotation.NonNull;
 
import com.hdl.sdk.common.config.AuthenticateConfig;
import com.hdl.sdk.connect.utils.AESUtils;
import com.hdl.sdk.connect.utils.AesUtil;
 
import java.io.UnsupportedEncodingException;
 
import static com.hdl.sdk.common.config.TopicConstant.DEIVCE_AUTH_REQUEST;
import static com.hdl.sdk.common.config.TopicConstant.GATEWAY_SEARCH;
 
/**
 * Created by Tong on 2021/9/29.
 */
public class LinkRequest {
    private String topic;
    private String data;
 
    private int length;
 
    public LinkRequest() {
    }
 
    public LinkRequest(String topic, String data) {
        this.topic = topic;
        setData(data);
    }
 
    public String getTopic() {
        return topic;
    }
 
    public void setTopic(String topic) {
        this.topic = topic;
    }
 
    public String getData() {
        return data;
    }
 
    public void setData(String data) {
        this.data = data;
        if (!TextUtils.isEmpty(data)) {
            setLength(getBytesLength(data));
//            setLength(data.length());
        } else {
            setLength(0);
        }
 
    }
 
    public int getLength() {
        return length;
    }
 
    private void setLength(int length) {
        this.length = length;
    }
 
    @NonNull
    @Override
    public String toString() {
        return "Topic:" +
                getTopic() +
                "\r\n" +
                "Length:" +
                getLength() +
                "\r\n\r\n" +
                getData();
    }
 
    private static byte[] stringToBytes(String str) {
        try {
            // 使用指定的字符集将此字符串编码为byte序列并存到一个byte数组中
            return str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new byte[]{};
    }
 
    private int getBytesLength(String str){
        return stringToBytes(str).length;
    }
 
    /**
     * 合并数组
     * @param bt1
     * @param bt2
     * @return
     */
    public static byte[] byteMerger(byte[] bt1, byte[] bt2){
        byte[] bt3 = new byte[bt1.length + bt2.length];
        System.arraycopy(bt1, 0, bt3, 0, bt1.length);
        System.arraycopy(bt2, 0, bt3, bt1.length, bt2.length);
        return bt3;
    }
    /**
     * 获取发送数据byte
     *
     * @return
     */
    public byte[] getSendBytes() {
        try {
            //判断是否需要加密
            if (AuthenticateConfig.getInstance().ifNeedEncrypt(topic)) {
                //需要加密
                byte[] dataBytes = AesUtil.aesEncrypt(stringToBytes(data), AuthenticateConfig.getInstance().getLocalSecret());
                String headString = "Topic:" + getTopic() + "\r\n" + "Length:" + dataBytes.length + "\r\n" + "\r\n";
//                byte[] headBytes = headString.getBytes("utf-8");
                byte[] headBytes = headString.getBytes();
                byte[] sendBytes = byteMerger(headBytes, dataBytes);
                return sendBytes;
            } else {
                return this.toString().getBytes("utf-8");
            }
 
        } catch (Exception e) {
            return new byte[]{};
        }
    }
}