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 MqttDisconnect extends MqttWireMessage {
    public static final String KEY = "Disc";
    private MqttProperties properties;
    private int returnCode;
    private static final int[] validReturnCodes = {0, 4, 128, MqttReturnCode.RETURN_CODE_MALFORMED_CONTROL_PACKET, MqttReturnCode.RETURN_CODE_PROTOCOL_ERROR, MqttReturnCode.RETURN_CODE_IMPLEMENTATION_SPECIFIC_ERROR, MqttReturnCode.RETURN_CODE_NOT_AUTHORIZED, MqttReturnCode.RETURN_CODE_SERVER_BUSY, MqttReturnCode.RETURN_CODE_SERVER_SHUTTING_DOWN, MqttReturnCode.RETURN_CODE_KEEP_ALIVE_TIMEOUT, MqttReturnCode.RETURN_CODE_SESSION_TAKEN_OVER, MqttReturnCode.RETURN_CODE_TOPIC_FILTER_NOT_VALID, 144, MqttReturnCode.RETURN_CODE_RECEIVE_MAXIMUM_EXCEEDED, 148, 149, MqttReturnCode.RETURN_CODE_MESSAGE_RATE_TOO_HIGH, 151, MqttReturnCode.RETURN_CODE_ADMINISTRITIVE_ACTION, 153, 154, MqttReturnCode.RETURN_CODE_QOS_NOT_SUPPORTED, MqttReturnCode.RETURN_CODE_USE_ANOTHER_SERVER, MqttReturnCode.RETURN_CODE_SERVER_MOVED, 158, 159, 160, 161, 162};
    private static final Byte[] validProperties = {(byte) 17, (byte) 28, (byte) 31, (byte) 38};

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    protected byte getMessageInfo() {
        return (byte) 0;
    }

    public MqttDisconnect(byte[] bArr) throws MqttException, IOException {
        super((byte) 14);
        this.returnCode = 0;
        this.properties = new MqttProperties(validProperties);
        CountingInputStream countingInputStream = new CountingInputStream(new ByteArrayInputStream(bArr));
        DataInputStream dataInputStream = new DataInputStream(countingInputStream);
        if (bArr.length - countingInputStream.getCounter() >= 1) {
            int unsignedByte = dataInputStream.readUnsignedByte();
            this.returnCode = unsignedByte;
            validateReturnCode(unsignedByte, validReturnCodes);
        }
        if (((long) bArr.length) - ((long) countingInputStream.getCounter()) >= 2) {
            this.properties.decodeProperties(dataInputStream);
        }
        dataInputStream.close();
    }

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

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

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