package defpackage;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: renamed from: ˌᵵᵟ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C1645 extends FilterInputStream {

    /* JADX INFO: renamed from: ˌᴵ, reason: contains not printable characters */
    public volatile byte[] f6538;

    /* JADX INFO: renamed from: ᵔᵖᵹᵫ, reason: contains not printable characters */
    public int f6539;

    /* JADX INFO: renamed from: ᵜᵪʾ, reason: contains not printable characters */
    public int f6540;

    /* JADX INFO: renamed from: ᵨᵼⁱ, reason: contains not printable characters */
    public int f6541;

    /* JADX INFO: renamed from: ᵯᵷʽ, reason: contains not printable characters */
    public int f6542;

    /* JADX INFO: renamed from: ᵶˇᵱ, reason: contains not printable characters */
    public final C5540 f6543;

    public C1645(InputStream inputStream, C5540 c5540) {
        super(inputStream);
        this.f6541 = -1;
        this.f6543 = c5540;
        this.f6538 = (byte[]) c5540.m11166(65536, byte[].class);
    }

    /* JADX INFO: renamed from: ᵟᵹᵙ, reason: contains not printable characters */
    public static void m4539() throws IOException {
        throw new IOException("BufferedInputStream is closed");
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public final synchronized int available() {
        InputStream inputStream;
        inputStream = ((FilterInputStream) this).in;
        if (this.f6538 == null || inputStream == null) {
            m4539();
            throw null;
        }
        return (this.f6540 - this.f6542) + inputStream.available();
    }

    @Override // java.io.FilterInputStream, java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        if (this.f6538 != null) {
            this.f6543.m11170(this.f6538);
            this.f6538 = null;
        }
        InputStream inputStream = ((FilterInputStream) this).in;
        ((FilterInputStream) this).in = null;
        if (inputStream != null) {
            inputStream.close();
        }
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public final synchronized void mark(int i) {
        this.f6539 = Math.max(this.f6539, i);
        this.f6541 = this.f6542;
    }

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

    @Override // java.io.FilterInputStream, java.io.InputStream
    public final synchronized int read() {
        byte[] bArr = this.f6538;
        InputStream inputStream = ((FilterInputStream) this).in;
        if (bArr == null || inputStream == null) {
            m4539();
            throw null;
        }
        if (this.f6542 >= this.f6540 && m4541(inputStream, bArr) == -1) {
            return -1;
        }
        if (bArr != this.f6538 && (bArr = this.f6538) == null) {
            m4539();
            throw null;
        }
        int i = this.f6540;
        int i2 = this.f6542;
        if (i - i2 <= 0) {
            return -1;
        }
        this.f6542 = i2 + 1;
        return bArr[i2] & 255;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public final synchronized int read(byte[] bArr, int i, int i2) {
        int i3;
        int i4;
        byte[] bArr2 = this.f6538;
        if (bArr2 == null) {
            m4539();
            throw null;
        }
        if (i2 == 0) {
            return 0;
        }
        InputStream inputStream = ((FilterInputStream) this).in;
        if (inputStream == null) {
            m4539();
            throw null;
        }
        int i5 = this.f6542;
        int i6 = this.f6540;
        if (i5 < i6) {
            int i7 = i6 - i5;
            if (i7 >= i2) {
                i7 = i2;
            }
            System.arraycopy(bArr2, i5, bArr, i, i7);
            this.f6542 += i7;
            if (i7 == i2 || inputStream.available() == 0) {
                return i7;
            }
            i += i7;
            i3 = i2 - i7;
        } else {
            i3 = i2;
        }
        while (true) {
            if (this.f6541 == -1 && i3 >= bArr2.length) {
                i4 = inputStream.read(bArr, i, i3);
                if (i4 == -1) {
                    return i3 != i2 ? i2 - i3 : -1;
                }
            } else {
                if (m4541(inputStream, bArr2) == -1) {
                    return i3 != i2 ? i2 - i3 : -1;
                }
                if (bArr2 != this.f6538 && (bArr2 = this.f6538) == null) {
                    m4539();
                    throw null;
                }
                int i8 = this.f6540;
                int i9 = this.f6542;
                i4 = i8 - i9;
                if (i4 >= i3) {
                    i4 = i3;
                }
                System.arraycopy(bArr2, i9, bArr, i, i4);
                this.f6542 += i4;
            }
            i3 -= i4;
            if (i3 == 0) {
                return i2;
            }
            if (inputStream.available() == 0) {
                return i2 - i3;
            }
            i += i4;
        }
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public final synchronized void reset() {
        if (this.f6538 == null) {
            throw new IOException("Stream is closed");
        }
        int i = this.f6541;
        if (-1 == i) {
            throw new C2778("Mark has been invalidated, pos: " + this.f6542 + " markLimit: " + this.f6539);
        }
        this.f6542 = i;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public final synchronized long skip(long j) {
        if (j < 1) {
            return 0L;
        }
        byte[] bArr = this.f6538;
        if (bArr == null) {
            m4539();
            throw null;
        }
        InputStream inputStream = ((FilterInputStream) this).in;
        if (inputStream == null) {
            m4539();
            throw null;
        }
        int i = this.f6540;
        int i2 = this.f6542;
        if (i - i2 >= j) {
            this.f6542 = (int) (((long) i2) + j);
            return j;
        }
        long j2 = ((long) i) - ((long) i2);
        this.f6542 = i;
        if (this.f6541 == -1 || j > this.f6539) {
            long jSkip = inputStream.skip(j - j2);
            if (jSkip > 0) {
                this.f6541 = -1;
            }
            return j2 + jSkip;
        }
        if (m4541(inputStream, bArr) == -1) {
            return j2;
        }
        int i3 = this.f6540;
        int i4 = this.f6542;
        if (i3 - i4 >= j - j2) {
            this.f6542 = (int) ((((long) i4) + j) - j2);
            return j;
        }
        long j3 = (j2 + ((long) i3)) - ((long) i4);
        this.f6542 = i3;
        return j3;
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public final synchronized void m4540() {
        if (this.f6538 != null) {
            this.f6543.m11170(this.f6538);
            this.f6538 = null;
        }
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final int m4541(InputStream inputStream, byte[] bArr) throws IOException {
        int i = this.f6541;
        if (i != -1) {
            int i2 = this.f6542 - i;
            int i3 = this.f6539;
            if (i2 < i3) {
                if (i == 0 && i3 > bArr.length && this.f6540 == bArr.length) {
                    int length = bArr.length * 2;
                    if (length <= i3) {
                        i3 = length;
                    }
                    byte[] bArr2 = (byte[]) this.f6543.m11166(i3, byte[].class);
                    System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
                    this.f6538 = bArr2;
                    this.f6543.m11170(bArr);
                    bArr = bArr2;
                } else if (i > 0) {
                    System.arraycopy(bArr, i, bArr, 0, bArr.length - i);
                }
                int i4 = this.f6542 - this.f6541;
                this.f6542 = i4;
                this.f6541 = 0;
                this.f6540 = 0;
                int i5 = inputStream.read(bArr, i4, bArr.length - i4);
                int i6 = this.f6542;
                if (i5 > 0) {
                    i6 += i5;
                }
                this.f6540 = i6;
                return i5;
            }
        }
        int i7 = inputStream.read(bArr);
        if (i7 > 0) {
            this.f6541 = -1;
            this.f6542 = 0;
            this.f6540 = i7;
        }
        return i7;
    }
}
