package com.mbridge.msdk.thrid.okio; import android.support.v4.media.session.PlaybackStateCompat; import csdk.gluads.Consts; import java.io.EOFException; import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.charset.Charset; /* loaded from: classes4.dex */ final class RealBufferedSink implements BufferedSink { public final Buffer buffer = new Buffer(); boolean closed; public final Sink sink; @Override // com.mbridge.msdk.thrid.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 // com.mbridge.msdk.thrid.okio.Sink public void write(Buffer buffer, long j) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.write(buffer, j); emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink write(ByteString byteString) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.write(byteString); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeUtf8(String str) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeUtf8(str); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeUtf8(String str, int i, int i2) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeUtf8(str, i, i2); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeUtf8CodePoint(int i) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeUtf8CodePoint(i); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeString(String str, Charset charset) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeString(str, charset); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeString(String str, int i, int i2, Charset charset) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeString(str, i, i2, charset); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink write(byte[] bArr) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.write(bArr); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink write(byte[] bArr, int i, int i2) throws IOException { 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) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } int write = this.buffer.write(byteBuffer); emitCompleteSegments(); return write; } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public long writeAll(Source source) throws IOException { 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 // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink write(Source source, long j) throws IOException { while (j > 0) { long read = source.read(this.buffer, j); if (read == -1) { throw new EOFException(); } j -= read; emitCompleteSegments(); } return this; } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeByte(int i) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeByte(i); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeShort(int i) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeShort(i); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeShortLe(int i) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeShortLe(i); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeInt(int i) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeInt(i); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeIntLe(int i) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeIntLe(i); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeLong(long j) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeLong(j); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeLongLe(long j) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeLongLe(j); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeDecimalLong(long j) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeDecimalLong(j); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink writeHexadecimalUnsignedLong(long j) throws IOException { if (this.closed) { throw new IllegalStateException(Consts.PLACEMENT_STATUS_CLOSED); } this.buffer.writeHexadecimalUnsignedLong(j); return emitCompleteSegments(); } @Override // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink emitCompleteSegments() throws IOException { 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 // com.mbridge.msdk.thrid.okio.BufferedSink public BufferedSink emit() throws IOException { 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 // com.mbridge.msdk.thrid.okio.BufferedSink public OutputStream outputStream() { return new OutputStream() { // from class: com.mbridge.msdk.thrid.okio.RealBufferedSink.1 @Override // java.io.OutputStream public void write(int i) throws IOException { RealBufferedSink realBufferedSink = RealBufferedSink.this; if (realBufferedSink.closed) { throw new IOException(Consts.PLACEMENT_STATUS_CLOSED); } realBufferedSink.buffer.writeByte((int) ((byte) i)); RealBufferedSink.this.emitCompleteSegments(); } @Override // java.io.OutputStream public void write(byte[] bArr, int i, int i2) throws IOException { RealBufferedSink realBufferedSink = RealBufferedSink.this; if (realBufferedSink.closed) { throw new IOException(Consts.PLACEMENT_STATUS_CLOSED); } realBufferedSink.buffer.write(bArr, i, i2); RealBufferedSink.this.emitCompleteSegments(); } @Override // java.io.OutputStream, java.io.Flushable public void flush() throws IOException { RealBufferedSink realBufferedSink = RealBufferedSink.this; if (realBufferedSink.closed) { return; } realBufferedSink.flush(); } @Override // java.io.OutputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { RealBufferedSink.this.close(); } public String toString() { return RealBufferedSink.this + ".outputStream()"; } }; } @Override // com.mbridge.msdk.thrid.okio.BufferedSink, com.mbridge.msdk.thrid.okio.Sink, java.io.Flushable public void flush() throws IOException { 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 // com.mbridge.msdk.thrid.okio.Sink, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { 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 // com.mbridge.msdk.thrid.okio.Sink public Timeout timeout() { return this.sink.timeout(); } public String toString() { return "buffer(" + this.sink + ")"; } }