package com.bumptech.glide.load.data;

import androidx.media3.common.C;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/* JADX INFO: loaded from: classes.dex */
public final class c extends OutputStream {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final OutputStream f5611a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public byte[] f5612b;

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

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

    public c(FileOutputStream fileOutputStream, c5.b bVar) {
        this.f5611a = fileOutputStream;
        this.f5613c = bVar;
        this.f5612b = (byte[]) ((c5.k) bVar).c(C.DEFAULT_BUFFER_SEGMENT_SIZE, byte[].class);
    }

    @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable
    public final void close() throws IOException {
        OutputStream outputStream = this.f5611a;
        try {
            flush();
            outputStream.close();
            byte[] bArr = this.f5612b;
            if (bArr != null) {
                ((c5.k) this.f5613c).g(bArr);
                this.f5612b = null;
            }
        } catch (Throwable th) {
            outputStream.close();
            throw th;
        }
    }

    @Override // java.io.OutputStream, java.io.Flushable
    public final void flush() throws IOException {
        int i10 = this.f5614d;
        OutputStream outputStream = this.f5611a;
        if (i10 > 0) {
            outputStream.write(this.f5612b, 0, i10);
            this.f5614d = 0;
        }
        outputStream.flush();
    }

    @Override // java.io.OutputStream
    public final void write(int i10) throws IOException {
        byte[] bArr = this.f5612b;
        int i11 = this.f5614d;
        int i12 = i11 + 1;
        this.f5614d = i12;
        bArr[i11] = (byte) i10;
        if (i12 != bArr.length || i12 <= 0) {
            return;
        }
        this.f5611a.write(bArr, 0, i12);
        this.f5614d = 0;
    }

    @Override // java.io.OutputStream
    public final void write(byte[] bArr) throws IOException {
        write(bArr, 0, bArr.length);
    }

    @Override // java.io.OutputStream
    public final void write(byte[] bArr, int i10, int i11) throws IOException {
        int i12 = 0;
        do {
            int i13 = i11 - i12;
            int i14 = i10 + i12;
            int i15 = this.f5614d;
            OutputStream outputStream = this.f5611a;
            if (i15 == 0 && i13 >= this.f5612b.length) {
                outputStream.write(bArr, i14, i13);
                return;
            }
            int iMin = Math.min(i13, this.f5612b.length - i15);
            System.arraycopy(bArr, i14, this.f5612b, this.f5614d, iMin);
            int i16 = this.f5614d + iMin;
            this.f5614d = i16;
            i12 += iMin;
            byte[] bArr2 = this.f5612b;
            if (i16 == bArr2.length && i16 > 0) {
                outputStream.write(bArr2, 0, i16);
                this.f5614d = 0;
            }
        } while (i12 < i11);
    }
}
