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;

/* JADX INFO: loaded from: classes2.dex */
public class MqttConnAck extends MqttAck {
    public static final String KEY = "Con";
    private MqttProperties properties;
    private boolean sessionPresent;
    private static final int[] validReturnCodes = {0, 128, MqttReturnCode.RETURN_CODE_MALFORMED_CONTROL_PACKET, MqttReturnCode.RETURN_CODE_PROTOCOL_ERROR, MqttReturnCode.RETURN_CODE_IMPLEMENTATION_SPECIFIC_ERROR, MqttReturnCode.RETURN_CODE_UNSUPPORTED_PROTOCOL_VERSION, MqttReturnCode.RETURN_CODE_IDENTIFIER_NOT_VALID, MqttReturnCode.RETURN_CODE_BAD_USERNAME_OR_PASSWORD, MqttReturnCode.RETURN_CODE_NOT_AUTHORIZED, MqttReturnCode.RETURN_CODE_SERVER_UNAVAILABLE, MqttReturnCode.RETURN_CODE_SERVER_BUSY, MqttReturnCode.RETURN_CODE_BANNED, MqttReturnCode.RETURN_CODE_BAD_AUTHENTICATION, 144, 149, 151, 154, MqttReturnCode.RETURN_CODE_USE_ANOTHER_SERVER, MqttReturnCode.RETURN_CODE_SERVER_MOVED, 159};
    private static final Byte[] validProperties = {(byte) 17, (byte) 33, (byte) 36, (byte) 37, (byte) 39, (byte) 18, (byte) 34, (byte) 40, (byte) 41, (byte) 42, (byte) 19, (byte) 26, (byte) 28, (byte) 21, (byte) 22, (byte) 31, (byte) 38};

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

    public MqttConnAck(byte[] bArr) throws MqttException, IOException {
        super((byte) 2);
        this.properties = new MqttProperties(validProperties);
        DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(bArr));
        this.sessionPresent = (dataInputStream.readUnsignedByte() & 1) == 1;
        this.reasonCode = dataInputStream.readUnsignedByte();
        validateReturnCode(this.reasonCode, validReturnCodes);
        this.properties.decodeProperties(dataInputStream);
        dataInputStream.close();
    }

    public MqttConnAck(boolean z, int i, MqttProperties mqttProperties) throws MqttException {
        super((byte) 2);
        if (mqttProperties != null) {
            this.properties = mqttProperties;
        } else {
            this.properties = new MqttProperties();
        }
        this.properties.setValidProperties(validProperties);
        this.sessionPresent = z;
        validateReturnCode(i, validReturnCodes);
        this.reasonCode = i;
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    protected byte[] getVariableHeader() throws MqttException {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            dataOutputStream.write(this.sessionPresent ? (byte) 1 : (byte) 0);
            dataOutputStream.write((byte) this.reasonCode);
            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 String getKey() {
        return "Con";
    }

    public boolean getSessionPresent() {
        return this.sessionPresent;
    }

    public void setSessionPresent(boolean z) {
        this.sessionPresent = z;
    }

    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;
    }

    public static int[] getValidreturncodes() {
        return validReturnCodes;
    }

    @Override // org.eclipse.paho.mqttv5.common.packet.MqttWireMessage
    public String toString() {
        return "MqttConnAck [returnCode=" + this.reasonCode + ", sessionPresent=" + this.sessionPresent + ", properties=" + this.properties + "]";
    }
}
