-
Notifications
You must be signed in to change notification settings - Fork 25
Reading an InputStream directly line by line #30
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
Conversation
/cc @davidmoten |
How is it better than |
I agree with @abersnaze on this one. And I suppose an alternative is Incidentally |
@davidmoten if you got the code what are you waiting for. Submit a PR! |
@Test
public void testDownload() throws InterruptedException {
Strign uri = ...
Observable
.defer(() -> {
try {
InputStream is = getAwsInputStream(uri);
if (is != null) {
return StringObservable.byLine(StringObservable.decode(StringObservable.from(is), "UTF-8"));
}
} catch (Throwable t) {
return Observable.error(t);
}
return Observable.empty();
})
.doOnSubscribe(() -> L.warn("doOnSubscribe"))
.doOnNext((t) -> L.warn("doOnNext - " + t))
.subscribe();
} This prints out However if i use @Test
public void testDownload() throws InterruptedException {
Strign uri = ...
Observable
.defer(() -> {
try {
InputStream is = getAwsInputStream(uri);
if (is != null) {
return Observable.create(new OnSubscribeInputStreamToLines(is));
}
} catch (Throwable t) {
return Observable.error(t);
}
return Observable.empty();
})
.doOnSubscribe(() -> L.warn("doOnSubscribe"))
.doOnNext((t) -> L.warn("doOnNext - " + t))
.subscribe();
} I see my |
I just tested with the following that works. StringObservable.split(StringObservable.from(new BufferedReader(new InputStreamReader(is, Charsets.UTF_8))), "\n") So i retried removing byLine and replacing it by StringObservable.split(StringObservable.decode(StringObservable.from(is), "UTF-8"), "\n"); And that works. The problem was just with the line separator actually. So yeah, it wasn't working because
I'll close that PR. Maybe is it worth opening an issue about |
strange the public static Observable<String> byLine(Observable<String> source) {
return split(source, System.getProperty("line.separator"));
} and this code confirms System.out.println("System.getProperty(\"line.separator\") = " + Arrays.toString(System.getProperty("line.separator").getBytes()));
System.out.println("\"\\n\" = " + Arrays.toString("\n".getBytes())); outputs this
@Crystark Could you report what middle snippet of code outputs on your system? |
I'm running my tests on windows so yeah, line separator is CRLF:
IMO, the System's line separator should be ignored and |
I added an OnSubscribe to deal with reading lines while the InputStream is being written to (e.g. for a large download). I hope this is useful.