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.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 MqttUnsubAck extends MqttAck {
    private MqttProperties properties;
    private static final int[] validReturnCodes = {0, 17, 128, MqttReturnCode.RETURN_CODE_IMPLEMENTATION_SPECIFIC_ERROR, MqttReturnCode.RETURN_CODE_NOT_AUTHORIZED, MqttReturnCode.RETURN_CODE_TOPIC_FILTER_NOT_VALID, MqttReturnCode.RETURN_CODE_PACKET_ID_IN_USE};
    private static final Byte[] validProperties = {(byte) 31, (byte) 38};

    public MqttUnsubAck(byte[] bArr) throws MqttException, IOException {
        super((byte) 11);
        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);
        int length = bArr.length - countingInputStream.getCounter();
        this.reasonCodes = new int[length];
        for (int i = 0; i < length; i++) {
            this.reasonCodes[i] = dataInputStream.readUnsignedByte();
            validateReturnCode(this.reasonCodes[i], validReturnCodes);
        }
        dataInputStream.close();
    }

    public MqttUnsubAck(int[] iArr, MqttProperties mqttProperties) throws MqttException {
        super((byte) 11);
        for (int i : iArr) {
            validateReturnCode(i, validReturnCodes);
        }
        this.reasonCodes = iArr;
        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 (int i : this.reasonCodes) {
                dataOutputStream.writeByte(i);
            }
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    public int[] getReturnCodes() {
        return this.reasonCodes;
    }

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