1818*
1919* this library is the base of Simple-ADB program : http://forum.xda-developers.com/android/software/revive-simple-adb-tool-t3417155
2020* you can contact me @ [email protected] 21- * Source : https://github.com/mhashim6/Commander
21+ * Source :
2222*
2323*/
2424
2525package mhashem6 .commander ;
2626
27- import java .io .BufferedReader ;
2827import java .io .IOException ;
29- import java .io .InputStreamReader ;
3028
3129import mhashem6 .commander .exceptions .*;
3230
3331/**
34- * The class the can execute/abort a command, with ability of
35- * producing(automatically) and writing (not automatically) an output.
32+ * The class the can execute/abort a command, with ability of producing and
33+ * writing output.
3634 *
3735 * @see Command
38- * @see SpecificCommand
39- * @see ExecuterAndWriter
4036 * @author mhashem6 (Muhammad Hashim)
4137 */
4238public class Executer {
@@ -46,33 +42,40 @@ public class Executer {
4642 protected ProcessBuilder pb ;
4743 protected Process process ;
4844
49- protected InputStreamReader stdReader ;
50- protected InputStreamReader errReader ;
45+ protected ExecuterStreamFormatter eStreamFormatter ;
5146
52- protected BufferedReader stdOutputBr ;
53- protected BufferedReader errOutputBr ;
54-
55- protected String outputStdStr ;
56- protected String outputErrStr ;
57- protected boolean hasError ;
58-
59- protected StringBuilder outputToSave ;
60-
61- private Appender appendrObject ;
47+ protected int exitval = 777 ;
6248
6349 /**
6450 *
65- * The public constructor that requires a Appender implementing object to
66- * display the output, if null is passed, the output will be written in the
67- * console.
51+ * A constructor that requires a pre-initialized ExecuterStreamFormatter
52+ * object to display the output.
53+ *
54+ * @param eStreamFormatter
55+ *
56+ * @see ExecuterStreamFormatter
57+ */
58+ public Executer (ExecuterStreamFormatter eStreamFormatter ) {
59+ setStreamFormatter (eStreamFormatter );
60+ }
61+ // ============================================================
62+
63+ /**
6864 *
69- * @param appendrObject
65+ * @return the ExecuterStreamFormatter instance of this instance.
66+ */
67+ public ExecuterStreamFormatter getStreamFormatter () {
68+ return eStreamFormatter ;
69+ }
70+ // ============================================================
71+
72+ /**
73+ * changes the current instance of ExecuterStreamFormatter.
7074 *
71- * @see Appender
75+ * @param eStreamFormatter
7276 */
73- public Executer (Appender appendrObject ) {
74- clearOutput ();
75- this .appendrObject = appendrObject ;
77+ public void setStreamFormatter (ExecuterStreamFormatter eStreamFormatter ) {
78+ this .eStreamFormatter = eStreamFormatter ;
7679 }
7780 // ============================================================
7881
@@ -83,12 +86,12 @@ public Executer(Appender appendrObject) {
8386 * @throws IOException
8487 */
8588 public void execute (Command command ) throws IOException {
86-
87- clearOutput ();
89+ exitval = 777 ;
8890 executeSilently (command );
89-
9091 recordOutput ();
9192
93+ abort ();
94+
9295 }
9396 // ============================================================
9497
@@ -114,92 +117,36 @@ protected void executeSilently(Command cmd) throws UnrecognisedCmdException {
114117 // ============================================================
115118
116119 /**
117- * the method responsible for recording the output of the command line
120+ * the method responsible for passing the output to the ExecuterStreamReader
121+ * object.
118122 *
119123 * @throws IOException
120124 *
121125 */
122126 protected void recordOutput () throws IOException {
123127
124- try {
125-
126- /** Get input streams */
127- stdReader = new InputStreamReader (process .getInputStream ());
128- errReader = new InputStreamReader (process .getErrorStream ());
129-
130- stdOutputBr = new BufferedReader (stdReader );
131- errOutputBr = new BufferedReader (errReader );
132-
133- outputToSave = new StringBuilder ();
134- /** Read command standard output */
135- while ((outputStdStr = stdOutputBr .readLine ()) != null ) {
136-
137- store (outputStdStr );
138- outputStdStr = null ;
139- }
140-
141- /** Read command error output */
142- while ((outputErrStr = errOutputBr .readLine ()) != null ) {
143- store (outputErrStr );
144- hasError = true ;
145- outputErrStr = null ;
146- }
147-
148- }
149-
150- finally {
151-
152- if (hasError == true )
153- throw new OutputIsErrorException (cmd .getClient () != "" ? cmd .getClient () : "System" );
154- closeResources ();
155-
156- }
157-
158- }
159- // ============================================================
160-
161- /**
162- * to store the output line by line.
163- *
164- * @param line
165- */
166- protected void store (String line ) {
167-
168- outputToSave .append ((line + System .getProperty ("line.separator" )));
169- showOutputLine (line );
170-
171- line = null ;
172- }
173- // ============================================================
174-
175- /**
176- * displays the output line by line
177- *
178- * @param line
179- */
180- protected void showOutputLine (String line ) {
181-
182- appendrObject .appendLine (line + System .getProperty ("line.separator" ));
183-
184- line = null ;
128+ eStreamFormatter .FormatStdStream (process .getInputStream ());
129+ eStreamFormatter .FormatErrStream (process .getErrorStream ());
185130
186131 }
187132 // ============================================================
188133
189134 /**
190135 * a method that aborts the current command process
191136 *
192- * @throws CmdAbortedException
137+ * @return
138+ *
193139 * @throws IOException
194140 *
195141 * @see Executer#isAlive()
196142 */
197143 public void abort () throws IOException {
198144 if (isAlive ()) {
199- // closeResources();
200145 process .destroyForcibly ();
201-
146+ exitval = process . exitValue ();
202147 }
148+ eStreamFormatter .closeResources ();
149+
203150 }
204151 // ============================================================
205152
@@ -216,66 +163,21 @@ public boolean isAlive() {
216163 }
217164 // ============================================================
218165
219- /**
220- * returns a String representing the full output of the execution
221- *
222- * @throws NoOutputException
223- * @throws ExecNotFinishedException
224- */
225- public String getOutput () throws NoOutputException , ExecNotFinishedException {
226-
227- if (isAlive ())
228- throw new ExecNotFinishedException ();
229-
230- if (outputToSave == null || outputToSave .equals ("" ))
231- throw new NoOutputException ();
232-
233- return outputToSave .toString ();
234- }
235- // ============================================================
236-
237166 /**
238167 * @return the previously executed Command object stored in the Executer
239168 */
240169 public Command getCommand () {
241- if (cmd == null )
242- throw new NullPointerException ();
243170 return cmd ;
244171 }
245172
246173 /**
247- * clears the output of the execution
248- */
249- public void clearOutput () {
250- outputToSave = null ;
251- outputStdStr = null ;
252- outputErrStr = null ;
253- hasError = false ;
254- }
255- // ============================================================
256-
257- /**
258- * closes the used resources objects used to obtain the output
259174 *
260- * @throws IOException
175+ * @return exit value of the process, or 777 if the exit value couldn't be
176+ * retrieved successfully.
261177 */
262- protected void closeResources () throws IOException {
263-
264- cmd = null ;
265- pb = null ;
266- process = null ;
267-
268- stdReader .close ();
269- stdOutputBr .close ();
270- stdReader = null ;
271- stdOutputBr = null ;
272-
273- errReader .close ();
274- errOutputBr .close ();
275- errReader = null ;
276- errOutputBr = null ;
178+ public int getExitValue () {
179+ return exitval ;
277180
278181 }
279- // ============================================================
280182
281183}
0 commit comments