From 3cddda03c95caad05decc95466f212a89d4e7eaa Mon Sep 17 00:00:00 2001 From: Reinhard Pointner Date: Tue, 30 Dec 2014 18:13:06 +0000 Subject: [PATCH] * improved support for non-standard console environments when using the scripting shell --- source/net/filebot/cli/PseudoConsole.java | 65 +++++++++++++++++++ .../net/filebot/cli/ScriptShellBaseClass.java | 6 +- 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 source/net/filebot/cli/PseudoConsole.java diff --git a/source/net/filebot/cli/PseudoConsole.java b/source/net/filebot/cli/PseudoConsole.java new file mode 100644 index 00000000..6bca04b1 --- /dev/null +++ b/source/net/filebot/cli/PseudoConsole.java @@ -0,0 +1,65 @@ +package net.filebot.cli; + +import java.io.BufferedReader; +import java.io.Console; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.Reader; +import java.nio.charset.Charset; + +/** + * A {@link Console} like class for when {@link System#console()} is not available in the current JVM + * + * @see Console + */ +public class PseudoConsole { + + private final BufferedReader reader; + private final PrintWriter writer; + + public PseudoConsole(InputStream in, PrintStream out, Charset cs) { + reader = new BufferedReader(new InputStreamReader(in, cs)); + writer = new PrintWriter(new OutputStreamWriter(out, cs), true); + } + + public PrintWriter writer() { + return writer; + } + + public Reader reader() { + return reader; + } + + public PrintWriter format(String fmt, Object... args) { + return writer.format(fmt, args); + } + + public PrintWriter printf(String format, Object... args) { + return writer.printf(format, args); + } + + public String readLine(String fmt, Object... args) { + throw new UnsupportedOperationException(); + } + + public String readLine() throws IOException { + return reader.readLine(); + } + + public char[] readPassword(String fmt, Object... args) { + throw new UnsupportedOperationException(); + } + + public char[] readPassword() { + throw new UnsupportedOperationException(); + } + + public void flush() { + writer.flush(); + } + +} diff --git a/source/net/filebot/cli/ScriptShellBaseClass.java b/source/net/filebot/cli/ScriptShellBaseClass.java index c7136da9..b365d47a 100644 --- a/source/net/filebot/cli/ScriptShellBaseClass.java +++ b/source/net/filebot/cli/ScriptShellBaseClass.java @@ -9,7 +9,6 @@ import groovy.lang.MissingPropertyException; import groovy.lang.Script; import groovy.xml.MarkupBuilder; -import java.io.Console; import java.io.File; import java.io.FileFilter; import java.io.IOException; @@ -17,6 +16,7 @@ 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; @@ -189,8 +189,8 @@ public abstract class ScriptShellBaseClass extends Script { } // define global variable: console - public Console getConsole() { - return System.console(); + public Object getConsole() { + return System.console() != null ? System.console() : new PseudoConsole(System.in, System.out, StandardCharsets.UTF_8); } public Date getNow() {