Mercurial > hg > truffle
comparison graal/com.oracle.max.graal.graphviz/src/com/oracle/max/graal/graphviz/GraphvizRunner.java @ 2874:d90bf514d647
Renamed packages.
author | Thomas Wuerthinger <thomas@wuerthinger.net> |
---|---|
date | Wed, 08 Jun 2011 08:59:54 +0200 |
parents | graal/com.oracle.max.graal.graphviz/src/com/oracle/graal/graph/vis/GraphvizRunner.java@0341b6424579 |
children |
comparison
equal
deleted
inserted
replaced
2873:810e2d253e00 | 2874:d90bf514d647 |
---|---|
1 /* | |
2 * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.max.graal.graphviz; | |
24 | |
25 import java.io.IOException; | |
26 import java.io.InputStream; | |
27 import java.io.OutputStream; | |
28 | |
29 /** | |
30 * Provides functionality to process graphs in the DOT language with a Graphviz tool and obtain the generated output. | |
31 */ | |
32 public class GraphvizRunner { | |
33 | |
34 public static final String DOT_LAYOUT = "dot"; | |
35 | |
36 /** | |
37 * Processes data from an input stream with a Graphviz tool such as {@code dot}, writing output in the specified | |
38 * format to the given output stream. The method waits for the executed tool to finish and then returns its exit | |
39 * code. | |
40 * | |
41 * @param layout | |
42 * The Graphviz layouter to use (e.g. "dot"). | |
43 * @param in | |
44 * Stream to read input from. | |
45 * @param out | |
46 * Stream to write output to. | |
47 * @param format | |
48 * Desired output format (-T parameter). | |
49 * @return Exit code of the called utility. | |
50 * @throws IOException | |
51 * When the process can not be started (e.g. Graphviz missing) or reading/writing a stream fails. | |
52 */ | |
53 public static int process(String layout, InputStream in, OutputStream out, String format) throws IOException { | |
54 byte[] buffer = new byte[4096]; | |
55 | |
56 // create and start process | |
57 ProcessBuilder pb = new ProcessBuilder("dot", "-T", format, "-K", layout); | |
58 Process p = pb.start(); | |
59 | |
60 // write data from in to stdin | |
61 OutputStream stdin = p.getOutputStream(); | |
62 transfer(buffer, in, stdin); | |
63 stdin.close(); | |
64 in.close(); | |
65 | |
66 // read output from stdout and write to out | |
67 InputStream stdout = p.getInputStream(); | |
68 transfer(buffer, stdout, out); | |
69 stdout.close(); | |
70 | |
71 // wait for process to terminate | |
72 for (;;) { | |
73 try { | |
74 return p.waitFor(); | |
75 } catch (InterruptedException e) { | |
76 // ignore | |
77 } | |
78 } | |
79 } | |
80 | |
81 /** | |
82 * Reads all data from an {@link InputStream} and writes it to an {@link OutputStream}, using the provided buffer. | |
83 */ | |
84 private static void transfer(byte[] buffer, InputStream in, OutputStream out) throws IOException { | |
85 int count; | |
86 while ((count = in.read(buffer, 0, buffer.length)) != -1) { | |
87 out.write(buffer, 0, count); | |
88 } | |
89 in.close(); | |
90 } | |
91 } |