Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Turn off" the output stream

I'm using a wayward library that, unfortunately, prints information to System.out (or occasionally System.err). What's the simplest way to prevent this?

I've been thinking about creating an output stream to memory, replace System.out and err before every call to one of the troublemaking methods, restore them later, and just ignore the buffer of the created stream. Is there an easier, more elegant way?

EDIT: I don't want to redirect all output - that's easily accomplished. I only want to ignore output potentially generated by certain library calls.

like image 786
Oak Avatar asked Nov 25 '10 21:11

Oak


1 Answers

I ended up doing something like:

PrintStream out = System.out;
System.setOut(new PrintStream(new OutputStream() {
    @Override public void write(int b) throws IOException {}
}));
try {
    <library call>
} finally {
    System.setOut(out);
}

Thanks to AlexR and stacker for redirecting me to this short solution.

like image 138
Oak Avatar answered Sep 18 '22 04:09

Oak