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

/* JADX INFO: loaded from: classes2.dex */
public class MqttPubComp extends MqttAck {
    private MqttProperties properties;
    private static final int[] validReturnCodes = {0, MqttReturnCode.RETURN_CODE_PACKET_ID_NOT_FOUND};
    private static final Byte[] validProperties = {(byte) 31, (byte) 38};

    public MqttPubComp(byte[] bArr) throws MqttException, IOException {
        super((byte) 7);
        this.properties = new MqttProperties(validProperties);
        CountingInputStream countingInputStream = new CountingInputStream(new ByteArrayInputStream(bArr));
        DataInputStream dataInputStream = new DataInputStream(countingInputStream);
        this.msgId = dataInputStream.readUnsignedShort();
        long length = ((long) bArr.length) - ((long) countingInputStream.getCounter());
        if (length >= 1) {
            this.reasonCode = dataInputStream.readUnsignedByte();
            validateReturnCode(this.reasonCode, validReturnCodes);
        } else {
            this.reasonCode = 0;
        }
        if (length >= 4) {
            this.properties.decodeProperties(dataInputStream);
        }
        dataInputStream.close();
    }

    public MqttPubComp(int i, int i2, MqttProperties mqttProperties) throws MqttException {
        super((byte) 7);
        validateReturnCode(i, validReturnCodes);
        this.reasonCode = i;
        this.msgId = i2;
        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);
            byte[] bArrEncodeProperties = this.properties.encodeProperties();
            if (this.reasonCode != 0 && bArrEncodeProperties.length == 1) {
                dataOutputStream.write((byte) this.reasonCode);
            } else if (this.reasonCode != 0 || bArrEncodeProperties.length > 1) {
                dataOutputStream.write((byte) this.reasonCode);
                dataOutputStream.write(bArrEncodeProperties);
            }
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    public int getReturnCode() {
        return this.reasonCode;
    }

    public void setReturnCode(int i) {
        this.reasonCode = i;
    }

    @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 "MqttPubComp [returnCode=" + this.reasonCode + ", properties=" + this.properties + "]";
    }
}
