Debian Codegraph @

src/main/java/io/reactivex/rxjava3/subscribers/SafeSubscriber.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.subscribers;
15
16 import org.reactivestreams.*;
17
18 import io.reactivex.rxjava3.annotations.NonNull;
19 import io.reactivex.rxjava3.core.FlowableSubscriber;
20 import io.reactivex.rxjava3.exceptions.*;
21 import io.reactivex.rxjava3.internal.subscriptions.*;
22 import io.reactivex.rxjava3.internal.util.ExceptionHelper;
23 import io.reactivex.rxjava3.plugins.RxJavaPlugins;
24
25 /**
26 * Wraps another {@link Subscriber} and ensures all {@code onXXX} methods conform the protocol
27 * (except the requirement for serialized access).
28 *
29 * @param <T> the value type
30 */
31 public final class SafeSubscriber<@NonNull T> implements FlowableSubscriber<T>, Subscription {
32 /** The actual Subscriber. */
33 final Subscriber<? super T> downstream;
34 /** The subscription. */
35 Subscription upstream;
36 /** Indicates a terminal state. */
37 boolean done;
38
39 /**
40 * Constructs a {@code SafeSubscriber} by wrapping the given actual {@link Subscriber}.
41 * @param downstream the actual {@code Subscriber} to wrap, not {@code null} (not validated)
42 */
43 public SafeSubscriber(@NonNull Subscriber<? super T> downstream) {
44 this.downstream = downstream;
45 }
46
47 @Override
48 public void onSubscribe(@NonNull Subscription s) {
49 if (SubscriptionHelper.validate(this.upstream, s)) {
50 this.upstream = s;
51 try {
52 downstream.onSubscribe(this);
53 } catch (Throwable e) {
54 Exceptions.throwIfFatal(e);
55 done = true;
56 // can't call onError because the actual's state may be corrupt at this point
57 try {
58 s.cancel();
59 } catch (Throwable e1) {
60 Exceptions.throwIfFatal(e1);
61 RxJavaPlugins.onError(new CompositeException(e, e1));
62 return;
63 }
64 RxJavaPlugins.onError(e);
65 }
66 }
67 }
68
69 @Override
70 public void onNext(@NonNull T t) {
71 if (done) {
72 return;
73 }
74 if (upstream == null) {
75 onNextNoSubscription();
76 return;
77 }
78
79 if (t == null) {
80 Throwable ex = ExceptionHelper.createNullPointerException("onNext called with a null Throwable.");
81 try {
82 upstream.cancel();
83 } catch (Throwable e1) {
84 Exceptions.throwIfFatal(e1);
85 onError(new CompositeException(ex, e1));
86 return;
87 }
88 onError(ex);
89 return;
90 }
91
92 try {
93 downstream.onNext(t);
94 } catch (Throwable e) {
95 Exceptions.throwIfFatal(e);
96 try {
97 upstream.cancel();
98 } catch (Throwable e1) {
99 Exceptions.throwIfFatal(e1);
100 onError(new CompositeException(e, e1));
101 return;
102 }
103 onError(e);
104 }
105 }
106
107 void onNextNoSubscription() {
108 done = true;
109 Throwable ex = new NullPointerException("Subscription not set!");
110
111 try {
112 downstream.onSubscribe(EmptySubscription.INSTANCE);
113 } catch (Throwable e) {
114 Exceptions.throwIfFatal(e);
115 // can't call onError because the actual's state may be corrupt at this point
116 RxJavaPlugins.onError(new CompositeException(ex, e));
117 return;
118 }
119 try {
120 downstream.onError(ex);
121 } catch (Throwable e) {
122 Exceptions.throwIfFatal(e);
123 // if onError failed, all that's left is to report the error to plugins
124 RxJavaPlugins.onError(new CompositeException(ex, e));
125 }
126 }
127
128 @Override
129 public void onError(@NonNull Throwable t) {
130 if (done) {
131 RxJavaPlugins.onError(t);
132 return;
133 }
134 done = true;
135
136 if (upstream == null) {
137 Throwable npe = new NullPointerException("Subscription not set!");
138
139 try {
140 downstream.onSubscribe(EmptySubscription.INSTANCE);
141 } catch (Throwable e) {
142 Exceptions.throwIfFatal(e);
143 // can't call onError because the actual's state may be corrupt at this point
144 RxJavaPlugins.onError(new CompositeException(t, npe, e));
145 return;
146 }
147 try {
148 downstream.onError(new CompositeException(t, npe));
149 } catch (Throwable e) {
150 Exceptions.throwIfFatal(e);
151 // if onError failed, all that's left is to report the error to plugins
152 RxJavaPlugins.onError(new CompositeException(t, npe, e));
153 }
154 return;
155 }
156
157 if (t == null) {
158 t = ExceptionHelper.createNullPointerException("onError called with a null Throwable.");
159 }
160
161 try {
162 downstream.onError(t);
163 } catch (Throwable ex) {
164 Exceptions.throwIfFatal(ex);
165
166 RxJavaPlugins.onError(new CompositeException(t, ex));
167 }
168 }
169
170 @Override
171 public void onComplete() {
172 if (done) {
173 return;
174 }
175 done = true;
176
177 if (upstream == null) {
178 onCompleteNoSubscription();
179 return;
180 }
181
182 try {
183 downstream.onComplete();
184 } catch (Throwable e) {
185 Exceptions.throwIfFatal(e);
186 RxJavaPlugins.onError(e);
187 }
188 }
189
190 void onCompleteNoSubscription() {
191
192 Throwable ex = new NullPointerException("Subscription not set!");
193
194 try {
195 downstream.onSubscribe(EmptySubscription.INSTANCE);
196 } catch (Throwable e) {
197 Exceptions.throwIfFatal(e);
198 // can't call onError because the actual's state may be corrupt at this point
199 RxJavaPlugins.onError(new CompositeException(ex, e));
200 return;
201 }
202 try {
203 downstream.onError(ex);
204 } catch (Throwable e) {
205 Exceptions.throwIfFatal(e);
206 // if onError failed, all that's left is to report the error to plugins
207 RxJavaPlugins.onError(new CompositeException(ex, e));
208 }
209 }
210
211 @Override
212 public void request(long n) {
213 try {
214 upstream.request(n);
215 } catch (Throwable e) {
216 Exceptions.throwIfFatal(e);
217 try {
218 upstream.cancel();
219 } catch (Throwable e1) {
220 Exceptions.throwIfFatal(e1);
221 RxJavaPlugins.onError(new CompositeException(e, e1));
222 return;
223 }
224 RxJavaPlugins.onError(e);
225 }
226 }
227
228 @Override
229 public void cancel() {
230 try {
231 upstream.cancel();
232 } catch (Throwable e1) {
233 Exceptions.throwIfFatal(e1);
234 RxJavaPlugins.onError(e1);
235 }
236 }
237 }