001package votorola.g.lang; // Copyright 2006, 2009-2010, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import java.io.*;
004import java.nio.charset.*;
005import votorola.g.io.*;
006
007
008/** Process utilities.
009  */
010public @ThreadSafe final class ProcessX
011{
012
013    private ProcessX() {}
014
015
016
017    /** Appends the entire output of the process to the specified appendable.
018      *
019      *     @return the same appendable
020      */
021    public static Appendable appendTo( final Appendable a, final Process p, final Charset charset )
022      throws IOException
023    {
024        final BufferedReader in = new BufferedReader( new InputStreamReader(
025          p.getInputStream(), charset ));
026        try{ ReaderX.appendTo( a, in ); }
027        finally{ in.close(); }
028        return a;
029    }
030
031
032
033 // /** Same as p.waitFor(), but returns normally if interrupted.
034 //   * Useful only if you don't mind the exit code being hidden.
035 //   *
036 //   *     @return true if the wait ended normally; false if an InterruptedException occured
037 //   */
038 // public static boolean tryWaitFor( final Process p )
039 // {
040 //     try
041 //     {
042 //         p.waitFor();
043 //         return true;
044 //     }
045 //     catch( InterruptedException x ) { return false; }
046 // }
047 //// why hide the exit code?
048
049
050
051    /** Same as p.waitFor(), but throws an IOException if interrupted.
052      */
053    public static int waitForWithoutInterrupt( final Process p ) throws IOException
054    {
055        try{ return p.waitFor(); }
056        catch( InterruptedException x ) { throw new IOException( x ); }
057    }
058
059
060
061}