@@ -38,9 +38,7 @@
|
||||
*/
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.io.PrintStream;
|
||||
@@ -56,67 +54,34 @@ import java.nio.charset.StandardCharsets;
|
||||
public final class PipedConsoleInput
|
||||
implements ConsoleInput {
|
||||
|
||||
/** The encoding between streams. */
|
||||
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
|
||||
/** THe inner manager. */
|
||||
private final StreamConsoleInput innerManager;
|
||||
/** The stream to pipe commands into. */
|
||||
private final PipedOutputStream commandInput;
|
||||
/** The reader to get application return from. */
|
||||
private final BufferedReader commandBuffOutput;
|
||||
/** The stream to get application return from. */
|
||||
private final PipedInputStream commandOutput;
|
||||
/** The print writer for application to write return to. */
|
||||
private final PrintStream outPrint;
|
||||
/** The stream for the application to read commands from. */
|
||||
private final PipedInputStream in;
|
||||
/** The writing thread. */
|
||||
private final WritingRunnable writing;
|
||||
/** The reading thread. */
|
||||
private final ReadingRunnable reading;
|
||||
|
||||
/** Create a manager that will write and read through piped stream.
|
||||
*
|
||||
*
|
||||
* @param outPrint the stream to write the prompting messages to
|
||||
* @throws IOException if the piping failed for streams */
|
||||
public PipedConsoleInput() throws IOException {
|
||||
public PipedConsoleInput(final PrintStream outPrint) throws IOException {
|
||||
commandInput = new PipedOutputStream();
|
||||
in = new PipedInputStream(commandInput);
|
||||
commandOutput = new PipedInputStream();
|
||||
final PipedOutputStream out = new PipedOutputStream(commandOutput);
|
||||
commandBuffOutput = new BufferedReader(
|
||||
new InputStreamReader(commandOutput, StandardCharsets.UTF_8));
|
||||
outPrint = new PrintStream(out, true, UTF_8);
|
||||
innerManager = new StreamConsoleInput(outPrint, in,
|
||||
StandardCharsets.UTF_8);
|
||||
writing = new WritingRunnable(commandInput, StandardCharsets.UTF_8);
|
||||
reading = new ReadingRunnable(commandBuffOutput);
|
||||
Thread th = new Thread(writing, "write"); //$NON-NLS-1$
|
||||
final Thread th = new Thread(writing, "write"); //$NON-NLS-1$
|
||||
th.start();
|
||||
th = new Thread(reading, "read"); //$NON-NLS-1$
|
||||
th.setDaemon(true);
|
||||
th.start();
|
||||
}
|
||||
|
||||
/** Test if a content is available on the reading head.
|
||||
* <p>
|
||||
* If this method returns true, the next {@link #prompt()} operation should
|
||||
* return immediatly.
|
||||
*
|
||||
* @return the content of the next line written by the application
|
||||
* @throws IOException if the reading failed */
|
||||
public boolean available() throws IOException {
|
||||
return reading.hasMessage();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
reading.setRunning(false);
|
||||
writing.setRunning(false);
|
||||
in.close();
|
||||
innerManager.close();
|
||||
outPrint.close();
|
||||
commandBuffOutput.close();
|
||||
commandOutput.close();
|
||||
commandInput.close();
|
||||
}
|
||||
|
||||
@@ -125,18 +90,6 @@ public final class PipedConsoleInput
|
||||
return innerManager.getPrompt();
|
||||
}
|
||||
|
||||
/** Wait for a specific message to arrive.
|
||||
* <p>
|
||||
* When this method returns, the message was appended to the data, it
|
||||
* <em>may or may not</em> be the next line of data.
|
||||
*
|
||||
* @param message the message
|
||||
* @return the thread to join to wait for message delivery
|
||||
* @see fr.bigeon.gclc.manager.ReadingRunnable#getWaitForDelivery(java.lang.String) */
|
||||
public Thread getWaitForDelivery(final String message) {
|
||||
return reading.getWaitForDelivery(message);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
|
||||
@Override
|
||||
@@ -181,16 +134,6 @@ public final class PipedConsoleInput
|
||||
return innerManager.prompt(message + System.lineSeparator(), timeout);
|
||||
}
|
||||
|
||||
/** Read the next line in the input printed content.
|
||||
* <p>
|
||||
* This corresponds to the {@link #prompt(String)} messages.
|
||||
*
|
||||
* @return the content of the next line written by the application
|
||||
* @throws IOException if the reading failed */
|
||||
public String readNextLine() throws IOException {
|
||||
return reading.getMessage();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleInput#setPrompt(java.lang.String) */
|
||||
@Override
|
||||
|
||||
@@ -154,7 +154,10 @@ public final class StreamConsoleInput implements ConsoleInput {
|
||||
@Override
|
||||
public String prompt(final String message) throws IOException {
|
||||
checkOpen();
|
||||
out.print(message);
|
||||
if (out != null) {
|
||||
out.print(message);
|
||||
out.flush();
|
||||
}
|
||||
return reading.getMessage();
|
||||
}
|
||||
|
||||
@@ -164,7 +167,10 @@ public final class StreamConsoleInput implements ConsoleInput {
|
||||
public String prompt(final String message,
|
||||
final long timeout) throws IOException {
|
||||
checkOpen();
|
||||
out.print(message);
|
||||
if (out != null) {
|
||||
out.print(message);
|
||||
out.flush();
|
||||
}
|
||||
return reading.getNextMessage(timeout);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user