package io.grpc.okhttp; import com.google.common.base.Preconditions; import io.grpc.internal.SerializingExecutor; import io.grpc.okhttp.ExceptionHandlingFrameWriter; import java.io.IOException; import java.net.Socket; import o.C13115fRJ; import o.C13116fRL; import o.C15111ghZ; import o.C15139giB; import o.InterfaceC15168giw; /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class AsyncSink implements InterfaceC15168giw { private final SerializingExecutor serializingExecutor; private InterfaceC15168giw sink; private Socket socket; private final ExceptionHandlingFrameWriter.TransportExceptionHandler transportExceptionHandler; private final Object lock = new Object(); private final C15111ghZ buffer = new C15111ghZ(); private boolean writeEnqueued = false; private boolean flushEnqueued = false; private boolean closed = false; private AsyncSink(SerializingExecutor serializingExecutor, ExceptionHandlingFrameWriter.TransportExceptionHandler transportExceptionHandler) { this.serializingExecutor = (SerializingExecutor) Preconditions.checkNotNull(serializingExecutor, "executor"); this.transportExceptionHandler = (ExceptionHandlingFrameWriter.TransportExceptionHandler) Preconditions.checkNotNull(transportExceptionHandler, "exceptionHandler"); } /* JADX INFO: Access modifiers changed from: package-private */ public static AsyncSink sink(SerializingExecutor serializingExecutor, ExceptionHandlingFrameWriter.TransportExceptionHandler transportExceptionHandler) { return new AsyncSink(serializingExecutor, transportExceptionHandler); } /* JADX INFO: Access modifiers changed from: package-private */ public final void becomeConnected(InterfaceC15168giw interfaceC15168giw, Socket socket) { Preconditions.checkState(this.sink == null, "AsyncSink's becomeConnected should only be called once."); this.sink = (InterfaceC15168giw) Preconditions.checkNotNull(interfaceC15168giw, "sink"); this.socket = (Socket) Preconditions.checkNotNull(socket, "socket"); } @Override // o.InterfaceC15168giw public final void write(C15111ghZ c15111ghZ, long j) throws IOException { Preconditions.checkNotNull(c15111ghZ, "source"); if (this.closed) { throw new IOException("closed"); } C13115fRJ.f(); try { synchronized (this.lock) { this.buffer.write(c15111ghZ, j); if (!this.writeEnqueued && !this.flushEnqueued && this.buffer.e() > 0) { this.writeEnqueued = true; this.serializingExecutor.execute(new WriteRunnable(this) { // from class: io.grpc.okhttp.AsyncSink.1 final C13116fRL link = C13115fRJ.h(); final AsyncSink this$0; { this.this$0 = this; } @Override // io.grpc.okhttp.AsyncSink.WriteRunnable public void doRun() throws IOException { C13115fRJ.f(); C13115fRJ.e(); C15111ghZ c15111ghZ2 = new C15111ghZ(); try { synchronized (this.this$0.lock) { c15111ghZ2.write(this.this$0.buffer, this.this$0.buffer.e()); this.this$0.writeEnqueued = false; } this.this$0.sink.write(c15111ghZ2, c15111ghZ2.c); } finally { C13115fRJ.j(); } } }); } } } finally { C13115fRJ.j(); } } @Override // o.InterfaceC15168giw, java.io.Flushable public final void flush() throws IOException { if (this.closed) { throw new IOException("closed"); } C13115fRJ.f(); try { synchronized (this.lock) { if (this.flushEnqueued) { return; } this.flushEnqueued = true; this.serializingExecutor.execute(new WriteRunnable(this) { // from class: io.grpc.okhttp.AsyncSink.2 final C13116fRL link = C13115fRJ.h(); final AsyncSink this$0; { this.this$0 = this; } @Override // io.grpc.okhttp.AsyncSink.WriteRunnable public void doRun() throws IOException { C13115fRJ.f(); C13115fRJ.e(); C15111ghZ c15111ghZ = new C15111ghZ(); try { synchronized (this.this$0.lock) { c15111ghZ.write(this.this$0.buffer, this.this$0.buffer.c); this.this$0.flushEnqueued = false; } this.this$0.sink.write(c15111ghZ, c15111ghZ.c); this.this$0.sink.flush(); } finally { C13115fRJ.j(); } } }); } } finally { C13115fRJ.j(); } } @Override // o.InterfaceC15168giw public final C15139giB timeout() { return C15139giB.NONE; } @Override // o.InterfaceC15168giw, java.io.Closeable, java.lang.AutoCloseable public final void close() { if (this.closed) { return; } this.closed = true; this.serializingExecutor.execute(new Runnable(this) { // from class: io.grpc.okhttp.AsyncSink.3 final AsyncSink this$0; { this.this$0 = this; } @Override // java.lang.Runnable public void run() { this.this$0.buffer.close(); try { if (this.this$0.sink != null) { this.this$0.sink.close(); } } catch (IOException e) { this.this$0.transportExceptionHandler.onException(e); } try { if (this.this$0.socket != null) { this.this$0.socket.close(); } } catch (IOException e2) { this.this$0.transportExceptionHandler.onException(e2); } } }); } /* loaded from: classes6.dex */ abstract class WriteRunnable implements Runnable { final AsyncSink this$0; public abstract void doRun() throws IOException; private WriteRunnable(AsyncSink asyncSink) { this.this$0 = asyncSink; } @Override // java.lang.Runnable public final void run() { try { if (this.this$0.sink == null) { throw new IOException("Unable to perform write due to unavailable sink."); } doRun(); } catch (Exception e) { this.this$0.transportExceptionHandler.onException(e); } } } }