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.MqttSubscription;
import org.eclipse.paho.mqttv5.common.packet.util.CountingInputStream;

/* JADX INFO: loaded from: classes2.dex */
public class MqttSubscribe extends MqttPersistableWireMessage {
    private static final Byte[] validProperties = {(byte) 11, (byte) 127, (byte) 38};
    private MqttProperties properties;
    private MqttSubscription[] subscriptions;

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

    public MqttSubscribe(byte[] bArr) throws MqttException, IOException {
        super((byte) 8);
        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(decodeSubscription(MqttDataTypes.decodeUTF8(dataInputStream), dataInputStream.readByte()));
        }
        this.subscriptions = (MqttSubscription[]) arrayList.toArray(new MqttSubscription[arrayList.size()]);
        dataInputStream.close();
    }

    public MqttSubscribe(MqttSubscription[] mqttSubscriptionArr, MqttProperties mqttProperties) {
        super((byte) 8);
        this.subscriptions = mqttSubscriptionArr;
        if (mqttProperties != null) {
            this.properties = mqttProperties;
        } else {
            this.properties = new MqttProperties();
        }
        this.properties.setValidProperties(validProperties);
    }

    public MqttSubscribe(MqttSubscription mqttSubscription, MqttProperties mqttProperties) {
        super((byte) 8);
        this.subscriptions = new MqttSubscription[]{mqttSubscription};
        this.properties = mqttProperties;
        mqttProperties.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 (MqttSubscription mqttSubscription : this.subscriptions) {
                dataOutputStream.write(encodeSubscription(mqttSubscription));
            }
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    private byte[] encodeSubscription(MqttSubscription mqttSubscription) throws MqttException {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            MqttDataTypes.encodeUTF8(dataOutputStream, mqttSubscription.getTopic());
            byte qos = (byte) mqttSubscription.getQos();
            if (mqttSubscription.isNoLocal()) {
                qos = (byte) (qos | 4);
            }
            if (mqttSubscription.isRetainAsPublished()) {
                qos = (byte) (qos | 8);
            }
            dataOutputStream.write((byte) ((mqttSubscription.getRetainHandling() << 4) | qos));
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    private MqttSubscription decodeSubscription(String str, byte b) {
        MqttSubscription mqttSubscription = new MqttSubscription(str);
        mqttSubscription.setQos(b & 3);
        mqttSubscription.setNoLocal((b & 4) != 0);
        mqttSubscription.setRetainAsPublished((b & 8) != 0);
        mqttSubscription.setRetainHandling((b >> 4) & 3);
        return mqttSubscription;
    }

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

    public MqttSubscription[] getSubscriptions() {
        return this.subscriptions;
    }

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