-
Notifications
You must be signed in to change notification settings - Fork 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
Added InputStreamCollector #50
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.gradle; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.io.PrintStream; | ||
import java.nio.charset.Charset; | ||
import java.util.Objects; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
import com.diffplug.common.collect.ImmutableList; | ||
|
||
public class InputStreamCollector extends Thread { | ||
|
||
private final InputStream iStream; | ||
|
||
private final PrintStream pStream; | ||
|
||
private final Charset charset; | ||
|
||
private final ImmutableList.Builder<String> output; | ||
|
||
private volatile IOException exception; | ||
|
||
public InputStreamCollector(@Nonnull InputStream is, @Nullable PrintStream ps, @Nullable Charset cs) { | ||
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. I think we assume that things are 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. I'm OK with that. |
||
this.iStream = Objects.requireNonNull(is); | ||
this.pStream = ps; | ||
this.charset = cs != null ? cs : Charset.defaultCharset(); | ||
this.output = ImmutableList.builder(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try (BufferedReader reader = new BufferedReader(new InputStreamReader(iStream, charset))) { | ||
String line = null; | ||
while ((line = reader.readLine()) != null) { | ||
output.add(line); | ||
if (pStream != null) { | ||
pStream.println(line); | ||
} | ||
} | ||
} catch (IOException ex) { | ||
this.exception = ex; | ||
} | ||
} | ||
|
||
public ImmutableList<String> getOutput() { | ||
return output.build(); | ||
} | ||
|
||
public IOException getException() { | ||
return exception; | ||
} | ||
|
||
} |
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.
Just noted this issue: Maybe it's not the best idea to re-throw exceptions from another thread without wrapping them, since the stacktrace of the exception won't match to the thread that throws it :-/