From: <bma...@us...> - 2011-09-29 16:13:36
|
Revision: 6461 http://fudaa.svn.sourceforge.net/fudaa/?rev=6461&view=rev Author: bmarchan Date: 2011-09-29 16:13:30 +0000 (Thu, 29 Sep 2011) Log Message: ----------- Mod : FuLib.runProgram() avec r?\195?\169cup?\195?\169ration des sortie err ou out dans 2 buffers s?\195?\169par?\195?\169s Modified Paths: -------------- trunk/framework/ctulu-fu/src/main/java/com/memoire/fu/FuLib.java Modified: trunk/framework/ctulu-fu/src/main/java/com/memoire/fu/FuLib.java =================================================================== --- trunk/framework/ctulu-fu/src/main/java/com/memoire/fu/FuLib.java 2011-09-29 09:25:39 UTC (rev 6460) +++ trunk/framework/ctulu-fu/src/main/java/com/memoire/fu/FuLib.java 2011-09-29 16:13:30 UTC (rev 6461) @@ -949,20 +949,32 @@ /** * Runs an external program. With exception. + * @param _cmd + * @param _dir + */ + public static String runProgram(String[] _cmd, File _dir) throws IOException { + return runProgram(_cmd, _dir, null, null); + } + + /** + * Runs an external program, with errors and outputs in differents buffers. With exception. * * @param _cmd the command line + * @param _sbout Output stringbuffer. Can be null. + * @param _sberr Errors stringbuffer. Ca be null. * @return the output of the command execution */ - public static String runProgram(String[] _cmd,File _dir) throws IOException { + public static String runProgram(String[] _cmd,File _dir, final StringBuffer _sbout, final StringBuffer _sberr) throws IOException { final Process proc = _dir==null?Runtime.getRuntime().exec(_cmd):Runtime.getRuntime().exec(_cmd,null,_dir); - final ByteArrayOutputStream out = new ByteArrayOutputStream(); + final ByteArrayOutputStream sall = new ByteArrayOutputStream(); + final ByteArrayOutputStream serr = new ByteArrayOutputStream(); + final ByteArrayOutputStream sout = new ByteArrayOutputStream(); final boolean[] exited = new boolean[1]; Thread thread = new Thread(new Runnable() { public void run() { InputStream sin1 = null; InputStream sin2 = null; - PrintStream sout = new PrintStream(out); try { sin1 = new BufferedInputStream(proc.getInputStream()); @@ -981,6 +993,7 @@ break; } sout.write(c); + sall.write(c); wait = false; } } catch (IOException ex) { @@ -994,7 +1007,8 @@ sin2 = null; break; } - sout.write(c); + serr.write(c); + sall.write(c); wait = false; } } catch (IOException ex) { @@ -1008,13 +1022,28 @@ } } - while ((sin1 != null) && (sin1.available() > 0)) - sout.write(sin1.read()); - while ((sin2 != null) && (sin2.available() > 0)) - sout.write(sin2.read()); + while ((sin1 != null) && (sin1.available() > 0)) { + int c=sin1.read(); + sout.write(c); + sall.write(c); + } + while ((sin2 != null) && (sin2.available() > 0)) { + int c=sin2.read(); + serr.write(c); + sall.write(c); + } } catch (IOException ex) {} finally { - /* if(sout!=null) */sout.flush(); + /* if(sout!=null) */ try { + sall.flush(); + sout.flush(); + serr.flush(); + } + catch (IOException _exc) { + FuLog.error(_exc); + } + + try { if(sin1!=null) sin1.close(); } catch (IOException _evt) { FuLog.error(_evt); @@ -1040,7 +1069,12 @@ thread.join(); } catch (InterruptedException ex) {} - return new String(out.toByteArray()); + if (_sberr!=null) + _sberr.append(serr.toString()); + if (_sbout!=null) + _sbout.append(sout.toString()); + + return new String(sall.toByteArray()); } /* This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <bma...@us...> - 2017-01-09 19:45:04
|
Revision: 9502 http://sourceforge.net/p/fudaa/svn/9502 Author: bmarchan Date: 2017-01-09 19:45:02 +0000 (Mon, 09 Jan 2017) Log Message: ----------- Ajout de status de sortie sur les methodes runProgram() Modified Paths: -------------- trunk/framework/ctulu-fu/src/main/java/com/memoire/fu/FuLib.java Modified: trunk/framework/ctulu-fu/src/main/java/com/memoire/fu/FuLib.java =================================================================== --- trunk/framework/ctulu-fu/src/main/java/com/memoire/fu/FuLib.java 2017-01-09 19:44:22 UTC (rev 9501) +++ trunk/framework/ctulu-fu/src/main/java/com/memoire/fu/FuLib.java 2017-01-09 19:45:02 UTC (rev 9502) @@ -56,6 +56,9 @@ private static final boolean DEBUG = Fu.DEBUG && true; private static final boolean TRACE = Fu.TRACE && true; + /** Le statut d'exit de la derniere execution d'un processus externe */ + public static int lastRunStatus = 0; + public static final String DEFAULT_ENCODING = getSystemProperty("file.encoding"); public static void sleep(long _delay) { @@ -1080,7 +1083,7 @@ thread.setPriority(Math.max(Thread.MIN_PRIORITY, Thread.currentThread().getPriority() - 1)); thread.start(); try { - proc.waitFor(); + lastRunStatus=proc.waitFor(); } catch (InterruptedException ex) {} exited[0] = true; try { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |