/** * Create a new stream set as {@link System} is currently configured. */ public static StreamSet system() { return new StreamSet(System.in, System.out, System.err); }
/** * Restores the System streams to the given pair and resets the hijacker state to un-installed. */ public static synchronized void restore(final StreamSet streams) { assert streams != null; StreamSet.system(streams); previous = null; installed = false; }
/** * Close all streams. */ public void close() throws IOException { Closer.close(in, out); // Only attempt to close the err stream if we aren't sharing it with out if (!streams.isOutputCombined()) { Closer.close(err); } }
private PrintStream get() { return current().getOutput(type); }
private InputStream get() { return current().getInput(); }
/** * Stop pumping the streams. */ public void stop() { if (outputThread != null) { try { outputThread.join(); } catch (InterruptedException e) { // ignore } } if (errorThread != null) { try { errorThread.join(); } catch (InterruptedException e) { // ignore } } if (inputPump != null) { inputPump.stop(); } streams.flush(); }
public void close() { Closer.close(in, out); if (!isOutputCombined()) { Closer.close(err); } }
/** * Flush both output streams. */ public void flush() { Flusher.flush(out); // Only attempt to flush the err stream if we aren't sharing it with out if (!streams.isOutputCombined()) { Flusher.flush(err); } }
/** * Register streams for the current thread. */ public static void register(final InputStream in, final PrintStream out, final PrintStream err) { ensureInstalled(); if (log.isTraceEnabled()) { log.trace("Registering: {} -> {}, {}, {}", new Object[] { Thread.currentThread(), in, out, err }); } StreamRegistration prev = registration(false); StreamSet set = new StreamSet(in, out, err); StreamRegistration next = new StreamRegistration(set, prev); registrations.set(next); }
/** * Helper which uses current values from {@link StreamSet#system}. */ public IO() { this(StreamSet.system(), true); }
public void flush() { Flusher.flush(out); if (!isOutputCombined()) { Flusher.flush(err); } }
private static StreamSet ansiStreams(final StreamSet streams) { assert streams != null; return new StreamSet(streams.in, wrap(streams.out), wrap(streams.err)); }
public AnsiIO() { this(StreamSet.system(), true); }
public IO(final StreamSet streams, final boolean autoFlush) { assert streams != null; this.streams = streams; this.in = createReader(streams.in); this.out = createWriter(streams.out, autoFlush); /// Don't rewrite the error stream if we have the same stream for out and error if (streams.isOutputCombined()) { this.err = this.out; } else { this.err = createWriter(streams.err, autoFlush); } }
/** * Install the hijacker. */ public static synchronized void install() { if (installed) { throw new IllegalStateException("Already installed"); } // Capture the current set of streams previous = new StreamSet(System.in, System.out, System.err); // Install our streams System.setIn(new DelegateInputStream()); System.setOut(new DelegateOutputStream(StreamSet.OutputType.OUT)); System.setErr(new DelegateOutputStream(StreamSet.OutputType.ERR)); installed = true; log.debug("Installed"); }
/** * Restores the original System streams from {@link StreamSet#SYSTEM} and resets * the hijacker state to un-installed. */ public static synchronized void restore() { restore(StreamSet.system()); }
public Console(final IO io, final Callable<ConsoleTask> taskFactory, final History history, final InputStream bindings) throws IOException { assert io != null; // history could be null // bindings could be null this.pipe = new InputPipe(io.streams, io.getTerminal(), new InputPipe.InterruptHandler() { public boolean interrupt() throws Exception { return interruptTask(); } public boolean stop() throws Exception { return false; } }); this.pipe.setName("Console InputPipe"); this.pipe.setDaemon(true); // Setup a new IO w/our pipe input stream & rebuilding the input reader this.io = new IO(new StreamSet(pipe.getInputStream(), io.streams.out, io.streams.err), null, io.out, io.err, true); this.reader = new ConsoleReader( this.io.streams.in, this.io.out, bindings, io.getTerminal()); this.reader.setPaginationEnabled(true); this.reader.setCompletionHandler(new CandidateListCompletionHandler()); this.reader.setHistory(history != null ? history : new MemoryHistory()); assert taskFactory != null; this.taskFactory = taskFactory; }