package io.grpc.internal; import com.google.android.gms.common.internal.ServiceSpecificExtraArgs; import com.google.common.base.Preconditions; import io.grpc.Decompressor; import io.grpc.internal.ApplicationThreadDeframerListener; import io.grpc.internal.MessageDeframer; import io.grpc.internal.StreamListener; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; /* loaded from: classes6.dex */ public class ApplicationThreadDeframer implements Deframer { private final ApplicationThreadDeframerListener appListener; private final MessageDeframer deframer; private final MessageDeframer.Listener storedListener; /* loaded from: classes6.dex */ interface TransportExecutor extends ApplicationThreadDeframerListener.TransportExecutor { } /* JADX INFO: Access modifiers changed from: package-private */ public ApplicationThreadDeframer(MessageDeframer.Listener listener, TransportExecutor transportExecutor, MessageDeframer messageDeframer) { SquelchLateMessagesAvailableDeframerListener squelchLateMessagesAvailableDeframerListener = new SquelchLateMessagesAvailableDeframerListener((MessageDeframer.Listener) Preconditions.checkNotNull(listener, ServiceSpecificExtraArgs.CastExtraArgs.LISTENER)); this.storedListener = squelchLateMessagesAvailableDeframerListener; ApplicationThreadDeframerListener applicationThreadDeframerListener = new ApplicationThreadDeframerListener(squelchLateMessagesAvailableDeframerListener, transportExecutor); this.appListener = applicationThreadDeframerListener; messageDeframer.setListener(applicationThreadDeframerListener); this.deframer = messageDeframer; } @Override // io.grpc.internal.Deframer public void setMaxInboundMessageSize(int i) { this.deframer.setMaxInboundMessageSize(i); } @Override // io.grpc.internal.Deframer public void setDecompressor(Decompressor decompressor) { this.deframer.setDecompressor(decompressor); } @Override // io.grpc.internal.Deframer public void setFullStreamDecompressor(GzipInflatingBuffer gzipInflatingBuffer) { this.deframer.setFullStreamDecompressor(gzipInflatingBuffer); } @Override // io.grpc.internal.Deframer public void request(int i) { this.storedListener.messagesAvailable(new InitializingMessageProducer(new Runnable(this, i) { // from class: io.grpc.internal.ApplicationThreadDeframer.1 final ApplicationThreadDeframer this$0; final int val$numMessages; { this.this$0 = this; this.val$numMessages = i; } @Override // java.lang.Runnable public void run() { if (this.this$0.deframer.isClosed()) { return; } try { this.this$0.deframer.request(this.val$numMessages); } catch (Throwable th) { this.this$0.appListener.deframeFailed(th); this.this$0.deframer.close(); } } })); } @Override // io.grpc.internal.Deframer public void deframe(ReadableBuffer readableBuffer) { this.storedListener.messagesAvailable(new CloseableInitializingMessageProducer(this, new Runnable(this, readableBuffer) { // from class: io.grpc.internal.ApplicationThreadDeframer.2 final ApplicationThreadDeframer this$0; final ReadableBuffer val$data; { this.this$0 = this; this.val$data = readableBuffer; } @Override // java.lang.Runnable public void run() { try { this.this$0.deframer.deframe(this.val$data); } catch (Throwable th) { this.this$0.appListener.deframeFailed(th); this.this$0.deframer.close(); } } }, new Closeable(this, readableBuffer) { // from class: io.grpc.internal.ApplicationThreadDeframer.3 final ApplicationThreadDeframer this$0; final ReadableBuffer val$data; { this.this$0 = this; this.val$data = readableBuffer; } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() { this.val$data.close(); } })); } @Override // io.grpc.internal.Deframer public void closeWhenComplete() { this.storedListener.messagesAvailable(new InitializingMessageProducer(new Runnable(this) { // from class: io.grpc.internal.ApplicationThreadDeframer.4 final ApplicationThreadDeframer this$0; { this.this$0 = this; } @Override // java.lang.Runnable public void run() { this.this$0.deframer.closeWhenComplete(); } })); } @Override // io.grpc.internal.Deframer public void close() { this.deframer.stopDelivery(); this.storedListener.messagesAvailable(new InitializingMessageProducer(new Runnable(this) { // from class: io.grpc.internal.ApplicationThreadDeframer.5 final ApplicationThreadDeframer this$0; { this.this$0 = this; } @Override // java.lang.Runnable public void run() { this.this$0.deframer.close(); } })); } /* loaded from: classes6.dex */ class InitializingMessageProducer implements StreamListener.MessageProducer { private boolean initialized; private final Runnable runnable; final ApplicationThreadDeframer this$0; private InitializingMessageProducer(ApplicationThreadDeframer applicationThreadDeframer, Runnable runnable) { this.this$0 = applicationThreadDeframer; this.initialized = false; this.runnable = runnable; } private void initialize() { if (this.initialized) { return; } this.runnable.run(); this.initialized = true; } @Override // io.grpc.internal.StreamListener.MessageProducer public InputStream next() { initialize(); return this.this$0.appListener.messageReadQueuePoll(); } } /* loaded from: classes6.dex */ class CloseableInitializingMessageProducer extends InitializingMessageProducer implements Closeable { private final Closeable closeable; final ApplicationThreadDeframer this$0; /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */ public CloseableInitializingMessageProducer(ApplicationThreadDeframer applicationThreadDeframer, Runnable runnable, Closeable closeable) { super(runnable); this.this$0 = applicationThreadDeframer; this.closeable = closeable; } @Override // java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { this.closeable.close(); } } MessageDeframer.Listener getAppListener() { return this.appListener; } }