package io.netty.handler.codec.marshalling;

import java.io.IOException;
import org.jboss.marshalling.ByteInput;

/* JADX INFO: loaded from: classes.dex */
class LimitingByteInput implements ByteInput {
    private static final TooBigObjectException EXCEPTION = new TooBigObjectException();
    private final ByteInput input;
    private final long limit;
    private long read;

    static final class TooBigObjectException extends IOException {
        private static final long serialVersionUID = 1;

        TooBigObjectException() {
        }
    }

    LimitingByteInput(ByteInput byteInput, long j2) {
        if (j2 <= 0) {
            throw new IllegalArgumentException("The limit MUST be > 0");
        }
        this.input = byteInput;
        this.limit = j2;
    }

    private int readable(int i2) {
        return (int) Math.min(i2, this.limit - this.read);
    }

    public int available() throws IOException {
        return readable(this.input.available());
    }

    public void close() throws IOException {
    }

    public int read() throws IOException {
        if (readable(1) <= 0) {
            throw EXCEPTION;
        }
        int i2 = this.input.read();
        this.read++;
        return i2;
    }

    public long skip(long j2) throws IOException {
        int i2 = readable((int) j2);
        if (i2 <= 0) {
            throw EXCEPTION;
        }
        long jSkip = this.input.skip(i2);
        this.read += jSkip;
        return jSkip;
    }

    public int read(byte[] bArr) throws IOException {
        return read(bArr, 0, bArr.length);
    }

    public int read(byte[] bArr, int i2, int i3) throws IOException {
        int i4 = readable(i3);
        if (i4 > 0) {
            int i5 = this.input.read(bArr, i2, i4);
            this.read += (long) i5;
            return i5;
        }
        throw EXCEPTION;
    }
}
