Skip to content
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

Merged
merged 2 commits into from
Feb 2, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 15 additions & 28 deletions src/main/java/com/diffplug/gradle/CmdLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
*/
package com.diffplug.gradle;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

Expand Down Expand Up @@ -143,42 +140,32 @@ public static Result runCmd(File directory, String cmd, boolean echoCmd, boolean
Process process = builder.start();

// wrap the process' input and output
try (
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.defaultCharset()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream(), Charset.defaultCharset()));) {

try {
if (echoCmd) {
System.out.println("cmd>" + cmd);
}

// dump the output
ImmutableList.Builder<String> output = ImmutableList.builder();
ImmutableList.Builder<String> error = ImmutableList.builder();

String line = null;
while ((line = stdInput.readLine()) != null) {
output.add(line);
if (echoOutput) {
System.out.println(line);
}
}

// dump the input
while ((line = stdError.readLine()) != null) {
error.add(line);
if (echoOutput) {
System.err.println(line);
}
}
InputStreamCollector stdInputThread = new InputStreamCollector(process.getInputStream(), echoOutput ? System.out : null, null);
stdInputThread.start();
InputStreamCollector stdErrorThread = new InputStreamCollector(process.getErrorStream(), echoOutput ? System.err : null, null);
stdErrorThread.start();

// check that the process exited correctly
int exitValue = process.waitFor();
if (exitValue != EXIT_VALUE_SUCCESS) {
// then wait for threads collecting the output of thread
stdInputThread.join();
stdErrorThread.join();

if (stdInputThread.getException() != null) {
throw stdInputThread.getException();
} else if (stdErrorThread.getException() != null) {
throw stdErrorThread.getException();
} else if (exitValue != EXIT_VALUE_SUCCESS) {
throw new RuntimeException("'" + cmd + "' exited with " + exitValue);
}

Copy link
Contributor Author

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 :-/

// returns the result of this successful execution
return new Result(directory, cmd, output.build(), error.build());
return new Result(directory, cmd, stdInputThread.getOutput(), stdErrorThread.getOutput());
} catch (InterruptedException e) {
// this isn't expected, but it's possible
throw new RuntimeException(e);
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/com/diffplug/gradle/InputStreamCollector.java
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we assume that things are @Nonnull by default, similarly to how Guava and Checker Framework approach things, so I'd vote to have this instance of @Nonnull removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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;
}

}