wjc
2025-05-23 971a24a9e58a21bc306897fd3ad63012a399f7db
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
package com.hdl.sdk.link.core.utils;
 
import android.text.TextUtils;
 
/**
 * Created by Tong on 2021/9/22.
 * 解析Link协议
 */
public class ProtocolParse {
 
    private String topic;
    private int length;
    private int dataIndex;
 
    public ProtocolParse(byte[] bytes) {
        parse(bytes);
    }
 
    private void parse(byte[] bytes) {
        try {
            String[] split = new String(bytes, "utf-8").split("\r\n");
            setTopic(parseTopic(split));
            setLength(parseLength(split));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private static String parseTopic(String[] bytes) {
        try {
            for (String s : bytes) {
                if (s.startsWith("Topic:")) {
                    return s.replace("Topic:", "");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    private static int parseLength(String[] bytes) {
        try {
            for (String s : bytes) {
                if (!TextUtils.isEmpty(s) && s.startsWith("Length:")) {
                    return Integer.parseInt(s.replace("Length:", ""));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }
 
 
 
    public String getTopic() {
        return topic;
    }
 
    public void setTopic(String topic) {
        this.topic = topic;
    }
 
    public int getLength() {
        return length;
    }
 
    public void setLength(int length) {
        this.length = length;
    }
 
 
}