diff --git a/src/com/github/jabbalaci/graphviz/GraphViz.java b/src/com/github/jabbalaci/graphviz/GraphViz.java index 5e3893b..aa2e017 100644 --- a/src/com/github/jabbalaci/graphviz/GraphViz.java +++ b/src/com/github/jabbalaci/graphviz/GraphViz.java @@ -25,13 +25,11 @@ ****************************************************************************** */ -import java.io.BufferedReader; -import java.io.DataInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.FileWriter; -import java.io.InputStreamReader; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; import java.util.Properties; /** @@ -61,10 +59,10 @@ * * * - * @version v0.6, 2013/11/28 (November) -- Patch of Olivier Duplouy is added. Now you + * @version v0.6, 2013/11/28 (November) -- Patch of Olivier Duplouy is added. Now you * can specify the representation type of your graph: dot, neato, fdp, sfdp, twopi, circo * @version v0.5.1, 2013/03/18 (March) -- Patch of Juan Hoyos (Mac support) - * @version v0.5, 2012/04/24 (April) -- Patch of Abdur Rahman (OS detection + start subgraph + + * @version v0.5, 2012/04/24 (April) -- Patch of Abdur Rahman (OS detection + start subgraph + * read config file) * @version v0.4, 2011/02/05 (February) -- Patch of Keheliya Gallaba is added. Now you * can specify the type of the output file: gif, dot, fig, pdf, ps, svg, png, etc. @@ -107,7 +105,7 @@ public class GraphViz /** * The image size in dpi. 96 dpi is normal size. Higher values are 10% higher each. * Lower values 10% lower each. - * + * * dpi patch by Peter Mueller */ private int[] dpiSizes = {46, 51, 57, 63, 70, 78, 86, 96, 106, 116, 128, 141, 155, 170, 187, 206, 226, 249}; @@ -203,7 +201,7 @@ public void clearGraph(){ */ public byte[] getGraph(String dot_source, String type, String representationType) { - File dot; + Path dot = null; byte[] img_stream = null; try { @@ -211,40 +209,33 @@ public byte[] getGraph(String dot_source, String type, String representationType if (dot != null) { img_stream = get_img_stream(dot, type, representationType); - if (dot.delete() == false) - System.err.println("Warning: " + dot.getAbsolutePath() + " could not be deleted!"); + Files.delete(dot); + return img_stream; } return null; - } catch (java.io.IOException ioe) { return null; } + } catch (java.io.IOException ioe) { + System.err.println("Warning: " + dot.toString() + " could not be deleted!"); + return null; + } } /** * Writes the graph's image in a file. * @param img A byte array containing the image of the graph. * @param file Name of the file to where we want to write. - * @return Success: 1, Failure: -1 */ - public int writeGraphToFile(byte[] img, String file) - { - File to = new File(file); - return writeGraphToFile(img, to); + public void writeGraphToFile(byte[] img, String file) throws IOException { + writeGraphToFile(img, Paths.get(file)); } /** * Writes the graph's image in a file. * @param img A byte array containing the image of the graph. * @param to A File object to where we want to write. - * @return Success: 1, Failure: -1 */ - public int writeGraphToFile(byte[] img, File to) - { - try { - FileOutputStream fos = new FileOutputStream(to); - fos.write(img); - fos.close(); - } catch (java.io.IOException ioe) { return -1; } - return 1; + public void writeGraphToFile(byte[] img, Path to) throws IOException { + Files.write(to, img, StandardOpenOption.CREATE); } /** @@ -264,33 +255,34 @@ public int writeGraphToFile(byte[] img, File to) * @see http://www.graphviz.org under the Roadmap title * @return The image of the graph in .gif format. */ - private byte[] get_img_stream(File dot, String type, String representationType) + private byte[] get_img_stream(Path dot, String type, String representationType) { - File img; + Path img = null; + try { + img = Files.createTempFile(Paths.get(GraphViz.TEMP_DIR), "graph_", "."+type); + } catch (IOException e) { + System.err.println("Error: in I/O processing of tempfile in dir " + GraphViz.TEMP_DIR+"\n"); + e.printStackTrace(); + } byte[] img_stream = null; - try { - img = File.createTempFile("graph_", "."+type, new File(GraphViz.TEMP_DIR)); + try { Runtime rt = Runtime.getRuntime(); // patch by Mike Chenault // representation type with -K argument by Olivier Duplouy - String[] args = {DOT, "-T"+type, "-K"+representationType, "-Gdpi="+dpiSizes[this.currentDpiPos], dot.getAbsolutePath(), "-o", img.getAbsolutePath()}; + String[] args = {DOT, "-T"+type, "-K"+representationType, "-Gdpi="+dpiSizes[this.currentDpiPos], dot.toString(), "-o", img.toString()}; Process p = rt.exec(args); p.waitFor(); - FileInputStream in = new FileInputStream(img.getAbsolutePath()); + InputStream in = Files.newInputStream(img, StandardOpenOption.READ); img_stream = new byte[in.available()]; in.read(img_stream); - // Close it if we need to - if( in != null ) in.close(); - if (img.delete() == false) - System.err.println("Warning: " + img.getAbsolutePath() + " could not be deleted!"); + Files.delete(img); } catch (java.io.IOException ioe) { - System.err.println("Error: in I/O processing of tempfile in dir " + GraphViz.TEMP_DIR+"\n"); - System.err.println(" or in calling external command"); + System.err.println("Error: in calling external command"); ioe.printStackTrace(); } catch (java.lang.InterruptedException ie) { @@ -307,20 +299,10 @@ private byte[] get_img_stream(File dot, String type, String representationType) * @param str Source of the graph (in dot language). * @return The file (as a File object) that contains the source of the graph. */ - private File writeDotSourceToFile(String str) throws java.io.IOException + private Path writeDotSourceToFile(String str) throws IOException { - File temp; - try { - temp = File.createTempFile("graph_", ".dot.tmp", new File(GraphViz.TEMP_DIR)); - FileWriter fout = new FileWriter(temp); - fout.write(str); - fout.close(); - } - catch (Exception e) { - System.err.println("Error: I/O error while writing the dot source to temp file!"); - return null; - } - return temp; + Path path = Files.createTempFile(Paths.get(GraphViz.TEMP_DIR), "graph_", ".dot.tmp"); + return Files.write(path, str.getBytes(), StandardOpenOption.CREATE); } /** @@ -358,7 +340,7 @@ public String end_subgraph() { /** * Read a DOT graph from a text file. - * + * * @param input Input text file containing the DOT graph * source. */ @@ -366,18 +348,9 @@ public void readSource(String input) { StringBuilder sb = new StringBuilder(); - try - { - FileInputStream fis = new FileInputStream(input); - DataInputStream dis = new DataInputStream(fis); - BufferedReader br = new BufferedReader(new InputStreamReader(dis)); - String line; - while ((line = br.readLine()) != null) { - sb.append(line); - } - dis.close(); - } - catch (Exception e) { + try { + Files.lines(Paths.get(input)).forEach(sb::append); + } catch (IOException e) { System.err.println("Error: " + e.getMessage()); } diff --git a/src/com/github/jabbalaci/graphviz/Proba.java b/src/com/github/jabbalaci/graphviz/Proba.java index fe9abdd..08e5093 100644 --- a/src/com/github/jabbalaci/graphviz/Proba.java +++ b/src/com/github/jabbalaci/graphviz/Proba.java @@ -1,6 +1,8 @@ package com.github.jabbalaci.graphviz; -import java.io.File; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; public class Proba { @@ -42,9 +44,13 @@ private void start() // String repesentationType= "twopi"; // String repesentationType= "circo"; - File out = new File("/tmp/out"+gv.getImageDpi()+"."+ type); // Linux + Path out = Paths.get("/tmp/out"+gv.getImageDpi()+"."+ type); // Linux // File out = new File("c:/eclipse.ws/graphviz-java-api/out." + type); // Windows - gv.writeGraphToFile( gv.getGraph(gv.getDotSource(), type, repesentationType), out ); + try { + gv.writeGraphToFile( gv.getGraph(gv.getDotSource(), type, repesentationType), out ); + } catch (IOException e) { + e.printStackTrace(); + } } /** @@ -77,9 +83,13 @@ private void start2() // String repesentationType= "sfdp"; // String repesentationType= "twopi"; // String repesentationType= "circo"; - - File out = new File("/tmp/simple." + type); // Linux + + Path out = Paths.get("/tmp/simple." + type); // Linux // File out = new File("c:/eclipse.ws/graphviz-java-api/tmp/simple." + type); // Windows - gv.writeGraphToFile( gv.getGraph(gv.getDotSource(), type, repesentationType), out ); + try { + gv.writeGraphToFile( gv.getGraph(gv.getDotSource(), type, repesentationType), out ); + } catch (IOException e) { + e.printStackTrace(); + } } }