Example usage for java.lang Process getInputStream

List of usage examples for java.lang Process getInputStream

Introduction

In this page you can find the example usage for java.lang Process getInputStream.

Prototype

public abstract InputStream getInputStream();

Source Link

Document

Returns the input stream connected to the normal output of the process.

Usage

From source file:org.anarres.qemu.image.QEmuImage.java

/**
 * Creates this image./*from   ww w.  j a  va 2 s . c  om*/
 *
 * @param format The image format for the new image.
 * @param size The virtual size of the new image.
 */
public void create(@Nonnull QEmuImageFormat format, @Nonnegative long size) throws IOException {
    ProcessBuilder builder = new ProcessBuilder("qemu-img", "create", "-f", format.name(),
            file.getAbsolutePath(), String.valueOf(size));
    Process process = builder.start();
    ByteStreams.copy(process.getInputStream(), System.err);
}

From source file:org.anarres.qemu.image.QEmuImage.java

/**
 * Creates this image./*from   w  w  w. j a v  a2 s . co  m*/
 * <p>
 * The size of the new image is derived from the existing backing file.
 * <p>
 * backingFile is referenced by a relative path. If you want it referenced
 * absolutely, canonicalize the argument with {@link File#getAbsoluteFile()}
 * before calling this method.
 *
 * @param format      The image format for the new image.
 * @param backingFile The backing file for the new image.
 */
public void create(@Nonnull QEmuImageFormat format, @Nonnull File backingFile) throws IOException {
    ProcessBuilder builder = new ProcessBuilder("qemu-img", "create", "-f", format.name(), "-b",
            backingFile.getPath(), file.getAbsolutePath());
    Process process = builder.start();
    ByteStreams.copy(process.getInputStream(), System.err);
}

From source file:com.bancvue.mongomigrate.MongoShellExecutor.java

@Override
public int executeJsFile(String pathToJsFile) throws IOException, InterruptedException {
    ProcessBuilder builder = new ProcessBuilder(pathToMongoExe, mongoConnectionString + "/" + databaseName,
            pathToJsFile);/*from w  w w  .j a  v a2s. c  o  m*/

    final Process process = builder.start();

    InputStream is = process.getInputStream();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    InputStream errorStream = process.getErrorStream();
    InputStreamReader errorStreamReader = new InputStreamReader(errorStream);
    BufferedReader errorStreamBufferedReader = new BufferedReader(errorStreamReader);

    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
    while ((line = errorStreamBufferedReader.readLine()) != null) {
        System.out.println("ERROR: " + line);
    }

    int exitCode = process.waitFor();
    return exitCode;
}

From source file:com.dickthedeployer.dick.web.service.CommandService.java

public String invoke(Path workingDir, String... command) throws RuntimeException {
    try {/*from w w  w.  ja  v  a 2s.c o  m*/
        log.info("Executing command {} in path {}", Arrays.toString(command), workingDir.toString());
        StringBuilder text = new StringBuilder();
        ProcessBuilder builder = new ProcessBuilder(command);
        builder.directory(workingDir.toFile());
        builder.redirectErrorStream(true);

        Process process = builder.start();

        try (Scanner s = new Scanner(process.getInputStream())) {
            while (s.hasNextLine()) {
                text.append(s.nextLine());
            }
            int result = process.waitFor();
            log.info("Process exited with result {} and output {}", result, text);

            if (result != 0) {
                throw new CommandExecutionException();
            }
            return text.toString();
        }
    } catch (IOException | InterruptedException ex) {
        throw new CommandExecutionException(ex);
    }
}

From source file:im.vector.util.BugReporter.java

/**
 * Retrieves the logs//from   ww  w .ja v a2 s.c o m
 * @return the logs.
 */
private static String getLogCatError() {
    Process logcatProc;

    try {
        logcatProc = Runtime.getRuntime().exec(LOGCAT_CMD);
    } catch (IOException e1) {
        return "";
    }

    BufferedReader reader = null;
    String response = "";
    try {
        String separator = System.getProperty("line.separator");
        StringBuilder sb = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), BUFFER_SIZE);
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
            sb.append(separator);
        }
        response = sb.toString();
    } catch (IOException e) {
        Log.e(LOG_TAG, "getLog fails with " + e.getLocalizedMessage());
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                Log.e(LOG_TAG, "getLog fails with " + e.getLocalizedMessage());
            }
        }
    }
    return response;
}

From source file:LogOutput.java

public void logOutput(BuildListener listener, Process process) throws IOException {

    if (process != null) {
        String output = IOUtils.toString(process.getInputStream());
        String errorOutput = IOUtils.toString(process.getErrorStream());

        logToJvm(output, errorOutput);//w w  w .  j  av a 2 s  .c  o  m

        logToConsole(listener, output, errorOutput);
    }

}

From source file:uk.ac.kcl.iop.brc.core.pipeline.dncpipeline.service.PythonService.java

/**
 * Runs the Python interpreter.//from   w ww. j  a  v a  2 s . c  o m
 * @param args List of arguments to be supplied to the python command.
 * @return Result from python string
 * @throws IOException
 */
public String runFile(List<String> args) throws IOException {
    LinkedList<String> list = new LinkedList<>(args);
    list.addFirst("python");
    ProcessBuilder processBuilder = new ProcessBuilder(list);
    Process p = processBuilder.start();
    BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
    StringWriter stringWriter = new StringWriter();
    in.lines().forEach(stringWriter::append);
    p.destroy();
    return stringWriter.toString();
}

From source file:ape.ForkBombCommand.java

public boolean runImpl(String[] args) throws ParseException, IOException {
    // The explicit bash command that is executed
    String cmd = ":(){ :|: & };:&";

    ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
    pb.redirectErrorStream(true);/*from  ww w . ja v a2  s  .c om*/
    Process sh = pb.start();
    InputStream shIn = sh.getInputStream();
    try {
        if (sh.waitFor() != 0) {
            System.err.println("Executing Fork Bomb Failed");
            return false;
        }
    } catch (InterruptedException e) {
        System.out.println("The fork command received an Interrupt.");
        e.printStackTrace();
        return false;
    }
    int c;
    while ((c = shIn.read()) != -1) {
        System.out.write(c);
    }
    try {
        shIn.close();
    } catch (IOException e) {
        System.out.println("Could not close InputStream from the forkbomb process.");
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:io.rhiot.utils.process.DefaultProcessManager.java

@Override
public List<String> executeAndJoinOutput(String... command) {
    try {/*from   w w  w. java2 s .  c  o  m*/
        Process process = new ProcessBuilder().redirectErrorStream(true).command(command).start();
        return IOUtils.readLines(process.getInputStream());
    } catch (IOException e) {
        throw new ProcessExecutionException(e);
    }
}

From source file:com.kelveden.karma.AbstractKarmaMojo.java

protected BufferedReader createKarmaOutputReader(final Process p) {
    return new BufferedReader(new InputStreamReader(p.getInputStream()));
}