inputFeeder = new StreamFeeder( systemIn, p.getOutputStream() ); inputFeeder.start(); inputFeeder.waitUntilDone(); inputFeeder.close(); handleException( inputFeeder, "stdin" ); inputFeeder.disable(); inputFeeder.close();
private static void handleException( final StreamFeeder streamFeeder, final String streamName ) throws CommandLineException { if ( streamFeeder.getException() != null ) { throw new CommandLineException( String.format( "Failure processing %s.", streamName ), streamFeeder.getException() ); } }
private void feed() throws IOException { boolean flush = false; int data = input.read(); while ( !isDone() && data != -1 ) { synchronized ( output ) { if ( !isDisabled() ) { output.write( data ); flush = true; } data = input.read(); } } if ( flush ) { output.flush(); } }
public static void waitForAllPumpers(Optional<StreamFeeder> stdin, StreamPumper stdout, StreamPumper stderr) throws InterruptedException { if (stdin.isPresent()) { stdin.get().waitUntilDone(); } stdout.waitUntilDone(); stderr.waitUntilDone(); }
public void startPumping() { Assert.isTrue(open, "Not open"); Assert.isFalse(pumping, "Already pumping"); Assert.noNulls(inputFeeder, outputPumper, errorPumper); if (inputFeeder.isPresent()) { inputFeeder.get().start(); } errorPumper.start(); outputPumper.start(); this.pumping = true; }
private static Optional<StreamFeeder> getStreamFeeder(Optional<InputStream> in, OutputStream out) { if (in.isPresent()) { return Optional.of(new StreamFeeder(in.get(), out)); } else { return absent(); } }
public void disableQuietly() { if (inputFeeder != null && inputFeeder.isPresent()) { inputFeeder.get().disable(); } if (errorPumper != null) { errorPumper.disable(); } if (outputPumper != null) { outputPumper.disable(); } }
public void closeQuietly() { if (inputFeeder != null && inputFeeder.isPresent()) { inputFeeder.get().close(); } if (errorPumper != null) { errorPumper.close(); } if (outputPumper != null) { outputPumper.close(); } }
public void waitUntilDone() throws InterruptedException { Assert.isTrue(open, "Not open"); Assert.isTrue(pumping, "Not pumping"); Assert.isFalse(done, "Already done"); if (inputFeeder.isPresent()) { inputFeeder.get().waitUntilDone(); } outputPumper.waitUntilDone(); errorPumper.waitUntilDone(); this.done = true; }
/** * Immediately forks a process and returns a callable that blocks until the forked process has completed */ private static CommandLineCallable executeCommandLineAsCallable(Commandline cl, ExecRequest request) throws CommandLineException { checkNotNull(cl, "cl"); checkNotNull(request, "request"); final Process process = cl.execute(); final Optional<StreamFeeder> stdin = getStreamFeeder(request.getStdin(), process.getOutputStream()); final StreamPumper stdout = StreamPumper.builder(process.getInputStream(), request.getStdout()).withBufferSize(request.getStreamBufferSize()).build(); final StreamPumper stderr = StreamPumper.builder(process.getErrorStream(), request.getStderr()).withBufferSize(request.getStreamBufferSize()).build(); if (stdin.isPresent()) { stdin.get().start(); } stdout.start(); stderr.start(); final ProcessHook processHook = new ProcessHook(process); addShutDownHook(processHook); return PlexusCLICallable.builder().withTimeoutInSeconds(request.getTimeoutInSeconds()).withStdin(stdin).withStdout(stdout).withStderr(stderr).withProcess(process) .withProcessHook(processHook).build(); }
protected Optional<StreamFeeder> getInputFeeder(CommandContext context, ChannelExec exec) throws IOException { if (context.getStdin().isPresent()) { StreamFeeder feeder = new StreamFeeder(context.getStdin().get(), exec.getOutputStream()); return Optional.of(feeder); } else { return Optional.absent(); } }
private void handleInterruption(InterruptedException e) { if (stdin.isPresent()) { stdin.get().disable(); } stdout.disable(); stderr.disable(); }