package org.eclipse.paho.mqttv5.common.packet;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.MqttMessage;
import org.eclipse.paho.mqttv5.common.packet.util.CountingInputStream;

/* JADX INFO: loaded from: classes2.dex */
public class MqttPublish extends MqttPersistableWireMessage {
    private static final Byte[] validProperties = {(byte) 1, (byte) 2, (byte) 35, (byte) 8, (byte) 9, (byte) 38, (byte) 3, Byte.valueOf(MqttProperties.SUBSCRIPTION_IDENTIFIER_MULTI), (byte) 11};
    private boolean dup;
    private byte[] payload;
    private MqttProperties properties;
    private int qos;
    private boolean retained;
    private String topicName;

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    public boolean isMessageIdRequired() {
        return true;
    }

    public MqttPublish(String str, MqttMessage mqttMessage, MqttProperties mqttProperties) {
        super((byte) 3);
        this.qos = 1;
        this.retained = false;
        this.dup = false;
        this.topicName = str;
        this.payload = mqttMessage.getPayload();
        this.qos = mqttMessage.getQos();
        this.dup = mqttMessage.isDuplicate();
        this.retained = mqttMessage.isRetained();
        if (mqttProperties != null) {
            this.properties = mqttProperties;
        } else {
            this.properties = new MqttProperties();
        }
        this.properties.setValidProperties(validProperties);
    }

    public MqttPublish(byte b, byte[] bArr) throws MqttException, IOException {
        super((byte) 3);
        this.qos = 1;
        this.retained = false;
        this.dup = false;
        this.properties = new MqttProperties(validProperties);
        this.qos = 3 & (b >> 1);
        if ((b & 1) == 1) {
            this.retained = true;
        }
        if ((b & 8) == 8) {
            this.dup = true;
        }
        CountingInputStream countingInputStream = new CountingInputStream(new ByteArrayInputStream(bArr));
        DataInputStream dataInputStream = new DataInputStream(countingInputStream);
        this.topicName = MqttDataTypes.decodeUTF8(dataInputStream);
        if (this.qos > 0) {
            this.msgId = dataInputStream.readUnsignedShort();
        }
        this.properties.decodeProperties(dataInputStream);
        byte[] bArr2 = new byte[bArr.length - countingInputStream.getCounter()];
        this.payload = bArr2;
        dataInputStream.readFully(bArr2);
        dataInputStream.close();
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    protected byte[] getVariableHeader() throws MqttException {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            String str = this.topicName;
            if (str != null) {
                MqttDataTypes.encodeUTF8(dataOutputStream, str);
            } else {
                MqttDataTypes.encodeUTF8(dataOutputStream, "");
            }
            if (this.qos > 0) {
                dataOutputStream.writeShort(this.msgId);
            }
            dataOutputStream.write(this.properties.encodeProperties());
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    protected byte getMessageInfo() {
        byte b = (byte) (this.qos << 1);
        if (this.retained) {
            b = (byte) (b | 1);
        }
        return (this.dup || this.duplicate) ? (byte) (b | 8) : b;
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    public byte[] getPayload() {
        return this.payload;
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttPersistableWireMessage, org.eclipse.paho.mqttv5.common.MqttPersistable
    public int getPayloadLength() {
        byte[] bArr = this.payload;
        if (bArr != null) {
            return bArr.length;
        }
        return 0;
    }

    public MqttMessage getMessage() {
        return new MqttMessage(this.payload, this.qos, this.retained, this.properties);
    }

    public void setMessage(MqttMessage mqttMessage) {
        this.payload = mqttMessage.getPayload();
        this.qos = mqttMessage.getQos();
        this.dup = mqttMessage.isDuplicate();
        this.retained = mqttMessage.isRetained();
    }

    public String getTopicName() {
        return this.topicName;
    }

    public int getQoS() {
        return this.qos;
    }

    public void setTopicName(String str) {
        this.topicName = str;
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    public MqttProperties getProperties() {
        return this.properties;
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    public String toString() {
        String str;
        StringBuilder sb = new StringBuilder();
        int iMin = Math.min(this.payload.length, 20);
        for (int i = 0; i < iMin; i++) {
            String hexString = Integer.toHexString(this.payload[i]);
            if (hexString.length() == 1) {
                hexString = "0" + hexString;
            }
            sb.append(hexString);
        }
        try {
            str = new String(this.payload, 0, iMin, "UTF-8");
        } catch (UnsupportedEncodingException unused) {
            str = "?";
        }
        StringBuilder sb2 = new StringBuilder("MqttPublish [, qos=");
        sb2.append(this.qos);
        if (this.qos > 0) {
            sb2.append(", messageId=").append(this.msgId);
        }
        sb2.append(", retained=").append(this.retained);
        sb2.append(", duplicate=").append(this.duplicate);
        sb2.append(", topic=").append(this.topicName);
        sb2.append(", payload=[hex=").append((CharSequence) sb);
        sb2.append(", utf8=").append(str);
        sb2.append(", length=").append(this.payload.length).append("], properties=");
        sb2.append(this.properties.toString());
        return sb2.toString();
    }
}
