Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

stdio streams may now be set at a single place in ERT#setXxxStream() …

…or ERT#set_stdio()
  • Loading branch information...
commit 9abc3cc3472e33356304f1b2f80d1c0fe4fe47b0 1 parent d73bbb5
@jetztgradnet authored
View
74 src/main/java/erjang/ERT.java
@@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
@@ -1042,15 +1043,56 @@ public static void shutdown() {
}
}
- static public InputStream orig_in = System.in;
- static public PrintStream orig_out = System.out;
- static public PrintStream orig_err = System.err;
+ static protected volatile InputStream in = System.in;
+ static protected volatile PrintStream out = System.out;
+ static protected volatile PrintStream err = System.err;
public static void set_stdio(InputStream in,
PrintStream out, PrintStream err) {
- System.setIn(in);
- System.setOut(out);
- System.setErr(err);
+ // avoid null
+ ERT.in = (in != null ? in : new NullInputStream());
+ ERT.out = (out != null ? out : new PrintStream(new NullOutputStream()));
+ ERT.err = (err != null ? err : new PrintStream(new NullOutputStream()));
+ }
+
+ public static void set_stdio(InputStream in,
+ OutputStream out, OutputStream err) {
+ // avoid null
+ ERT.in = (in != null ? in : new NullInputStream());
+ ERT.out = (out != null ? new PrintStream(out) : new PrintStream(new NullOutputStream()));
+ ERT.err = (err != null ? new PrintStream(err) : new PrintStream(new NullOutputStream()));
+ }
+
+ public static void setInputStream(InputStream in) {
+ ERT.in = (in != null ? in : new NullInputStream());
+ }
+
+ public static InputStream getInputStream() {
+ return in;
+ }
+
+ public static void setOutputStream(PrintStream out) {
+ ERT.out = (out != null ? out : new PrintStream(new NullOutputStream()));
+ }
+
+ public static void setOutputStream(OutputStream out) {
+ ERT.out = (out != null ? new PrintStream(out) : new PrintStream(new NullOutputStream()));
+ }
+
+ public static PrintStream getOutputStream() {
+ return out;
+ }
+
+ public static void setErrorStream(PrintStream err) {
+ ERT.err = (err != null ? err : new PrintStream(new NullOutputStream()));
+ }
+
+ public static void setErrorStream(OutputStream err) {
+ ERT.err = (err != null ? new PrintStream(err) : new PrintStream(new NullOutputStream()));
+ }
+
+ public static PrintStream getErrorStream() {
+ return err;
}
public static File newFile(String file_name) {
@@ -1070,4 +1112,24 @@ public static void debug(boolean condition, String text) {
}
}
+ static class NullInputStream extends InputStream {
+ @Override
+ public int read() throws IOException {
+ return -1;
+ }
+ }
+ static class NullOutputStream extends OutputStream {
+ @Override
+ public void write(int arg0) throws IOException {
+ // ignore
+ }
+ @Override
+ public void write(byte[] b) throws IOException {
+ // ignore
+ }
+ @Override
+ public void write(byte[] b, int off, int len) throws IOException {
+ // ignore
+ }
+ }
}
View
3  src/main/java/erjang/console/TTYTextAreaDriverControl.java
@@ -47,6 +47,7 @@
import erjang.EHandle;
import erjang.EObject;
import erjang.EPID;
+import erjang.ERT;
import erjang.EString;
import erjang.driver.EDriverInstance;
import erjang.driver.IO;
@@ -157,7 +158,7 @@ public void replace(DocumentFilter.FilterBypass fb,
}
public InputStream getInputStream() {
- return System.in;
+ return ERT.getInputStream();
}
public OutputStream getOutputStream() {
View
6 src/main/java/erjang/driver/FDDriverInstance.java
@@ -60,15 +60,15 @@ public FDDriverInstance(int in, int out) {
this.out = out;
if (in == 0) {
- ins = System.in;
+ ins = ERT.getInputStream();
}
if (out == 1) {
- outs = System.out;
+ outs = ERT.getOutputStream();
}
if (out == 2) {
- outs = System.err;
+ outs = ERT.getErrorStream();
}
// that's it.
}
View
2  src/main/java/erjang/m/erlang/ErlProc.java
@@ -119,7 +119,7 @@ public static EObject process_info(EObject pid) {
@BIF
public static EObject display(EProc proc, EObject obj) {
- System.out.println(obj);
+ ERT.getOutputStream().println(obj);
return ERT.TRUE;
}
View
4 src/main/java/erjang/util/Progress.java
@@ -21,6 +21,8 @@
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
+import erjang.ERT;
+
public class Progress {
static ProgressListener listener;
@@ -47,7 +49,7 @@ static public void activity(String string) {
}
if (!Boolean.parseBoolean(suppress)) {
- System.err.write(wheel[next % 4]);
+ ERT.getErrorStream().write(wheel[next % 4]);
}
} catch (IOException e) {
// ignore
Please sign in to comment.
Something went wrong with that request. Please try again.