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