package io.reactivex.internal.subscribers; import io.reactivex.FlowableSubscriber; import io.reactivex.disposables.Disposable; import io.reactivex.exceptions.CompositeException; import io.reactivex.exceptions.Exceptions; import io.reactivex.functions.Action; import io.reactivex.functions.Consumer; import io.reactivex.internal.functions.Functions; import io.reactivex.internal.subscriptions.SubscriptionHelper; import io.reactivex.observers.LambdaConsumerIntrospection; import io.reactivex.plugins.RxJavaPlugins; import java.util.concurrent.atomic.AtomicReference; import org.reactivestreams.Subscription; /* loaded from: classes2.dex */ public final class BoundedSubscriber extends AtomicReference implements FlowableSubscriber, Subscription, Disposable, LambdaConsumerIntrospection { private static final long serialVersionUID = -7251123623727029452L; final int bufferSize; int consumed; final int limit; final Action onComplete; final Consumer onError; final Consumer onNext; final Consumer onSubscribe; public BoundedSubscriber(Consumer consumer, Consumer consumer2, Action action, Consumer consumer3, int i) { this.onNext = consumer; this.onError = consumer2; this.onComplete = action; this.onSubscribe = consumer3; this.bufferSize = i; this.limit = i - (i >> 2); } @Override // org.reactivestreams.Subscription public void cancel() { SubscriptionHelper.cancel(this); } @Override // io.reactivex.disposables.Disposable public void dispose() { cancel(); } public boolean hasCustomOnError() { return this.onError != Functions.e; } public boolean isDisposed() { return get() == SubscriptionHelper.CANCELLED; } @Override // org.reactivestreams.Subscriber public void onComplete() { Subscription subscription = get(); SubscriptionHelper subscriptionHelper = SubscriptionHelper.CANCELLED; if (subscription != subscriptionHelper) { lazySet(subscriptionHelper); try { this.onComplete.run(); } catch (Throwable th) { Exceptions.b(th); RxJavaPlugins.b(th); } } } @Override // org.reactivestreams.Subscriber public void onError(Throwable th) { Subscription subscription = get(); SubscriptionHelper subscriptionHelper = SubscriptionHelper.CANCELLED; if (subscription == subscriptionHelper) { RxJavaPlugins.b(th); return; } lazySet(subscriptionHelper); try { this.onError.accept(th); } catch (Throwable th2) { Exceptions.b(th2); RxJavaPlugins.b(new CompositeException(th, th2)); } } @Override // org.reactivestreams.Subscriber public void onNext(T t) { if (isDisposed()) { return; } try { this.onNext.accept(t); int i = this.consumed + 1; if (i == this.limit) { this.consumed = 0; get().request(this.limit); } else { this.consumed = i; } } catch (Throwable th) { Exceptions.b(th); get().cancel(); onError(th); } } @Override // org.reactivestreams.Subscriber public void onSubscribe(Subscription subscription) { if (SubscriptionHelper.setOnce(this, subscription)) { try { this.onSubscribe.accept(this); } catch (Throwable th) { Exceptions.b(th); subscription.cancel(); onError(th); } } } @Override // org.reactivestreams.Subscription public void request(long j) { get().request(j); } }