package g0; import c.d.b.a.a; import d0.z.d.m; import java.nio.ByteBuffer; import okio.BufferedSink; import okio.ByteString; /* compiled from: RealBufferedSink.kt */ public final class q implements BufferedSink { public final e i = new e(); public boolean j; public final v k; public q(v vVar) { m.checkParameterIsNotNull(vVar, "sink"); this.k = vVar; } @Override // okio.BufferedSink public BufferedSink E() { if (!this.j) { long f = this.i.f(); if (f > 0) { this.k.write(this.i, f); } return this; } throw new IllegalStateException("closed".toString()); } @Override // okio.BufferedSink public BufferedSink J(String str) { m.checkParameterIsNotNull(str, "string"); if (!this.j) { this.i.b0(str); E(); return this; } throw new IllegalStateException("closed".toString()); } @Override // okio.BufferedSink public long O(x xVar) { m.checkParameterIsNotNull(xVar, "source"); long j = 0; while (true) { long h02 = ((n) xVar).h0(this.i, (long) 8192); if (h02 == -1) { return j; } j += h02; E(); } } @Override // okio.BufferedSink public BufferedSink P(long j) { if (!this.j) { this.i.P(j); return E(); } throw new IllegalStateException("closed".toString()); } @Override // g0.v, java.io.Closeable, java.lang.AutoCloseable public void close() { if (!this.j) { Throwable th = null; try { e eVar = this.i; long j = eVar.j; if (j > 0) { this.k.write(eVar, j); } } catch (Throwable th2) { th = th2; } try { this.k.close(); } catch (Throwable th3) { if (th == null) { th = th3; } } this.j = true; if (th != null) { throw th; } } } @Override // okio.BufferedSink public BufferedSink d0(ByteString byteString) { m.checkParameterIsNotNull(byteString, "byteString"); if (!this.j) { this.i.Q(byteString); E(); return this; } throw new IllegalStateException("closed".toString()); } @Override // okio.BufferedSink, g0.v, java.io.Flushable public void flush() { if (!this.j) { e eVar = this.i; long j = eVar.j; if (j > 0) { this.k.write(eVar, j); } this.k.flush(); return; } throw new IllegalStateException("closed".toString()); } @Override // okio.BufferedSink public e g() { return this.i; } @Override // java.nio.channels.Channel public boolean isOpen() { return !this.j; } @Override // okio.BufferedSink public BufferedSink o() { if (!this.j) { e eVar = this.i; long j = eVar.j; if (j > 0) { this.k.write(eVar, j); } return this; } throw new IllegalStateException("closed".toString()); } @Override // okio.BufferedSink public BufferedSink p0(long j) { if (!this.j) { this.i.p0(j); E(); return this; } throw new IllegalStateException("closed".toString()); } @Override // g0.v public y timeout() { return this.k.timeout(); } @Override // java.lang.Object public String toString() { StringBuilder P = a.P("buffer("); P.append(this.k); P.append(')'); return P.toString(); } @Override // java.nio.channels.WritableByteChannel public int write(ByteBuffer byteBuffer) { m.checkParameterIsNotNull(byteBuffer, "source"); if (!this.j) { int write = this.i.write(byteBuffer); E(); return write; } throw new IllegalStateException("closed".toString()); } @Override // okio.BufferedSink public BufferedSink write(byte[] bArr) { m.checkParameterIsNotNull(bArr, "source"); if (!this.j) { this.i.R(bArr); E(); return this; } throw new IllegalStateException("closed".toString()); } @Override // okio.BufferedSink public BufferedSink write(byte[] bArr, int i, int i2) { m.checkParameterIsNotNull(bArr, "source"); if (!this.j) { this.i.S(bArr, i, i2); E(); return this; } throw new IllegalStateException("closed".toString()); } @Override // g0.v public void write(e eVar, long j) { m.checkParameterIsNotNull(eVar, "source"); if (!this.j) { this.i.write(eVar, j); E(); return; } throw new IllegalStateException("closed".toString()); } @Override // okio.BufferedSink public BufferedSink writeByte(int i) { if (!this.j) { this.i.T(i); return E(); } throw new IllegalStateException("closed".toString()); } @Override // okio.BufferedSink public BufferedSink writeInt(int i) { if (!this.j) { this.i.W(i); return E(); } throw new IllegalStateException("closed".toString()); } @Override // okio.BufferedSink public BufferedSink writeShort(int i) { if (!this.j) { this.i.a0(i); E(); return this; } throw new IllegalStateException("closed".toString()); } }