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 MqttAuth extends MqttWireMessage {
    private MqttProperties properties;
    private static final int[] validReturnCodes = {0, 24, 25};
    private static final Byte[] validProperties = {(byte) 21, (byte) 22, (byte) 31, (byte) 38};

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

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

    public MqttAuth(int i, MqttProperties mqttProperties) throws MqttException {
        super((byte) 15);
        if (mqttProperties != null) {
            this.properties = mqttProperties;
        } else {
            this.properties = new MqttProperties();
        }
        this.properties.setValidProperties(validProperties);
        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.writeByte(this.reasonCode);
            dataOutputStream.write(this.properties.encodeProperties());
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

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

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