Author: agudian Date: Tue Feb 3 21:17:36 2015 New Revision: 1656924 URL: http://svn.apache.org/r1656924 Log: [SUREFIRE-1137] Allow passing a Charset to be used by the StreamPumper to read from the input streams (i.e. the output streams of the forked process)
Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/StreamPumper.java Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java?rev=1656924&r1=1656923&r2=1656924&view=diff ============================================================================== --- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java (original) +++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/CommandLineUtils.java Tue Feb 3 21:17:36 2015 @@ -20,6 +20,7 @@ package org.apache.maven.shared.utils.cl */ import java.io.InputStream; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -27,12 +28,12 @@ import java.util.Map; import java.util.Properties; import java.util.StringTokenizer; -import org.apache.maven.shared.utils.Os; -import org.apache.maven.shared.utils.StringUtils; - import javax.annotation.Nonnull; import javax.annotation.Nullable; +import org.apache.maven.shared.utils.Os; +import org.apache.maven.shared.utils.StringUtils; + /** * @author <a href="mailto:tryg...@inamo.no">Trygve Laugstøl </a> * @version $Id$ @@ -135,9 +136,32 @@ public abstract class CommandLineUtils @Nullable Runnable runAfterProcessTermination ) throws CommandLineException { + return executeCommandLine( cl, systemIn, systemOut, systemErr, timeoutInSeconds, runAfterProcessTermination, + null ); + } + + /** + * @param cl The command line to execute + * @param systemIn The input to read from, must be thread safe + * @param systemOut A consumer that receives output, must be thread safe + * @param systemErr A consumer that receives system error stream output, must be thread safe + * @param timeoutInSeconds Positive integer to specify timeout, zero and negative integers for no timeout. + * @param runAfterProcessTermination Optional callback to run after the process terminated or the the timeout was + * exceeded, but before waiting on the stream feeder and pumpers to finish. + * @param streamCharset Charset to use for reading streams + * @return A return value, see {@link Process#exitValue()} + * @throws CommandLineException or CommandLineTimeOutException if time out occurs + * @noinspection ThrowableResultOfMethodCallIgnored + */ + public static int executeCommandLine( @Nonnull Commandline cl, InputStream systemIn, StreamConsumer systemOut, + StreamConsumer systemErr, int timeoutInSeconds, + @Nullable Runnable runAfterProcessTermination, + @Nullable final Charset streamCharset ) + throws CommandLineException + { final CommandLineCallable future = executeCommandLineAsCallable( cl, systemIn, systemOut, systemErr, timeoutInSeconds, - runAfterProcessTermination ); + runAfterProcessTermination, streamCharset ); return future.call(); } @@ -164,6 +188,35 @@ public abstract class CommandLineUtils @Nullable final Runnable runAfterProcessTermination ) throws CommandLineException { + return executeCommandLineAsCallable( cl, systemIn, systemOut, systemErr, timeoutInSeconds, + runAfterProcessTermination, null ); + } + + /** + * Immediately forks a process, returns a callable that will block until process is complete. + * + * @param cl The command line to execute + * @param systemIn The input to read from, must be thread safe + * @param systemOut A consumer that receives output, must be thread safe + * @param systemErr A consumer that receives system error stream output, must be thread safe + * @param timeoutInSeconds Positive integer to specify timeout, zero and negative integers for no timeout. + * @param runAfterProcessTermination Optional callback to run after the process terminated or the the timeout was + * @param streamCharset Charset to use for reading streams + * @return A CommandLineCallable that provides the process return value, see {@link Process#exitValue()}. "call" + * must be called on this to be sure the forked process has terminated, no guarantees is made about + * any internal state before after the completion of the call statements + * @throws CommandLineException or CommandLineTimeOutException if time out occurs + * @noinspection ThrowableResultOfMethodCallIgnored + */ + public static CommandLineCallable executeCommandLineAsCallable( @Nonnull final Commandline cl, + @Nullable final InputStream systemIn, + final StreamConsumer systemOut, + final StreamConsumer systemErr, + final int timeoutInSeconds, + @Nullable final Runnable runAfterProcessTermination, + @Nullable final Charset streamCharset ) + throws CommandLineException + { //noinspection ConstantConditions if ( cl == null ) { Modified: maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/StreamPumper.java URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/StreamPumper.java?rev=1656924&r1=1656923&r2=1656924&view=diff ============================================================================== --- maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/StreamPumper.java (original) +++ maven/shared/trunk/maven-shared-utils/src/main/java/org/apache/maven/shared/utils/cli/StreamPumper.java Tue Feb 3 21:17:36 2015 @@ -24,6 +24,11 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; +import java.io.Reader; +import java.nio.charset.Charset; + +import javax.annotation.Nullable; + import org.apache.maven.shared.utils.io.IOUtil; /** @@ -32,7 +37,7 @@ import org.apache.maven.shared.utils.io. * * @author <a href="mailto:fvan...@maxiq.com">Florin Vancea </a> * @author <a href="mailto:p...@thoughtworks.com">Paul Julius </a> - * + * */ public class StreamPumper extends AbstractStreamHandler @@ -49,12 +54,17 @@ public class StreamPumper public StreamPumper( InputStream in, StreamConsumer consumer ) { - this( in, null, consumer ); + this( new InputStreamReader( in ), null, consumer ); + } + + public StreamPumper( InputStream in, StreamConsumer consumer, @Nullable Charset charset ) + { + this( null == charset ? new InputStreamReader( in ) : new InputStreamReader( in, charset ), null, consumer ); } - private StreamPumper( InputStream in, PrintWriter writer, StreamConsumer consumer ) + private StreamPumper( Reader in, PrintWriter writer, StreamConsumer consumer ) { - this.in = new BufferedReader( new InputStreamReader( in ), SIZE ); + this.in = new BufferedReader( in, SIZE ); this.out = writer; this.consumer = consumer; }