Advertisement
daddydean

Untitled

Jul 11th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. /**
  2.  * ##library.name##
  3.  * ##library.sentence##
  4.  * ##library.url##
  5.  *
  6.  * Copyright ##copyright## ##author##
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Lesser General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2.1 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Lesser General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Lesser General
  19.  * Public License along with this library; if not, write to the
  20.  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  21.  * Boston, MA  02111-1307  USA
  22.  *
  23.  * @author      ##author##
  24.  * @modified    ##date##
  25.  * @version     ##library.prettyVersion## (##library.version##)
  26.  */
  27.  
  28. package deadpixel.command;
  29.  
  30. import java.io.BufferedReader;
  31. import java.io.InputStreamReader;
  32. import java.io.IOException;
  33.  
  34. import java.util.List;
  35. import java.util.ArrayList;
  36.  
  37. /**
  38.  *
  39.  * @example Hello
  40.  *
  41.  * (the tag @example followed by the name of an example included in
  42.  * folder 'examples' will automatically include the example in the javadoc.)
  43.  *
  44.  */
  45.  
  46. public class Command {
  47.   public static final String VERSION = "##library.prettyVersion##";
  48.  
  49.   protected final ArrayList<String> outputBuffer = new ArrayList<>();
  50.   protected final Runtime runtime = Runtime.getRuntime();
  51.  
  52.   public String command;
  53.   public boolean success;
  54.  
  55.   /**
  56.    *
  57.    */
  58.   public Command(final String theCommand) {
  59.     command = theCommand;
  60.   }
  61.  
  62.   /**
  63.    * Runs the command. Returns true if the command was successful.
  64.    * The output of the command can be accessed by calling getOutput().
  65.    *
  66.    * @return true if the command ran successfully,
  67.    * false if there was an error running the command.
  68.    */
  69.   public boolean run() {
  70.     success = false;
  71.     outputBuffer.clear();
  72.  
  73.     try {
  74.       final Process process = runtime.exec(command);
  75.       final String msg = "COMMAND ERROR(s):\n";
  76.       final StringBuilder sb = new StringBuilder(msg);
  77.  
  78.       BufferedReader br;
  79.       String line;  
  80.  
  81.       // Capture the output
  82.       br = new BufferedReader(new InputStreamReader(process.getInputStream()));
  83.       while ((line = br.readLine()) != null)  outputBuffer.add(line);
  84.  
  85.       // Capture the errors
  86.       br = new BufferedReader(new InputStreamReader(process.getErrorStream()));
  87.       while ((line = br.readLine()) != null)  sb.append(line).append('\n');
  88.       if (sb.length() != msg.length())  System.err.println(sb);
  89.  
  90.       success = process.waitFor() == 0;
  91.     }
  92.     catch (final IOException e) {
  93.       System.err.println("COMMAND ERROR: " + e.getMessage());
  94.     }
  95.     catch (final InterruptedException e) {
  96.       System.err.println("COMMAND INTERRUPTED: " + e.getMessage());
  97.     }
  98.  
  99.     return success;
  100.   }
  101.  
  102.   /**
  103.    * Returns each line of the command's output as an Array of String objects.
  104.    * Useful if you need to capture the results from running a command.
  105.    */
  106.   public String[] getOutput() {
  107.     return outputBuffer.toArray(new String[outputBuffer.size()]);
  108.   }
  109.  
  110.   /**
  111.    * Returns each line of the command's output as a List of String objects.
  112.    * Useful if you need to capture the results from running a command.
  113.    */
  114.   public List<String> getOutputAsList() {
  115.     return (List<String>) outputBuffer.clone();
  116.   }
  117.  
  118.   /**
  119.    * Returns the command String being used.
  120.    *
  121.    * @return String
  122.    */
  123.   @Override public String toString() {
  124.     return command + ' ' + success;
  125.   }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement