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; } }