* fix IO redirection issues when script is reading from STDIN

This commit is contained in:
Reinhard Pointner 2015-04-19 04:36:29 +00:00
parent f1ad8c8cdf
commit 65e88be2b6
2 changed files with 11 additions and 2 deletions

View File

@ -10,6 +10,7 @@ import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
* A {@link Console} like class for when {@link System#console()} is not available in the current JVM
@ -18,6 +19,15 @@ import java.nio.charset.Charset;
*/
public class PseudoConsole {
private static PseudoConsole STDIO;
public static synchronized PseudoConsole getSystemConsole() {
if (STDIO == null) {
STDIO = new PseudoConsole(System.in, System.out, StandardCharsets.UTF_8);
}
return STDIO;
}
private final BufferedReader reader;
private final PrintWriter writer;

View File

@ -16,7 +16,6 @@ import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.StringWriter;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.EnumMap;
@ -190,7 +189,7 @@ public abstract class ScriptShellBaseClass extends Script {
// define global variable: console
public Object getConsole() {
return System.console() != null ? System.console() : new PseudoConsole(System.in, System.out, StandardCharsets.UTF_8);
return System.console() != null ? System.console() : PseudoConsole.getSystemConsole();
}
public Date getNow() {