package com.bumptech.glide.load.resource.bitmap;

import androidx.media3.common.C;
import c5.k;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: loaded from: classes.dex */
public final class a extends FilterInputStream {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public volatile byte[] f5814a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public int f5815b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f5816c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public int f5817d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public int f5818e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final c5.b f5819f;

    public a(InputStream inputStream, c5.b bVar) {
        super(inputStream);
        this.f5817d = -1;
        this.f5819f = bVar;
        this.f5814a = (byte[]) ((k) bVar).c(C.DEFAULT_BUFFER_SEGMENT_SIZE, byte[].class);
    }

    public static void k() 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.f5814a == null || inputStream == null) {
            k();
            throw null;
        }
        return (this.f5815b - this.f5818e) + inputStream.available();
    }

    @Override // java.io.FilterInputStream, java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public final void close() {
        if (this.f5814a != null) {
            ((k) this.f5819f).g(this.f5814a);
            this.f5814a = null;
        }
        InputStream inputStream = ((FilterInputStream) this).in;
        ((FilterInputStream) this).in = null;
        if (inputStream != null) {
            inputStream.close();
        }
    }

    public final int e(InputStream inputStream, byte[] bArr) throws IOException {
        int i10 = this.f5817d;
        if (i10 != -1) {
            int i11 = this.f5818e - i10;
            int i12 = this.f5816c;
            if (i11 < i12) {
                if (i10 == 0 && i12 > bArr.length && this.f5815b == bArr.length) {
                    int length = bArr.length * 2;
                    if (length <= i12) {
                        i12 = length;
                    }
                    byte[] bArr2 = (byte[]) ((k) this.f5819f).c(i12, byte[].class);
                    System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
                    this.f5814a = bArr2;
                    ((k) this.f5819f).g(bArr);
                    bArr = bArr2;
                } else if (i10 > 0) {
                    System.arraycopy(bArr, i10, bArr, 0, bArr.length - i10);
                }
                int i13 = this.f5818e - this.f5817d;
                this.f5818e = i13;
                this.f5817d = 0;
                this.f5815b = 0;
                int i14 = inputStream.read(bArr, i13, bArr.length - i13);
                int i15 = this.f5818e;
                if (i14 > 0) {
                    i15 += i14;
                }
                this.f5815b = i15;
                return i14;
            }
        }
        int i16 = inputStream.read(bArr);
        if (i16 > 0) {
            this.f5817d = -1;
            this.f5818e = 0;
            this.f5815b = i16;
        }
        return i16;
    }

    public final synchronized void g() {
        if (this.f5814a != null) {
            ((k) this.f5819f).g(this.f5814a);
            this.f5814a = null;
        }
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public final synchronized void mark(int i10) {
        this.f5816c = Math.max(this.f5816c, i10);
        this.f5817d = this.f5818e;
    }

    @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.f5814a;
        InputStream inputStream = ((FilterInputStream) this).in;
        if (bArr == null || inputStream == null) {
            k();
            throw null;
        }
        if (this.f5818e >= this.f5815b && e(inputStream, bArr) == -1) {
            return -1;
        }
        if (bArr != this.f5814a && (bArr = this.f5814a) == null) {
            k();
            throw null;
        }
        int i10 = this.f5815b;
        int i11 = this.f5818e;
        if (i10 - i11 <= 0) {
            return -1;
        }
        this.f5818e = i11 + 1;
        return bArr[i11] & 255;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public final synchronized void reset() {
        if (this.f5814a == null) {
            throw new IOException("Stream is closed");
        }
        int i10 = this.f5817d;
        if (-1 == i10) {
            throw new RecyclableBufferedInputStream$InvalidMarkException("Mark has been invalidated, pos: " + this.f5818e + " markLimit: " + this.f5816c);
        }
        this.f5818e = i10;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public final synchronized long skip(long j10) {
        if (j10 < 1) {
            return 0L;
        }
        byte[] bArr = this.f5814a;
        if (bArr == null) {
            k();
            throw null;
        }
        InputStream inputStream = ((FilterInputStream) this).in;
        if (inputStream == null) {
            k();
            throw null;
        }
        int i10 = this.f5815b;
        int i11 = this.f5818e;
        if (i10 - i11 >= j10) {
            this.f5818e = (int) (((long) i11) + j10);
            return j10;
        }
        long j11 = ((long) i10) - ((long) i11);
        this.f5818e = i10;
        if (this.f5817d == -1 || j10 > this.f5816c) {
            long jSkip = inputStream.skip(j10 - j11);
            if (jSkip > 0) {
                this.f5817d = -1;
            }
            return j11 + jSkip;
        }
        if (e(inputStream, bArr) == -1) {
            return j11;
        }
        int i12 = this.f5815b;
        int i13 = this.f5818e;
        if (i12 - i13 >= j10 - j11) {
            this.f5818e = (int) ((((long) i13) + j10) - j11);
            return j10;
        }
        long j12 = (j11 + ((long) i12)) - ((long) i13);
        this.f5818e = i12;
        return j12;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public final synchronized int read(byte[] bArr, int i10, int i11) {
        int i12;
        int i13;
        byte[] bArr2 = this.f5814a;
        if (bArr2 == null) {
            k();
            throw null;
        }
        if (i11 == 0) {
            return 0;
        }
        InputStream inputStream = ((FilterInputStream) this).in;
        if (inputStream == null) {
            k();
            throw null;
        }
        int i14 = this.f5818e;
        int i15 = this.f5815b;
        if (i14 < i15) {
            int i16 = i15 - i14;
            if (i16 >= i11) {
                i16 = i11;
            }
            System.arraycopy(bArr2, i14, bArr, i10, i16);
            this.f5818e += i16;
            if (i16 == i11 || inputStream.available() == 0) {
                return i16;
            }
            i10 += i16;
            i12 = i11 - i16;
        } else {
            i12 = i11;
        }
        while (true) {
            if (this.f5817d == -1 && i12 >= bArr2.length) {
                i13 = inputStream.read(bArr, i10, i12);
                if (i13 == -1) {
                    return i12 != i11 ? i11 - i12 : -1;
                }
            } else {
                if (e(inputStream, bArr2) == -1) {
                    return i12 != i11 ? i11 - i12 : -1;
                }
                if (bArr2 != this.f5814a && (bArr2 = this.f5814a) == null) {
                    k();
                    throw null;
                }
                int i17 = this.f5815b;
                int i18 = this.f5818e;
                i13 = i17 - i18;
                if (i13 >= i12) {
                    i13 = i12;
                }
                System.arraycopy(bArr2, i18, bArr, i10, i13);
                this.f5818e += i13;
            }
            i12 -= i13;
            if (i12 == 0) {
                return i11;
            }
            if (inputStream.available() == 0) {
                return i11 - i12;
            }
            i10 += i13;
        }
    }
}
