package com.bumptech.glide.load.data; import com.bumptech.glide.load.engine.bitmap_recycle.ArrayPool; import java.io.IOException; import java.io.OutputStream; /* loaded from: classes.dex */ public final class BufferedOutputStream extends OutputStream { private final OutputStream a; private byte[] b; private ArrayPool c; private int d; public BufferedOutputStream(OutputStream outputStream, ArrayPool arrayPool) { this(outputStream, arrayPool, 65536); } private void a() throws IOException { int i = this.d; if (i > 0) { this.a.write(this.b, 0, i); this.d = 0; } } private void b() throws IOException { if (this.d == this.b.length) { a(); } } private void c() { byte[] bArr = this.b; if (bArr != null) { this.c.put(bArr); this.b = null; } } @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { try { flush(); this.a.close(); c(); } catch (Throwable th) { this.a.close(); throw th; } } @Override // java.io.OutputStream, java.io.Flushable public void flush() throws IOException { a(); this.a.flush(); } @Override // java.io.OutputStream public void write(int i) throws IOException { byte[] bArr = this.b; int i2 = this.d; this.d = i2 + 1; bArr[i2] = (byte) i; b(); } BufferedOutputStream(OutputStream outputStream, ArrayPool arrayPool, int i) { this.a = outputStream; this.c = arrayPool; this.b = (byte[]) arrayPool.b(i, byte[].class); } @Override // java.io.OutputStream public void write(byte[] bArr) throws IOException { write(bArr, 0, bArr.length); } @Override // java.io.OutputStream public void write(byte[] bArr, int i, int i2) throws IOException { int i3 = 0; do { int i4 = i2 - i3; int i5 = i + i3; if (this.d == 0 && i4 >= this.b.length) { this.a.write(bArr, i5, i4); return; } int min = Math.min(i4, this.b.length - this.d); System.arraycopy(bArr, i5, this.b, this.d, min); this.d += min; i3 += min; b(); } while (i3 < i2); } }