16
16
package rx .observables ;
17
17
18
18
import rx .Observable ;
19
- import rx .Observable .OnSubscribe ;
20
19
import rx .Observable .Operator ;
21
20
import rx .Subscriber ;
22
21
import rx .functions .Action1 ;
23
22
import rx .functions .Func0 ;
24
23
import rx .functions .Func1 ;
25
24
import rx .functions .Func2 ;
25
+ import rx .internal .operators .OnSubscribeInputStream ;
26
+ import rx .internal .operators .OnSubscribeReader ;
26
27
27
28
import java .io .Closeable ;
28
29
import java .io .IOException ;
42
43
43
44
public class StringObservable {
44
45
/**
45
- * Reads from the bytes from a source {@link InputStream} and outputs {@link Observable} of
46
- * {@code byte[]}s
46
+ * Reads bytes from a source {@link InputStream} and outputs {@link Observable} of
47
+ * {@code byte[]}s. Supports backpressure.
47
48
* <p>
48
49
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.from.png" alt="">
49
50
*
@@ -103,44 +104,24 @@ public void call(S resource) {
103
104
}
104
105
105
106
/**
106
- * Reads from the bytes from a source {@link InputStream} and outputs {@link Observable} of
107
- * {@code byte[]}s
107
+ * Reads bytes from a source {@link InputStream} and outputs {@link Observable} of
108
+ * {@code byte[]}s. Supports backpressure.
108
109
* <p>
109
110
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.from.png" alt="">
110
111
*
111
- * @param i
112
+ * @param is
112
113
* Source {@link InputStream}
113
114
* @param size
114
115
* internal buffer size
115
116
* @return the Observable containing read byte arrays from the input
116
117
*/
117
- public static Observable <byte []> from (final InputStream i , final int size ) {
118
- return Observable .create (new OnSubscribe <byte []>() {
119
- @ Override
120
- public void call (Subscriber <? super byte []> o ) {
121
- byte [] buffer = new byte [size ];
122
- try {
123
- if (o .isUnsubscribed ())
124
- return ;
125
- int n = i .read (buffer );
126
- while (n != -1 && !o .isUnsubscribed ()) {
127
- o .onNext (Arrays .copyOf (buffer , n ));
128
- if (!o .isUnsubscribed ())
129
- n = i .read (buffer );
130
- }
131
- } catch (IOException e ) {
132
- o .onError (e );
133
- }
134
- if (o .isUnsubscribed ())
135
- return ;
136
- o .onCompleted ();
137
- }
138
- });
118
+ public static Observable <byte []> from (final InputStream is , final int size ) {
119
+ return Observable .create (new OnSubscribeInputStream (is , size ));
139
120
}
140
121
141
122
/**
142
- * Reads from the characters from a source {@link Reader} and outputs {@link Observable} of
143
- * {@link String}s
123
+ * Reads characters from a source {@link Reader} and outputs {@link Observable} of
124
+ * {@link String}s. Supports backpressure.
144
125
* <p>
145
126
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.from.png" alt="">
146
127
*
@@ -153,8 +134,8 @@ public static Observable<String> from(final Reader i) {
153
134
}
154
135
155
136
/**
156
- * Reads from the characters from a source {@link Reader} and outputs {@link Observable} of
157
- * {@link String}s
137
+ * Reads characters from a source {@link Reader} and outputs {@link Observable} of
138
+ * {@link String}s. Supports backpressure.
158
139
* <p>
159
140
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.from.png" alt="">
160
141
*
@@ -164,33 +145,12 @@ public static Observable<String> from(final Reader i) {
164
145
* internal buffer size
165
146
* @return the Observable of Strings read from the source
166
147
*/
167
- public static Observable <String > from (final Reader i , final int size ) {
168
- return Observable .create (new OnSubscribe <String >() {
169
- @ Override
170
- public void call (Subscriber <? super String > o ) {
171
- char [] buffer = new char [size ];
172
- try {
173
- if (o .isUnsubscribed ())
174
- return ;
175
- int n = 0 ;
176
- n = i .read (buffer );
177
- while (n != -1 && !o .isUnsubscribed ()) {
178
- o .onNext (new String (buffer , 0 , n ));
179
- if (!o .isUnsubscribed ())
180
- n = i .read (buffer );
181
- }
182
- } catch (IOException e ) {
183
- o .onError (e );
184
- }
185
- if (o .isUnsubscribed ())
186
- return ;
187
- o .onCompleted ();
188
- }
189
- });
148
+ public static Observable <String > from (final Reader reader , final int size ) {
149
+ return Observable .create (new OnSubscribeReader (reader , size ));
190
150
}
191
151
192
152
/**
193
- * Decodes a stream the multibyte chunks into a stream of strings that works on infinite streams
153
+ * Decodes a stream of multibyte chunks into a stream of strings that works on infinite streams
194
154
* and where handles when a multibyte character spans two chunks.
195
155
* <p>
196
156
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.decode.png" alt="">
@@ -204,7 +164,7 @@ public static Observable<String> decode(Observable<byte[]> src, String charsetNa
204
164
}
205
165
206
166
/**
207
- * Decodes a stream the multibyte chunks into a stream of strings that works on infinite streams
167
+ * Decodes a stream of multibyte chunks into a stream of strings that works on infinite streams
208
168
* and where handles when a multibyte character spans two chunks.
209
169
* <p>
210
170
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.decode.png" alt="">
@@ -218,8 +178,8 @@ public static Observable<String> decode(Observable<byte[]> src, Charset charset)
218
178
}
219
179
220
180
/**
221
- * Decodes a stream the multibyte chunks into a stream of strings that works on infinite streams
222
- * and where it handles when a multibyte character spans two chunks.
181
+ * Decodes a stream of multibyte chunks into a stream of strings that works on infinite streams
182
+ * and handles when a multibyte character spans two chunks.
223
183
* This method allows for more control over how malformed and unmappable characters are handled.
224
184
* <p>
225
185
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.decode.png" alt="">
@@ -311,7 +271,7 @@ public boolean process(byte[] next, ByteBuffer last, boolean endOfInput) {
311
271
}
312
272
313
273
/**
314
- * Encodes a possible infinite stream of strings into a Observable of byte arrays.
274
+ * Encodes a possibly infinite stream of strings into an Observable of byte arrays.
315
275
* <p>
316
276
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.encode.png" alt="">
317
277
*
@@ -324,7 +284,7 @@ public static Observable<byte[]> encode(Observable<String> src, String charsetNa
324
284
}
325
285
326
286
/**
327
- * Encodes a possible infinite stream of strings into a Observable of byte arrays.
287
+ * Encodes a possibly infinite stream of strings into an Observable of byte arrays.
328
288
* <p>
329
289
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.encode.png" alt="">
330
290
*
@@ -337,7 +297,7 @@ public static Observable<byte[]> encode(Observable<String> src, Charset charset)
337
297
}
338
298
339
299
/**
340
- * Encodes a possible infinite stream of strings into a Observable of byte arrays.
300
+ * Encodes a possibly infinite stream of strings into an Observable of byte arrays.
341
301
* This method allows for more control over how malformed and unmappable characters are handled.
342
302
* <p>
343
303
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.encode.png" alt="">
@@ -548,7 +508,7 @@ public static Observable<String> byLine(Observable<String> source) {
548
508
}
549
509
550
510
/**
551
- * Converts an String into an Observable that emits the chars in the String.
511
+ * Converts a String into an Observable that emits the chars in the String.
552
512
* <p>
553
513
* <img width="640" height="315" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/from.png" alt="">
554
514
*
0 commit comments