hxb
2022-09-08 2a01ef5e49422cca49bc7476fc1b8be8c8556561
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
package com.hdl.sdk.link.core.bean;
 
 
import com.hdl.sdk.link.common.utils.ByteUtils;
import com.hdl.sdk.link.common.utils.TextUtils;
 
 
/**
 * Created by Tong on 2021/9/29.
 */
public class LinkRequest {
    private String topic;
    protected byte []data;
    private int length=0;
 
    private boolean encrypt;
 
 
    public LinkRequest(String topic, String data, boolean encrypt) {
        this.topic = topic;
        setData(data);
        this.encrypt = encrypt;
    }
    public LinkRequest(String topic, byte []data, boolean encrypt) {
        this.topic = topic;
        setData(data);
        this.encrypt = encrypt;
    }
 
    protected LinkRequest(){}
 
    public String getTopic() {
        return topic;
    }
 
    public void setTopic(String topic) {
        this.topic = topic;
    }
 
    public byte[] getData() {
        return data;
    }
 
    public void setData(String data) {
        if (!TextUtils.isEmpty(data)) {
            this.data = ByteUtils.stringToBytes(data);
            setLength(this.data.length);
        }
    }
 
    public void setData(byte []data) {
        this.data = data;
        if(data!=null){
            setLength(data.length);
        }
    }
 
    public int getLength() {
        return length;
    }
 
    private void setLength(int length) {
        this.length = length;
    }
 
    /**
     * 加密标识
     *
     * @return
     */
    public boolean isEncrypt() {
        return encrypt;
    }
 
    /**
     * 加密标识
     *
     * @param encrypt
     */
    public void setEncrypt(boolean encrypt) {
        this.encrypt = encrypt;
    }
 
 
    public byte [] getSendBytes() {
        try {
            String header = "Topic:" +
                    getTopic() +
                    "\r\n" +
                    "Length:" +
                    getLength() +
                    "\r\n\r\n";
            return ByteUtils.concatBytes(header.getBytes("utf-8"), getData());
        } catch (Exception e) {
            return new byte[]{};
        }
    }
 
 
    private int getBytesLength(String str) {
        return ByteUtils.stringToBytes(str).length;
    }
 
}