ru.opensecreto.jconsole.StreamConsoleAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jconsole Show documentation
Show all versions of jconsole Show documentation
Java console adapter. Use console even if System.console()==null
The newest version!
package ru.opensecreto.jconsole;
import java.io.*;
public class StreamConsoleAdapter implements ConsoleAdapter {
private final PrintWriter out;
private final BufferedReader in;
public StreamConsoleAdapter(PrintStream out, InputStream in) {
this.out = new PrintWriter(out);
this.in = new BufferedReader(new InputStreamReader(in));
}
@Override
public PrintWriter writer() {
return out;
}
@Override
public Reader reader() {
return in;
}
@Override
public ConsoleAdapter format(String fmt, Object... args) {
out.format(fmt, args).flush();
return this;
}
@Override
public ConsoleAdapter printf(String format, Object... args) {
out.printf(format, args).flush();
return this;
}
@Override
public String readLine(String fmt, Object... args) throws IOException {
out.printf(fmt, args).flush();
return in.readLine();
}
@Override
public String readLine() throws IOException {
return in.readLine();
}
@Override
public char[] readPassword(String fmt, Object... args) throws IOException {
out.printf(fmt, args).flush();
return in.readLine().toCharArray();
}
@Override
public char[] readPassword() throws IOException {
return in.readLine().toCharArray();
}
@Override
public void flush() {
out.flush();
}
}