format and commands

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
2018-10-16 18:53:39 -04:00
parent 1df33afdbe
commit 216cd41dc8
7 changed files with 124 additions and 59 deletions

View File

@@ -51,7 +51,11 @@ import net.bigeon.gclc.exception.CommandRunExceptionType;
import net.bigeon.gclc.manager.ConsoleInput;
import net.bigeon.gclc.manager.ConsoleOutput;
/** A command that will execute a system command.
/** A command that will execute system commands.
* <p>
* Note that this class may induce security issues in the code as it can execute
* arbitrary system commands. It is recommended that it be used only in private
* applications or application used by aware parties.
*
* @author Emmanuel Bigeon */
public class ExecSystemCommand extends Command {
@@ -91,10 +95,11 @@ public class ExecSystemCommand extends Command {
} catch (final IOException e2) {
throw new CommandRunException("Unable to run process", e2);
}
// Stream forwarding to the application's console
final InputStream is = proc.getInputStream();
final Thread th = new Thread(new Runnable() {
/* (non-Javadoc)
* @see java.lang.Runnable#run() */
@Override
public void run() {
try {
@@ -109,10 +114,11 @@ public class ExecSystemCommand extends Command {
});
th.start();
in.setPrompt(""); //$NON-NLS-1$
// Forwar console input to the process.
final OutputStream os = proc.getOutputStream();
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os))) {
while (th.isAlive()) {
String user = in.prompt();
final String user = in.prompt();
if (!user.isEmpty()) {
writer.write(user + EOL);
}
@@ -123,14 +129,16 @@ public class ExecSystemCommand extends Command {
}
}
/** @param is the input stream
/** Read the input until its end.
*
* @param is the input stream
* @throws CommandRunException if the manager was closed while writing the
* stream */
protected void readToEnd(final ConsoleOutput out, final InputStream is)
throws CommandRunException {
int c;
try {
while ((c = is.read()) != -1) {
while ((c = is.read()) >= 0) {
printCharacter(out, (char) c);
}
} catch (final IOException e) {
@@ -143,7 +151,7 @@ public class ExecSystemCommand extends Command {
* @param out the console
* @param c the character
* @throws CommandRunException if the console failed to print the character. */
private void printCharacter(final ConsoleOutput out, char c)
private static void printCharacter(final ConsoleOutput out, final char c)
throws CommandRunException {
try {
out.print(Character.toString(c));