protected void runScript(String[] cmd, JTextArea txaMsg) { String strg = ""; if (cmd == null) return; Process prcs = null; try { Messages.postDebug("Running script: " + cmd[2]); Runtime rt = Runtime.getRuntime(); prcs = rt.exec(cmd); if (prcs == null) return; InputStream istrm = prcs.getInputStream(); if (istrm == null) return; BufferedReader bfr = new BufferedReader(new InputStreamReader(istrm)); while ((strg = bfr.readLine()) != null) { // System.out.println(strg); strg = strg.trim(); // Messages.postDebug(strg); strg = strg.toLowerCase(); if (txaMsg != null) { txaMsg.append(strg); txaMsg.append("\n"); } } } catch (Exception e) { // e.printStackTrace(); Messages.writeStackTrace(e); Messages.postDebug(e.toString()); } finally { // It is my understanding that these streams are left // open sometimes depending on the garbage collector. // So, close them. try { if (prcs != null) { OutputStream os = prcs.getOutputStream(); if (os != null) os.close(); InputStream is = prcs.getInputStream(); if (is != null) is.close(); is = prcs.getErrorStream(); if (is != null) is.close(); } } catch (Exception ex) { Messages.writeStackTrace(ex); } } }
public boolean executeFilter(@NotNull Editor editor, @NotNull TextRange range, String command) throws IOException { if (logger.isDebugEnabled()) logger.debug("command=" + command); CharSequence chars = editor.getDocument().getCharsSequence(); StringReader car = new StringReader( chars.subSequence(range.getStartOffset(), range.getEndOffset()).toString()); StringWriter sw = new StringWriter(); logger.debug("about to create filter"); Process filter = Runtime.getRuntime().exec(command); logger.debug("filter created"); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(filter.getOutputStream())); logger.debug("sending text"); copy(car, writer); writer.close(); logger.debug("sent"); BufferedReader reader = new BufferedReader(new InputStreamReader(filter.getInputStream())); logger.debug("getting result"); copy(reader, sw); sw.close(); logger.debug("received"); editor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), sw.toString()); lastCommand = command; return true; }
private void startProcess(List<String> command) { ProcessBuilder processBuilder = new ProcessBuilder(command); DarkMod darkMod = DarkMod.getInstance(); DarkModUI ui = darkMod.getUI(); try { if (ui != null) { ui.setVisible(false); ui.dispose(); } Process process = processBuilder.start(); StreamRedirectFactory.createInputToOutputRedirect(process.getInputStream(), System.out); StreamRedirectFactory.createInputToOutputRedirect(process.getErrorStream(), System.err); StreamRedirectFactory.createInputToOutputRedirect(System.in, process.getOutputStream()); System.exit(process.waitFor()); } catch (Exception exception) { exception.printStackTrace(); System.exit(-1); } }