Dynamically run java code with Process

泄露秘密 提交于 2019-12-09 06:51:27
  • Take a backup of the existing outputstream.

PrintStream realSystemOut = System.out;

  • Set it to other outputstream [ fileOutputStream, or some other streams] PrintStream overridePrintStream = new PrintStream(new FileOutputStream("log.txt"));

    System.setOut(overridePrintStream );

    ----- process -----

  • place the actual stream back to System.out

    System.setOut(realSystemOut);

Thanks

Java allows you to supply your own PrintStream to override stdout and stderr and a InputStream for stdin.

Personally, I don't like simply throwing away the original stream, cause I tend to only want to redirect or parse it, not stop it (although you could do that as well).

Here is a simple example of the idea...

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

public class RedirectStdOut {

    public static void main(String[] args) {

        Consumer stdConsumer = new Consumer() {

            @Override
            public void processLine(StreamCapturer capturer, String text) {
            }

            @Override
            public void processCharacter(StreamCapturer capturer, char character) {
                capturer.getParent().print(character);
            }
        };

        StreamCapturer stdout = new StreamCapturer(stdConsumer, System.out);
        StreamCapturer stderr = new StreamCapturer(stdConsumer, System.err);

        System.setOut(new PrintStream(stdout));
        System.setErr(new PrintStream(stderr));

        System.out.println("This is a test");
        System.err.println("This is an err");

    }

    public static interface Consumer {

        public void processLine(StreamCapturer capturer, String text);

        public void processCharacter(StreamCapturer capturer, char character);
    }

    public static class StreamCapturer extends OutputStream {

        private StringBuilder buffer;
        private Consumer consumer;
        private PrintStream parent;
        private boolean echo = false;

        public StreamCapturer(Consumer consumer, PrintStream parent) {
            buffer = new StringBuilder(128);
            this.parent = parent;
            this.consumer = consumer;
        }

        public PrintStream getParent() {
            return parent;
        }

        public boolean shouldEcho() {
            return echo;
        }

        public void setEcho(boolean echo) {
            this.echo = echo;
        }

        @Override
        public void write(int b) throws IOException {
            char c = (char) b;
            String value = Character.toString(c);
            buffer.append(value);
            if (value.equals("\n")) {
                consumer.processLine(this, value);
                buffer.delete(0, buffer.length());
            }
            consumer.processCharacter(this, c);
            if (shouldEcho()) {
                parent.print(c);
            }
        }
    }

}

Now the StreamCapturer has the ability to echo the output if you want, I've turned it off to demonstrate the use of the Consumer. I would normally use the Consumer to process what is coming through the stream, based on your needs, you can wait for the complete line or process the individual characters...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!