Example usage for java.lang Process getOutputStream

List of usage examples for java.lang Process getOutputStream

Introduction

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

Prototype

public abstract OutputStream getOutputStream();

Source Link

Document

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

Usage

From source file:me.tfeng.toolbox.dust.NodeEngine.java

private boolean isRegistered(Process process, String template) throws IOException {
    PrintWriter writer = new PrintWriter(process.getOutputStream());
    writer.println("[");
    printIsRegisteredExpression(writer, template);
    writer.println("]");
    writer.println("1");
    writer.flush();/*from   ww  w  . ja  v a 2  s . com*/

    String result = readInput(process, "1");
    JsonParser parser = new JsonFactory().createParser(result).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES,
            true);
    ArrayNode arrayNode = OBJECT_MAPPER.readValue(parser, ArrayNode.class);
    return arrayNode.get(0).booleanValue();
}

From source file:com.thoughtworks.go.util.ProcessWrapper.java

private void close(Process p) {
    try {/*from www .j  a  v a 2  s  .c o m*/
        IOUtils.closeQuietly(p.getInputStream());
        IOUtils.closeQuietly(p.getOutputStream());
        IOUtils.closeQuietly(p.getErrorStream());
    } finally {
        if (p != null) {
            p.destroy();
        }
    }
}

From source file:net.sourceforge.vulcan.dotnet.MSBuildTool.java

@Override
protected void preparePipes(final Process process) throws IOException {
    process.getOutputStream().close();
    process.getErrorStream().close();/* ww  w . java2  s  . c o  m*/

    if (logFile == null) {
        process.getInputStream().close();
        return;
    }

    final OutputStream logOutputStream = new FileOutputStream(logFile);

    logThread = new Thread(".NET Build Logger [" + projectName + "]") {
        @Override
        public void run() {
            final InputStream inputStream = process.getInputStream();
            try {
                try {
                    IOUtils.copy(inputStream, logOutputStream);
                } finally {
                    try {
                        logOutputStream.close();
                    } finally {
                        inputStream.close();
                    }
                }
            } catch (IOException e) {
                LOG.error("IOException capturing maven build log", e);
            }
        }
    };

    logThread.start();
}

From source file:org.openme.openme.java

public static String[] openme_run_program(String cmd, String[] env, String path) {
    /*//from   www  .j a va2 s  . c  om
       FGG: TBD - call local cM
            
       Input:  cmd  - command line to run
       env  - list of environment variables
       path - directory where to start program
            
       Output: string list:
         [0] - error text
         [1] - command output
    */

    File dir = null;
    if (path != null)
        dir = new File(path); //getCacheDir();

    Process p = null;
    String output = "";
    String err = "";

    try {
        p = Runtime.getRuntime().exec(cmd, env, dir);

        BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = null;
        while ((line = reader.readLine()) != null)
            output += line + '\n';

        reader.close();
        p.waitFor();
    } catch (Exception e) {
        err = e.toString();
    }

    if (p != null) {
        try {
            p.getOutputStream().close();
            p.getInputStream().close();
            p.getErrorStream().close();
        } catch (IOException e) {
            err = e.toString();
        }
    }

    return new String[] { err, output };
}

From source file:me.tfeng.toolbox.dust.NodeEngine.java

private JsonNode evaluate(Process process, String template, String data) throws IOException {
    PrintWriter writer = new PrintWriter(process.getOutputStream());
    writer.println("[");
    printRenderScript(writer, template, data);
    writer.println("]");
    writer.println("1");
    writer.flush();/*  w  w  w  . j  a  va  2s.  c  o m*/

    String result = readInput(process, "1");
    JsonParser parser = new JsonFactory().createParser(result).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES,
            true);
    ArrayNode arrayNode = OBJECT_MAPPER.readValue(parser, ArrayNode.class);
    return arrayNode.get(0);
}

From source file:net.wastl.rdfdot.render.GraphvizSerializerCommand.java

private void render(String data, String filename) {
    log.info("rendering graph using external 'dot' command ...");

    long start = System.currentTimeMillis();

    Process p;
    try {/*from w  w  w.j ava2 s.  c  o m*/
        p = Runtime.getRuntime().exec(String.format("dot -Tpng -o%s", filename));

        try (PrintWriter out = new PrintWriter(new OutputStreamWriter(p.getOutputStream()))) {
            out.print(data);
        }

        p.waitFor();

        log.info("finished ({}ms)!", System.currentTimeMillis() - start);
    } catch (IOException e) {
        log.error("could not execute dot command: {}", e.getMessage());
    } catch (InterruptedException e) {
        log.error("command execution interrupted");
    }

}

From source file:cognition.common.service.DocumentConversionService.java

private File makeTiffFromPDF(DNCWorkCoordinate coordinate, File input) throws IOException, TikaException {
    File output = File.createTempFile(coordinate.getFileName(), ".tiff");
    String[] cmd = { getImageMagickProg(), "-density", "300", input.getPath(), "-depth", "8", "-quality", "1",
            output.getPath() };//from   ww w .  ja v  a 2 s  .co m
    Process process = new ProcessBuilder(cmd).start();
    IOUtils.closeQuietly(process.getOutputStream());
    InputStream processInputStream = process.getInputStream();
    logStream(processInputStream);
    FutureTask<Integer> waitTask = new FutureTask<>(process::waitFor);
    Thread waitThread = new Thread(waitTask);
    waitThread.start();
    try {
        waitTask.get(240, TimeUnit.SECONDS);
        return output;
    } catch (Exception e) {
        logger.error(e.getMessage());
        waitThread.interrupt();
        process.destroy();
        waitTask.cancel(true);
    } finally {
        IOUtils.closeQuietly(processInputStream);
        process.destroy();
        waitThread.interrupt();
        waitTask.cancel(true);
    }
    return null;
}

From source file:edu.ucsd.sbrg.escher.EscherConverter.java

/**
 * Extracts CoBRA from {@link SBMLDocument} if it is FBC compliant. cobrapy must be present for
 * this.// w w w  . j  av a2 s . c  om
 *
 * @param file Input file.
 * @return Result of extraction.
 * @throws IOException Thrown if there are problems in reading the {@code input} file(s).
 * @throws XMLStreamException Thrown if there are problems in parsing XML file(s).
 */
public static boolean extractCobraModel(File file) throws IOException, XMLStreamException {
    if (false) {
        logger.warning(format(bundle.getString("SBMLFBCNotAvailable"), file.getName()));
        return false;
    } else {
        logger.info(format(bundle.getString("SBMLFBCInit"), file.getName()));
        // Execute: py3 -c "from cobra import io;
        // io.save_json_model(model=io.read_sbml_model('FILENAME'), file_name='FILENAME')"

        String[] command;
        command = new String[] { "python3", "-c",
                "\"print('yo');from cobra import io;" + "io.save_json_model(model=io.read_sbml_model('"
                        + file.getAbsolutePath() + "'), file_name='" + file.getAbsolutePath() + ".json"
                        + "');print('yo')\"",
                "> /temp/log" };
        // command = new String[] {"/usr/local/bin/python3", "-c", "\"print('yo')\""};
        command = new String[] { "python3" };
        Process p;
        try {
            // p = new ProcessBuilder(command).redirectErrorStream(true).start();
            p = Runtime.getRuntime().exec(command);
            p.waitFor();
            if (p.exitValue() == 0) {
                logger.info(format(bundle.getString("SBMLFBCExtractionSuccessful"), file.getAbsolutePath(),
                        file.getAbsolutePath()));
                InputStream is = p.getErrorStream();
                is = p.getInputStream();
                OutputStream os = p.getOutputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String cobrapy_output = "";
                cobrapy_output = reader.readLine();
                while (cobrapy_output != null) {
                    logger.warning(cobrapy_output);
                    cobrapy_output = reader.readLine();
                }
                return true;
            } else {
                logger.info(format(bundle.getString("SBMLFBCExtractionFailed")));
                BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String cobrapy_output = "";
                cobrapy_output = reader.readLine();
                while (cobrapy_output != null) {
                    logger.warning(cobrapy_output);
                    cobrapy_output = reader.readLine();
                }
                return false;
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            return false;
        }
    }
}

From source file:com.theaigames.engine.io.IOPlayer.java

public IOPlayer(Process process, int index) {

    if (index == 0) {
        this.inputStream = new OutputStreamWriter(process.getOutputStream());
    } else {// www.ja  va  2s . co  m
        this.inputStream = new OutputStreamWriter(new TeeOutputStream(process.getOutputStream(), System.out));
    }
    this.outputGobbler = new InputStreamGobbler(process.getInputStream(), this, "output", index == 1);
    this.errorGobbler = new InputStreamGobbler(process.getErrorStream(), this, "error", index == 1);
    this.process = process;
    this.dump = new StringBuilder();
    this.errorCounter = 0;
    this.finished = false;
    this.playerIndex = index;
}

From source file:io.fabric8.kit.common.ExternalCommand.java

public void execute(String processInput) throws IOException {
    final Process process = startProcess();
    start();//from   w  ww. j  a  va  2  s  .c  om
    try {
        inputStreamPump(process.getOutputStream(), processInput);

        Future<IOException> stderrFuture = startStreamPump(process.getErrorStream());
        outputStreamPump(process.getInputStream());

        stopStreamPump(stderrFuture);
        checkProcessExit(process);
    } catch (IOException e) {
        process.destroy();
        throw e;
    } finally {
        end();
    }
    if (statusCode != 0) {
        throw new IOException(
                String.format("Process '%s' exited with status %d", getCommandAsString(), statusCode));
    }

}