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

/* JADX INFO: loaded from: classes2.dex */
public abstract class MqttWireMessage {
    protected static final String DEFAULT_PROTOCOL_NAME = "MQTT";
    protected static final int DEFAULT_PROTOCOL_VERSION = 5;
    public static final byte MESSAGE_TYPE_AUTH = 15;
    public static final byte MESSAGE_TYPE_CONNACK = 2;
    public static final byte MESSAGE_TYPE_CONNECT = 1;
    public static final byte MESSAGE_TYPE_DISCONNECT = 14;
    public static final byte MESSAGE_TYPE_PINGREQ = 12;
    public static final byte MESSAGE_TYPE_PINGRESP = 13;
    public static final byte MESSAGE_TYPE_PUBACK = 4;
    public static final byte MESSAGE_TYPE_PUBCOMP = 7;
    public static final byte MESSAGE_TYPE_PUBLISH = 3;
    public static final byte MESSAGE_TYPE_PUBREC = 5;
    public static final byte MESSAGE_TYPE_PUBREL = 6;
    public static final byte MESSAGE_TYPE_RESERVED = 0;
    public static final byte MESSAGE_TYPE_SUBACK = 9;
    public static final byte MESSAGE_TYPE_SUBSCRIBE = 8;
    public static final byte MESSAGE_TYPE_UNSUBACK = 11;
    public static final byte MESSAGE_TYPE_UNSUBSCRIBE = 10;
    private static final String[] PACKET_NAMES = {"reserved", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL", "PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK", "PINGREQ", "PINGRESP", "DISCONNECT", "AUTH"};
    private static final byte[] PACKET_RESERVED_MASKS;
    protected static final String STRING_ENCODING = "UTF-8";
    private byte type;
    MqttProperties properties = new MqttProperties();
    protected int[] reasonCodes = null;
    protected int reasonCode = -1;
    protected boolean duplicate = false;
    protected int msgId = 0;

    protected abstract byte getMessageInfo();

    protected abstract byte[] getVariableHeader() throws MqttException;

    public boolean isMessageIdRequired() {
        return true;
    }

    public boolean isRetryable() {
        return false;
    }

    static {
        byte[] bArr = new byte[16];
        bArr[6] = 2;
        bArr[8] = 2;
        bArr[10] = 2;
        PACKET_RESERVED_MASKS = bArr;
    }

    public MqttWireMessage(byte b) {
        this.type = b;
    }

    public byte[] getPayload() throws MqttException {
        return new byte[0];
    }

    public byte getType() {
        return this.type;
    }

    public int getMessageId() {
        return this.msgId;
    }

    public void setMessageId(int i) {
        this.msgId = i;
    }

    public String getKey() {
        return Integer.toString(getMessageId());
    }

    public byte[] getHeader() throws MqttException {
        try {
            int type = ((getType() & 15) << 4) ^ (getMessageInfo() & 15);
            byte[] variableHeader = getVariableHeader();
            int length = variableHeader.length + getPayload().length;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            dataOutputStream.writeByte(type);
            dataOutputStream.write(encodeVariableByteInteger(length));
            dataOutputStream.write(variableHeader);
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    public static MqttWireMessage createWireMessage(MqttPersistable mqttPersistable) throws MqttException {
        byte[] payloadBytes = mqttPersistable.getPayloadBytes();
        if (payloadBytes == null) {
            payloadBytes = new byte[0];
        }
        return createWireMessage(new MultiByteArrayInputStream(mqttPersistable.getHeaderBytes(), mqttPersistable.getHeaderOffset(), mqttPersistable.getHeaderLength(), payloadBytes, mqttPersistable.getPayloadOffset(), mqttPersistable.getPayloadLength()));
    }

    public static MqttWireMessage createWireMessage(byte[] bArr) throws MqttException {
        return createWireMessage(new ByteArrayInputStream(bArr));
    }

    private static MqttWireMessage createWireMessage(InputStream inputStream) throws MqttException {
        try {
            CountingInputStream countingInputStream = new CountingInputStream(inputStream);
            DataInputStream dataInputStream = new DataInputStream(countingInputStream);
            int unsignedByte = dataInputStream.readUnsignedByte();
            byte b = (byte) (unsignedByte >> 4);
            byte b2 = (byte) (unsignedByte & 15);
            long counter = (((long) countingInputStream.getCounter()) + ((long) MqttDataTypes.readVariableByteInteger(dataInputStream).getValue())) - ((long) countingInputStream.getCounter());
            byte[] bArr = new byte[0];
            if (counter > 0) {
                int i = (int) counter;
                byte[] bArr2 = new byte[i];
                dataInputStream.readFully(bArr2, 0, i);
                bArr = bArr2;
            }
            switch (b) {
                case 1:
                    return new MqttConnect(b2, bArr);
                case 2:
                    return new MqttConnAck(bArr);
                case 3:
                    return new MqttPublish(b2, bArr);
                case 4:
                    return new MqttPubAck(bArr);
                case 5:
                    return new MqttPubRec(bArr);
                case 6:
                    return new MqttPubRel(bArr);
                case 7:
                    return new MqttPubComp(bArr);
                case 8:
                    return new MqttSubscribe(bArr);
                case 9:
                    return new MqttSubAck(bArr);
                case 10:
                    return new MqttUnsubscribe(bArr);
                case 11:
                    return new MqttUnsubAck(bArr);
                case 12:
                    return new MqttPingReq();
                case 13:
                    return new MqttPingResp();
                case 14:
                    return new MqttDisconnect(bArr);
                case 15:
                    return new MqttAuth(bArr);
                default:
                    throw ExceptionHelper.createMqttException(MqttException.REASON_CODE_MALFORMED_PACKET);
            }
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    public static byte[] encodeVariableByteInteger(int i) {
        long j = i;
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int i2 = 0;
        do {
            byte b = (byte) (j % 128);
            j /= 128;
            if (j > 0) {
                b = (byte) (b | 128);
            }
            byteArrayOutputStream.write(b);
            i2++;
            if (j <= 0) {
                break;
            }
        } while (i2 < 4);
        return byteArrayOutputStream.toByteArray();
    }

    public static void validateReservedBits(byte b, byte b2) throws MqttException, IllegalArgumentException {
        if (b == 3) {
            return;
        }
        if (b > 15) {
            throw new IllegalArgumentException("Unrecognised Message Type.");
        }
        if (b2 != PACKET_RESERVED_MASKS[b]) {
            throw new MqttException(MqttException.REASON_CODE_MALFORMED_PACKET);
        }
    }

    protected byte[] encodeMessageId() throws MqttException {
        try {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
            dataOutputStream.writeShort(this.msgId);
            dataOutputStream.flush();
            return byteArrayOutputStream.toByteArray();
        } catch (IOException e) {
            throw new MqttException(e);
        }
    }

    public void setDuplicate(boolean z) {
        this.duplicate = z;
    }

    public boolean isDuplicate() {
        return this.duplicate;
    }

    public MqttProperties getProperties() {
        return this.properties;
    }

    public void setProperties(MqttProperties mqttProperties) {
        this.properties = mqttProperties;
    }

    public String toString() {
        return PACKET_NAMES[this.type];
    }

    protected void validateReturnCode(int i, int[] iArr) throws MqttException {
        for (int i2 : iArr) {
            if (i == i2) {
                return;
            }
        }
        throw new MqttException(MqttException.REASON_CODE_INVALID_RETURN_CODE);
    }

    public int[] getReasonCodes() {
        int[] iArr = this.reasonCodes;
        if (iArr != null) {
            return iArr;
        }
        int i = this.reasonCode;
        if (i != -1) {
            return new int[]{i};
        }
        return null;
    }

    public byte[] serialize() throws MqttException {
        byte[] header = getHeader();
        byte[] payload = getPayload();
        byte[] bArr = new byte[header.length + payload.length];
        System.arraycopy(header, 0, bArr, 0, header.length);
        System.arraycopy(payload, 0, bArr, header.length, payload.length);
        return bArr;
    }
}
