package okio; import android.support.v4.media.session.PlaybackStateCompat; import csdk.gluads.Consts; import java.nio.ByteBuffer; /* loaded from: classes5.dex */ public final class RealBufferedSink implements BufferedSink { public final Buffer buffer = new Buffer(); public boolean closed; public final Sink sink; @Override // okio.BufferedSink public Buffer buffer() { return this.buffer; } @Override // java.nio.channels.Channel public boolean isOpen() { return !this.closed; } public RealBufferedSink(Sink sink) { if (sink == null) { throw new NullPointerException("sink == null"); } this.sink = sink; } @Override // okio.Sink public void write(Buffer buffer, long j) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.write(buffer, j); emitCompleteSegments(); } @Override // okio.BufferedSink public BufferedSink write(ByteString byteString) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.write(byteString); return emitCompleteSegments(); } @Override // okio.BufferedSink public BufferedSink writeUtf8(String str) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeUtf8(str); return emitCompleteSegments(); } @Override // okio.BufferedSink public BufferedSink write(byte[] bArr) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.write(bArr); return emitCompleteSegments(); } @Override // okio.BufferedSink public BufferedSink write(byte[] bArr, int i, int i2) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.write(bArr, i, i2); return emitCompleteSegments(); } @Override // java.nio.channels.WritableByteChannel public int write(ByteBuffer byteBuffer) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } int write = this.buffer.write(byteBuffer); emitCompleteSegments(); return write; } @Override // okio.BufferedSink public long writeAll(Source source) { if (source == null) { throw new IllegalArgumentException("source == null"); } long j = 0; while (true) { long read = source.read(this.buffer, PlaybackStateCompat.ACTION_PLAY_FROM_URI); if (read == -1) { return j; } j += read; emitCompleteSegments(); } } @Override // okio.BufferedSink public BufferedSink writeByte(int i) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeByte(i); return emitCompleteSegments(); } @Override // okio.BufferedSink public BufferedSink writeShort(int i) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeShort(i); return emitCompleteSegments(); } @Override // okio.BufferedSink public BufferedSink writeInt(int i) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeInt(i); return emitCompleteSegments(); } @Override // okio.BufferedSink public BufferedSink writeIntLe(int i) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeIntLe(i); return emitCompleteSegments(); } @Override // okio.BufferedSink public BufferedSink writeDecimalLong(long j) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeDecimalLong(j); return emitCompleteSegments(); } @Override // okio.BufferedSink public BufferedSink writeHexadecimalUnsignedLong(long j) { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeHexadecimalUnsignedLong(j); return emitCompleteSegments(); } @Override // okio.BufferedSink public BufferedSink emitCompleteSegments() { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } long completeSegmentByteCount = this.buffer.completeSegmentByteCount(); if (completeSegmentByteCount > 0) { this.sink.write(this.buffer, completeSegmentByteCount); } return this; } @Override // okio.BufferedSink public BufferedSink emit() { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } long size = this.buffer.size(); if (size > 0) { this.sink.write(this.buffer, size); } return this; } @Override // okio.BufferedSink, okio.Sink, java.io.Flushable public void flush() { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } Buffer buffer = this.buffer; long j = buffer.size; if (j > 0) { this.sink.write(buffer, j); } this.sink.flush(); } @Override // okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() { if (this.closed) { return; } try { Buffer buffer = this.buffer; long j = buffer.size; if (j > 0) { this.sink.write(buffer, j); } th = null; } catch (Throwable th) { th = th; } try { this.sink.close(); } catch (Throwable th2) { if (th == null) { th = th2; } } this.closed = true; if (th != null) { Util.sneakyRethrow(th); } } @Override // okio.Sink public Timeout timeout() { return this.sink.timeout(); } public String toString() { return "buffer(" + this.sink + ")"; } }