package io.netty.buffer;

import g.a.a.a.a;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.InputStream;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public class ByteBufInputStream extends InputStream implements DataInput {
    private final ByteBuf buffer;
    private boolean closed;
    private final int endIndex;
    private final StringBuilder lineBuf;
    private final boolean releaseOnClose;
    private final int startIndex;

    public ByteBufInputStream(ByteBuf byteBuf) {
        this(byteBuf, byteBuf.readableBytes());
    }

    public ByteBufInputStream(ByteBuf byteBuf, int i2) {
        this(byteBuf, i2, false);
    }

    public ByteBufInputStream(ByteBuf byteBuf, int i2, boolean z2) {
        this.lineBuf = new StringBuilder();
        Objects.requireNonNull(byteBuf, "buffer");
        if (i2 < 0) {
            if (z2) {
                byteBuf.release();
            }
            throw new IllegalArgumentException(a.l("length: ", i2));
        }
        if (i2 > byteBuf.readableBytes()) {
            if (z2) {
                byteBuf.release();
            }
            StringBuilder sbG = a.G("Too many bytes to be read - Needs ", i2, ", maximum is ");
            sbG.append(byteBuf.readableBytes());
            throw new IndexOutOfBoundsException(sbG.toString());
        }
        this.releaseOnClose = z2;
        this.buffer = byteBuf;
        int i3 = byteBuf.readerIndex();
        this.startIndex = i3;
        this.endIndex = i3 + i2;
        byteBuf.markReaderIndex();
    }

    public ByteBufInputStream(ByteBuf byteBuf, boolean z2) {
        this(byteBuf, byteBuf.readableBytes(), z2);
    }

    private void checkAvailable(int i2) throws EOFException {
        if (i2 < 0) {
            throw new IndexOutOfBoundsException("fieldSize cannot be a negative number");
        }
        if (i2 <= available()) {
            return;
        }
        StringBuilder sbG = a.G("fieldSize is too long! Length is ", i2, ", but maximum is ");
        sbG.append(available());
        throw new EOFException(sbG.toString());
    }

    @Override // java.io.InputStream
    public int available() {
        return this.endIndex - this.buffer.readerIndex();
    }

    @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() {
        try {
            super.close();
        } finally {
            if (this.releaseOnClose && !this.closed) {
                this.closed = true;
                this.buffer.release();
            }
        }
    }

    @Override // java.io.InputStream
    public void mark(int i2) {
        this.buffer.markReaderIndex();
    }

    @Override // java.io.InputStream
    public boolean markSupported() {
        return true;
    }

    @Override // java.io.InputStream
    public int read() {
        if (this.buffer.isReadable()) {
            return this.buffer.readByte() & 255;
        }
        return -1;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i2, int i3) {
        int iAvailable = available();
        if (iAvailable == 0) {
            return -1;
        }
        int iMin = Math.min(iAvailable, i3);
        this.buffer.readBytes(bArr, i2, iMin);
        return iMin;
    }

    @Override // java.io.DataInput
    public boolean readBoolean() throws EOFException {
        checkAvailable(1);
        return read() != 0;
    }

    @Override // java.io.DataInput
    public byte readByte() throws EOFException {
        if (this.buffer.isReadable()) {
            return this.buffer.readByte();
        }
        throw new EOFException();
    }

    public int readBytes() {
        return this.buffer.readerIndex() - this.startIndex;
    }

    @Override // java.io.DataInput
    public char readChar() {
        return (char) readShort();
    }

    @Override // java.io.DataInput
    public double readDouble() {
        return Double.longBitsToDouble(readLong());
    }

    @Override // java.io.DataInput
    public float readFloat() {
        return Float.intBitsToFloat(readInt());
    }

    @Override // java.io.DataInput
    public void readFully(byte[] bArr) throws EOFException {
        readFully(bArr, 0, bArr.length);
    }

    @Override // java.io.DataInput
    public void readFully(byte[] bArr, int i2, int i3) throws EOFException {
        checkAvailable(i3);
        this.buffer.readBytes(bArr, i2, i3);
    }

    @Override // java.io.DataInput
    public int readInt() throws EOFException {
        checkAvailable(4);
        return this.buffer.readInt();
    }

    @Override // java.io.DataInput
    public String readLine() {
        this.lineBuf.setLength(0);
        while (this.buffer.isReadable()) {
            short unsignedByte = this.buffer.readUnsignedByte();
            if (unsignedByte != 10) {
                if (unsignedByte != 13) {
                    this.lineBuf.append((char) unsignedByte);
                } else if (this.buffer.isReadable()) {
                    ByteBuf byteBuf = this.buffer;
                    if (((char) byteBuf.getUnsignedByte(byteBuf.readerIndex())) == '\n') {
                        this.buffer.skipBytes(1);
                    }
                }
            }
            return this.lineBuf.toString();
        }
        if (this.lineBuf.length() > 0) {
            return this.lineBuf.toString();
        }
        return null;
    }

    @Override // java.io.DataInput
    public long readLong() throws EOFException {
        checkAvailable(8);
        return this.buffer.readLong();
    }

    @Override // java.io.DataInput
    public short readShort() throws EOFException {
        checkAvailable(2);
        return this.buffer.readShort();
    }

    @Override // java.io.DataInput
    public String readUTF() {
        return DataInputStream.readUTF(this);
    }

    @Override // java.io.DataInput
    public int readUnsignedByte() {
        return readByte() & 255;
    }

    @Override // java.io.DataInput
    public int readUnsignedShort() {
        return readShort() & 65535;
    }

    @Override // java.io.InputStream
    public void reset() {
        this.buffer.resetReaderIndex();
    }

    @Override // java.io.InputStream
    public long skip(long j2) {
        return skipBytes(j2 > 2147483647L ? Integer.MAX_VALUE : (int) j2);
    }

    @Override // java.io.DataInput
    public int skipBytes(int i2) {
        int iMin = Math.min(available(), i2);
        this.buffer.skipBytes(iMin);
        return iMin;
    }
}
