-
Notifications
You must be signed in to change notification settings - Fork 25
Add backpressure support for StringObservable.from(InputStream) #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
import rx.Observable; | ||
import rx.Observable.OnSubscribe; | ||
import rx.Observable.Operator; | ||
import rx.Producer; | ||
import rx.Subscriber; | ||
import rx.functions.Action1; | ||
import rx.functions.Func0; | ||
|
@@ -38,8 +39,10 @@ | |
import java.nio.charset.CodingErrorAction; | ||
import java.util.Arrays; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.regex.Pattern; | ||
|
||
|
||
public class StringObservable { | ||
/** | ||
* Reads from the bytes from a source {@link InputStream} and outputs {@link Observable} of | ||
|
@@ -114,28 +117,106 @@ public void call(S resource) { | |
* internal buffer size | ||
* @return the Observable containing read byte arrays from the input | ||
*/ | ||
public static Observable<byte[]> from(final InputStream i, final int size) { | ||
public static Observable<byte[]> from(final InputStream is, final int size) { | ||
return Observable.create(new OnSubscribe<byte[]>() { | ||
@Override | ||
public void call(Subscriber<? super byte[]> o) { | ||
public void call(Subscriber<? super byte[]> subscriber) { | ||
subscriber.setProducer(new InputStreamProducer(is, subscriber, size)); | ||
} | ||
}); | ||
} | ||
|
||
private static class InputStreamProducer implements Producer { | ||
|
||
private final AtomicLong requested = new AtomicLong(0); | ||
private final InputStream is; | ||
private final Subscriber<? super byte[]> subscriber; | ||
private int size; | ||
|
||
InputStreamProducer(InputStream is, Subscriber<? super byte[]> subscriber, int size) { | ||
this.is = is; | ||
this.subscriber = subscriber; | ||
this.size = size; | ||
} | ||
|
||
@Override | ||
public void request(long n) { | ||
try { | ||
if (requested.get() == Long.MAX_VALUE) | ||
// already started with fast path | ||
return; | ||
else if (n == Long.MAX_VALUE) { | ||
// fast path | ||
requestAll(); | ||
} else | ||
requestSome(n); | ||
} catch (RuntimeException e) { | ||
subscriber.onError(e); | ||
} catch (IOException e) { | ||
subscriber.onError(e); | ||
} | ||
} | ||
|
||
private void requestAll() { | ||
requested.set(Long.MAX_VALUE); | ||
byte[] buffer = new byte[size]; | ||
try { | ||
if (subscriber.isUnsubscribed()) | ||
return; | ||
int n = is.read(buffer); | ||
while (n != -1 && !subscriber.isUnsubscribed()) { | ||
subscriber.onNext(Arrays.copyOf(buffer, n)); | ||
if (!subscriber.isUnsubscribed()) | ||
n = is.read(buffer); | ||
} | ||
} catch (IOException e) { | ||
subscriber.onError(e); | ||
} | ||
if (subscriber.isUnsubscribed()) | ||
return; | ||
subscriber.onCompleted(); | ||
} | ||
|
||
|
||
|
||
private void requestSome(long n) throws IOException { | ||
// back pressure path | ||
// this algorithm copied roughly from | ||
// rxjava/OnSubscribeFromIterable.java | ||
|
||
long previousCount = requested.getAndAdd(n); | ||
if (previousCount == 0) { | ||
byte[] buffer = new byte[size]; | ||
try { | ||
if (o.isUnsubscribed()) | ||
while (true) { | ||
long r = requested.get(); | ||
long numToEmit = r; | ||
|
||
//emit numToEmit | ||
|
||
if (subscriber.isUnsubscribed()) | ||
return; | ||
int n = i.read(buffer); | ||
while (n != -1 && !o.isUnsubscribed()) { | ||
o.onNext(Arrays.copyOf(buffer, n)); | ||
if (!o.isUnsubscribed()) | ||
n = i.read(buffer); | ||
int numRead; | ||
if (numToEmit>0) | ||
numRead = is.read(buffer); | ||
else | ||
numRead = 0; | ||
while (numRead != -1 && !subscriber.isUnsubscribed() && numToEmit>0) { | ||
subscriber.onNext(Arrays.copyOf(buffer, numRead)); | ||
numToEmit--; | ||
if (numToEmit >0 && !subscriber.isUnsubscribed()) | ||
numRead = is.read(buffer); | ||
} | ||
} catch (IOException e) { | ||
o.onError(e); | ||
|
||
//check if we have finished | ||
if (numRead == -1 && !subscriber.isUnsubscribed()) | ||
subscriber.onCompleted(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A |
||
else if (subscriber.isUnsubscribed()) | ||
return; | ||
else if (requested.addAndGet(-r) == 0) | ||
return; | ||
} | ||
if (o.isUnsubscribed()) | ||
return; | ||
o.onCompleted(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -176,7 +257,8 @@ public void call(Subscriber<? super String> o) { | |
n = i.read(buffer); | ||
while (n != -1 && !o.isUnsubscribed()) { | ||
o.onNext(new String(buffer, 0, n)); | ||
n = i.read(buffer); | ||
if (!o.isUnsubscribed()) | ||
n = i.read(buffer); | ||
} | ||
} catch (IOException e) { | ||
o.onError(e); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think RxJava's contract on request(n) isn't as strong as reactive-streams, therefore, request() should be thread-safe and non-racy. Here, if a concurrent request(1) and request(Long.MAX_VALUE); arrives, the one can start both cases and end up having negative request amount.