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.util.ArrayList;
import java.util.Arrays;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.packet.util.CountingInputStream;

/* JADX INFO: loaded from: classes2.dex */
public class MqttUnsubscribe extends MqttPersistableWireMessage {
    private static final Byte[] validProperties = {(byte) 38};
    private MqttProperties properties;
    private String[] topics;

    public MqttUnsubscribe(byte[] bArr) throws MqttException, IOException {
        super((byte) 10);
        this.properties = new MqttProperties(validProperties);
        CountingInputStream countingInputStream = new CountingInputStream(new ByteArrayInputStream(bArr));
        DataInputStream dataInputStream = new DataInputStream(countingInputStream);
        this.msgId = dataInputStream.readUnsignedShort();
        this.properties.decodeProperties(dataInputStream);
        ArrayList arrayList = new ArrayList();
        while (countingInputStream.getCounter() < bArr.length) {
            arrayList.add(MqttDataTypes.decodeUTF8(dataInputStream));
        }
        this.topics = (String[]) arrayList.toArray(new String[arrayList.size()]);
        dataInputStream.close();
    }

    public MqttUnsubscribe(String[] strArr, MqttProperties mqttProperties) {
        super((byte) 10);
        this.topics = strArr;
        if (mqttProperties != null) {
            this.properties = mqttProperties;
        } else {
            this.properties = new MqttProperties();
        }
        this.properties.setValidProperties(validProperties);
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    protected byte[] getVariableHeader() throws MqttException {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            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
    public byte[] getPayload() throws MqttException {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            for (String str : this.topics) {
                MqttDataTypes.encodeUTF8(dataOutputStream, str);
            }
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    protected byte getMessageInfo() {
        return (byte) ((this.duplicate ? 8 : 0) | 2);
    }

    public String[] getTopics() {
        return this.topics;
    }

    public void setTopics(String[] strArr) {
        this.topics = strArr;
    }

    @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() {
        return "MqttUnsubscribe [topics=" + Arrays.toString(this.topics) + ", properties=" + this.properties + "]";
    }
}
