Skip to content

Commit c2d29a0

Browse files
author
Muhammad Hashim
committed
new class : ExecuterStreamFormatter , for sake of readability and encapsulation, this class encapsulates the output-related code that was in the Executer class, modified other classes for compatibilty with this new class, Appender interface now has a method for standard output and a method for error output, removed no-more neccessary exceptions
1 parent 5a0175b commit c2d29a0

File tree

7 files changed

+232
-243
lines changed

7 files changed

+232
-243
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ Command cmd = new Command();
88
cmd.setClient("adb");
99
cmd.setCommand("devices -l");
1010
//optional : cmd.setCommand("devices", "-l"):
11-
Executer executer = new Executer(line -> System.out.println(line)); //redirecting output of execution.
11+
ExecuterStreamFormatter streamFormatter = new ExecuterStreamFormatter(); //redirecting output of execution to the console,
12+
//optional: implement the Appender interface to redirect the standard and error outputs your way.
13+
14+
Executer executer = new Executer(streamFormatter);
1215
try{
1316
executer.execute(cmd);
1417
}catch(IOException e){

src/mhashem6/commander/Appender.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@
3535
public interface Appender {
3636

3737
/**
38-
* appends a new line of output
38+
* appends a new line of standard output
3939
*
4040
* @param outputLine
4141
*/
4242
void appendLine(String outputLine);
43+
// ============================================================
4344

45+
/**
46+
* appends a new line of error output
47+
*
48+
* @param errOutputLine
49+
*/
50+
void appendErrLine(String errOutputLine);
51+
// ============================================================
4452
}
45-
// ============================================================

src/mhashem6/commander/Command.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,21 @@ public Command(String client, ArrayList<String> cmd) {
119119
setCommand(cmd);
120120
}
121121
// ============================================================
122+
123+
/**
124+
* A copy Constructor
125+
*
126+
* @param commandObject
127+
*/
128+
@SuppressWarnings("unchecked")
129+
public Command(Command commandObject) {
130+
this();
131+
client = commandObject.getClient();
132+
command = (ArrayList<String>) command.clone();
133+
commandArgs = (ArrayList<String>) commandArgs.clone();
134+
finalCommand = (ArrayList<String>) finalCommand.clone();
135+
}
136+
// ============================================================
122137
// ============================================================
123138

124139
// CLIENT >>>>
@@ -293,4 +308,11 @@ public void clearAll() {
293308
// ============================================================
294309
// ============================================================
295310

311+
@Override
312+
public boolean equals(Object otherObject) {
313+
if (otherObject instanceof Command)
314+
if (this.toString().equals(otherObject.toString()))
315+
return true;
316+
return false;
317+
}
296318
}

src/mhashem6/commander/Executer.java

Lines changed: 46 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,21 @@
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

2525
package mhashem6.commander;
2626

27-
import java.io.BufferedReader;
2827
import java.io.IOException;
29-
import java.io.InputStreamReader;
3028

3129
import 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
*/
4238
public 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

Comments
 (0)