List < String > performCommand ( final String [ ] cmdAttribs , final int max , final long timeout ) throws IOException { // this method does what it can to avoid the 'Too many open files' error // based on trial and error and these links: // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4784692 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4801027 // http://forum.java.sun.com/thread.jspa?threadID=533029&messageID=2572018 // however, its still not perfect as the JDK support is so poor // (see commons-exec or Ant for a better multi-threaded multi-os solution) final List < String > lines = new ArrayList < String > ( 20 ) ; Process proc = null ; InputStream in = null ; OutputStream out = null ; InputStream err = null ; BufferedReader inr = null ; try { final Thread monitor = ThreadMonitor . start ( timeout ) ; proc = openProcess ( cmdAttribs ) ; in = proc . getInputStream ( ) ; out = proc . getOutputStream ( ) ; err = proc . getErrorStream ( ) ; // default charset is most likely appropriate here inr = new BufferedReader ( new InputStreamReader ( in , Charset . defaultCharset ( ) ) ) ; String line = inr . readLine ( ) ; while ( line != null && lines . size ( ) < max ) { line = line . toLowerCase ( Locale . ENGLISH ) . trim ( ) ; lines . add ( line ) ; line = inr . readLine ( ) ; } proc . waitFor ( ) ; ThreadMonitor . stop ( monitor ) ; if ( proc . exitValue ( ) != 0 ) { // os command problem, throw exception throw new IOException ( "Command line returned OS error code '" + proc . exitValue ( ) + "' for command " + Arrays . asList ( cmdAttribs ) ) ; } if ( lines . isEmpty ( ) ) { // unknown problem, throw exception throw new IOException ( "Command line did not return any info " + "for command " + Arrays . asList ( cmdAttribs ) ) ; } return lines ; } catch ( final InterruptedException ex ) { throw new IOException ( "Command line threw an InterruptedException " + "for command " + Arrays . asList ( cmdAttribs ) + " timeout=" + timeout , ex ) ; } finally { IOUtil . closeQuietly ( in ) ; IOUtil . closeQuietly ( out ) ; IOUtil . closeQuietly ( err ) ; IOUtil . closeQuietly ( inr ) ; if ( proc != null ) { proc . destroy ( ) ; } } }
Performs the os command .