ScannerUtils cleanup

This commit is contained in:
Andrea Cavalli 2020-08-26 18:25:36 +02:00
parent 5a99ed366b
commit 6e7f8b97a2
2 changed files with 18 additions and 153 deletions

View File

@ -2,6 +2,7 @@ package it.tdlight.tdlight;
import it.tdlight.tdlib.NativeLog;
import it.tdlight.tdlight.utils.ObjectsUtils;
import java.io.PrintStream;
/**
* Interface for managing the internal logging of TDLib. By default TDLib writes logs to stderr or an OS specific log and uses a verbosity level of 5.
@ -27,7 +28,14 @@ public class Log {
* @return True on success, or false otherwise, i.e. if the file can't be opened for writing.
*/
public static boolean setFilePath(String filePath) {
return NativeLog.setFilePath(filePath);
PrintStream previousOut = System.out;
PrintStream previousErr = System.err;
try {
return NativeLog.setFilePath(filePath);
} finally {
System.setOut(previousOut);
System.setOut(previousErr);
}
}
/**

View File

@ -1,164 +1,21 @@
package it.tdlight.tdlight.utils;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.concurrent.locks.ReentrantLock;
public class ScannerUtils {
private static final Scanner scanner = new Scanner(System.in);
private static final ReentrantLock lock = new ReentrantLock();
private static final PrintStream emptyOut = new java.io.PrintStream(new java.io.OutputStream() {
@Override
public void write(int b) {
}
}) {
@Override
public void flush() {
}
private static final Scanner scanner;
private static final Object lock = new Object();
@Override
public void close() {
static {
synchronized (lock) {
scanner = new Scanner(System.in);
}
@Override
public void write(int b) {
}
@Override
public void write(byte[] b) {
}
@Override
public void write(byte[] buf, int off, int len) {
}
@Override
public void print(boolean b) {
}
@Override
public void print(char c) {
}
@Override
public void print(int i) {
}
@Override
public void print(long l) {
}
@Override
public void print(float f) {
}
@Override
public void print(double d) {
}
@Override
public void print(char[] s) {
}
@Override
public void print(String s) {
}
@Override
public void print(Object obj) {
}
@Override
public void println() {
}
@Override
public void println(boolean x) {
}
@Override
public void println(char x) {
}
@Override
public void println(int x) {
}
@Override
public void println(long x) {
}
@Override
public void println(float x) {
}
@Override
public void println(double x) {
}
@Override
public void println(char[] x) {
}
@Override
public void println(String x) {
}
@Override
public void println(Object x) {
}
@Override
public java.io.PrintStream printf(String format, Object... args) {
return this;
}
@Override
public java.io.PrintStream printf(java.util.Locale l, String format, Object... args) {
return this;
}
@Override
public java.io.PrintStream format(String format, Object... args) {
return this;
}
@Override
public java.io.PrintStream format(java.util.Locale l, String format, Object... args) {
return this;
}
@Override
public java.io.PrintStream append(CharSequence csq) {
return this;
}
@Override
public java.io.PrintStream append(CharSequence csq, int start, int end) {
return this;
}
@Override
public java.io.PrintStream append(char c) {
return this;
}
};
}
public static String askParameter(String displayName, String question) {
try {
lock.lock();
StringBuilder toPrint = new StringBuilder();
PrintStream oldOut = System.out;
PrintStream oldErr = System.err;
System.setOut(emptyOut);
System.setErr(emptyOut);
oldOut.print("[" + displayName + "] " + question + ": ");
String result = scanner.nextLine();
System.setOut(oldOut);
System.setErr(oldErr);
return result;
} finally {
lock.unlock();
synchronized (lock) {
System.out.print("[" + displayName + "] " + question + ": ");
return scanner.nextLine();
}
}
}