package org.eclipse.paho.mqttv5.client.wire;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketTimeoutException;
import org.eclipse.paho.mqttv5.client.internal.MqttState;
import org.eclipse.paho.mqttv5.client.logging.Logger;
import org.eclipse.paho.mqttv5.client.logging.LoggerFactory;
import org.eclipse.paho.mqttv5.common.ExceptionHelper;
import org.eclipse.paho.mqttv5.common.MqttException;
import org.eclipse.paho.mqttv5.common.packet.MqttDataTypes;
import org.eclipse.paho.mqttv5.common.packet.MqttWireMessage;

/* JADX INFO: loaded from: classes2.dex */
public class MqttInputStream extends InputStream {
    private static final String CLASS_NAME = "org.eclipse.paho.mqttv5.client.wire.MqttInputStream";
    private MqttState clientState;
    private DataInputStream in;
    private byte[] packet;
    private int packetLen;
    private Logger log = LoggerFactory.getLogger(LoggerFactory.MQTT_CLIENT_MSG_CAT, CLASS_NAME);
    private ByteArrayOutputStream bais = new ByteArrayOutputStream();
    private int remLen = -1;

    public MqttInputStream(MqttState mqttState, InputStream inputStream, String str) {
        this.clientState = mqttState;
        this.in = new DataInputStream(inputStream);
        this.log.setResourceName(str);
    }

    @Override // java.io.InputStream
    public int read() throws IOException {
        return this.in.read();
    }

    @Override // java.io.InputStream
    public int available() throws IOException {
        return this.in.available();
    }

    @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.in.close();
    }

    public MqttWireMessage readMqttWireMessage() throws MqttException, IOException {
        try {
            if (this.remLen < 0) {
                this.bais.reset();
                byte b = this.in.readByte();
                this.clientState.notifyReceivedBytes(1);
                byte b2 = (byte) ((b >>> 4) & 15);
                if (b2 < 1 || b2 > 15) {
                    throw ExceptionHelper.createMqttException(32108);
                }
                MqttWireMessage.validateReservedBits(b2, (byte) (b & 15));
                this.remLen = MqttDataTypes.readVariableByteInteger(this.in).getValue();
                this.bais.write(b);
                this.bais.write(MqttWireMessage.encodeVariableByteInteger(this.remLen));
                this.packet = new byte[this.bais.size() + this.remLen];
                if (this.clientState.getIncomingMaximumPacketSize() != null && this.bais.size() + this.remLen > this.clientState.getIncomingMaximumPacketSize().longValue()) {
                    throw ExceptionHelper.createMqttException(51001);
                }
                this.packetLen = 0;
            }
            if (this.remLen < 0) {
                return null;
            }
            readFully();
            this.remLen = -1;
            byte[] byteArray = this.bais.toByteArray();
            System.arraycopy(byteArray, 0, this.packet, 0, byteArray.length);
            MqttWireMessage mqttWireMessageCreateWireMessage = MqttWireMessage.createWireMessage(this.packet);
            this.log.fine(CLASS_NAME, "readMqttWireMessage", "530", new Object[]{mqttWireMessageCreateWireMessage});
            return mqttWireMessageCreateWireMessage;
        } catch (SocketTimeoutException unused) {
            return null;
        }
    }

    private void readFully() throws IOException {
        int size = this.bais.size();
        int i = this.packetLen;
        int i2 = size + i;
        int i3 = this.remLen - i;
        if (i3 < 0) {
            throw new IndexOutOfBoundsException();
        }
        int i4 = 0;
        while (i4 < i3) {
            try {
                int i5 = this.in.read(this.packet, i2 + i4, i3 - i4);
                if (i5 < 0) {
                    throw new EOFException();
                }
                this.clientState.notifyReceivedBytes(i5);
                i4 += i5;
            } catch (SocketTimeoutException e) {
                this.packetLen += i4;
                throw e;
            }
        }
    }
}
