📄 exec.java
字号:
// attach to input of process processWriter = new PrintWriter(p.getOutputStream()); } // wait for exit status exitVal = p.waitFor(); // also wait for redir threads to die, if doing redir if (outStreamRedir != null) outReader.join(); if (errStreamRedir != null) errReader.join(); StringBuffer com = new StringBuffer(); if (command != null) com.append(command); else { for (int i=0; i<exec.length; i++) com.append(exec[i]+" "); } //System.out.println("Process finished [exit: "+exitVal+"]: "+com.toString()); synchronized(finishedListeners) { FinishedEvent e = new FinishedEvent(this, com.toString(), dir, exitVal); ArrayList<FinishedListener> copy = new ArrayList<FinishedListener>(); // make copy cause listeners may want to remove themselves if process finished for (FinishedListener l : finishedListeners) { copy.add(l); } for (FinishedListener l : copy) { l.processFinished(e); } } synchronized(this) { if (processWriter != null) { processWriter.close(); processWriter = null; } } } catch (Exception e) { ActivityLogger.logException(e); } } /** * Send a line of text to the process. This is not useful * if the process is not expecting any input. * @param line a line of text to send to the process */ public void writeln(String line) { synchronized(this) { if (processWriter == null) { System.out.println("Can't write to process: No valid process running."); return; } processWriter.println(line); processWriter.flush(); } } /** * Add a Exec.FinishedListener * @param a the listener */ public void addFinishedListener(FinishedListener a) { synchronized(finishedListeners) { finishedListeners.add(a); } } /** * Remove a Exec.FinishedListener * @param a the listener */ public void removeFinishedListener(FinishedListener a) { synchronized(finishedListeners) { finishedListeners.remove(a); } } /** * End this process, if it is running. Otherwise, does nothing */ public synchronized void destroyProcess() { if (p != null) { p.destroy(); } } public int getExitVal() { return exitVal; } /** * Check for a string passed to the OutputStream. All chars passed to * this class are also transparently passed to System.out. * This only checks for strings within a single line of text. * The strings are simple strings, not regular expressions. */ public static class OutputStreamChecker extends OutputStream implements Serializable { private OutputStream ostream; private String checkFor; private StringBuffer lastLine; private char [] buf; private int bufOffset; private boolean found; private boolean regexp; // if checkFor string is a regular expression private String foundLine; private File copyToFile; private PrintWriter out; private List<OutputStreamCheckerListener> listeners; private Exec exec = null; /** * Checks for string in output stream. The string may span multiple lines, or may be contained * within a non-terminated line (such as an input query). * @param ostream send read data to this output stream (usually System.out) * @param checkFor the string to check for */ public OutputStreamChecker(OutputStream ostream, String checkFor) { this(ostream, checkFor, false, null); } /** * Checks for string in output stream. String must be contained within one line. * @param ostream send read data to this output stream (usually System.out) * @param checkFor the string to check for * @param regexp if true, the string is considered a regular expression * @param copyToFile if non-null, the output is copied to this file */ public OutputStreamChecker(OutputStream ostream, String checkFor, boolean regexp, File copyToFile) { this.ostream = ostream; this.checkFor = checkFor; this.regexp = regexp; lastLine = null; buf = null; bufOffset = 0; lastLine = new StringBuffer(); if (!regexp) buf = new char[checkFor.length()]; found = false; foundLine = null; out = null; this.copyToFile = copyToFile; if (copyToFile != null) { try { out = new PrintWriter(new BufferedWriter(new FileWriter(this.copyToFile))); } catch (IOException e) { System.out.println(e.getMessage()); out = null; } } listeners = new ArrayList<OutputStreamCheckerListener>(); } public void write(int b) throws IOException { ostream.write(b); if (out != null) out.write(b); lastLine.append((char)b); if (regexp) { // match against regular expression on end-of-line if (b == '\n') { if (lastLine.toString().matches(checkFor)) { found = true; foundLine = lastLine.toString(); alertListeners(foundLine); } } } else { // store data in buffer of same length as string trying to match buf[bufOffset] = (char)b; bufOffset++; if (bufOffset >= buf.length) bufOffset = 0; // check against string. Since same length, when string is found bufOffset // will be at start of string. boolean matched = true; for (int i=0; i<buf.length; i++) { int y = (i+bufOffset) % buf.length; if (checkFor.charAt(i) != buf[y]) { matched = false; break; } } if (matched) { found = true; foundLine = lastLine.toString(); alertListeners(foundLine); } } if (b == '\n') { lastLine.delete(0, lastLine.length()); } } public void addOutputStreamCheckerListener(OutputStreamCheckerListener l) { listeners.add(l); } public void removeOutputStreamCheckerListener(OutputStreamCheckerListener l) { listeners.remove(l); } private void alertListeners(String matched) { for (OutputStreamCheckerListener l : listeners) { l.matchFound(exec, matched); } } public boolean getFound() { return found; } public String getFoundLine() { return foundLine; } public void close() { if (out != null) out.close(); } public File getCopyToFile() { return copyToFile; } private void setExec(Exec e) { this.exec = e; } } /** * Interface for objects to be notified immediately when OutputStreamChecker * matches when it is looking for. */ public interface OutputStreamCheckerListener { public void matchFound(Exec exec, String matched); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -