package org.snmp4j.asn1;

import java.io.IOException;
import java.io.InputStream;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;

/* JADX INFO: loaded from: classes.dex */
public class BERInputStream extends InputStream {
    private ByteBuffer buffer;

    public BERInputStream(ByteBuffer byteBuffer) {
        this.buffer = byteBuffer;
        byteBuffer.mark();
    }

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

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

    public int getAvailableBytes() {
        return this.buffer.limit();
    }

    public ByteBuffer getBuffer() {
        return this.buffer;
    }

    public long getPosition() {
        return this.buffer.position();
    }

    public boolean isMarked() {
        return true;
    }

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

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

    @Override // java.io.InputStream
    public int read() throws IOException {
        try {
            return this.buffer.get() & 255;
        } catch (BufferUnderflowException unused) {
            throw new IOException("Unexpected end of input stream at position " + getPosition());
        }
    }

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

    public void setBuffer(ByteBuffer byteBuffer) {
        this.buffer = byteBuffer;
    }

    @Override // java.io.InputStream
    public long skip(long j2) throws IOException {
        long jMin = Math.min(this.buffer.remaining(), j2);
        ByteBuffer byteBuffer = this.buffer;
        byteBuffer.position((int) (((long) byteBuffer.position()) + jMin));
        return jMin;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr) throws IOException {
        if (this.buffer.remaining() <= 0) {
            return -1;
        }
        int iMin = Math.min(this.buffer.remaining(), bArr.length);
        this.buffer.get(bArr, 0, iMin);
        return iMin;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i2, int i3) throws IOException {
        if (this.buffer.remaining() <= 0) {
            return -1;
        }
        int iMin = Math.min(this.buffer.remaining(), bArr.length);
        this.buffer.get(bArr, i2, i3);
        return iMin;
    }
}
