From 6e7f8b97a2e107af251ad14b59d09b367f68c369 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Wed, 26 Aug 2020 18:25:36 +0200 Subject: [PATCH] ScannerUtils cleanup --- src/main/java/it/tdlight/tdlight/Log.java | 10 +- .../tdlight/tdlight/utils/ScannerUtils.java | 161 +----------------- 2 files changed, 18 insertions(+), 153 deletions(-) diff --git a/src/main/java/it/tdlight/tdlight/Log.java b/src/main/java/it/tdlight/tdlight/Log.java index 4f45ad0..e982d63 100644 --- a/src/main/java/it/tdlight/tdlight/Log.java +++ b/src/main/java/it/tdlight/tdlight/Log.java @@ -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); + } } /** diff --git a/src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java b/src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java index 31609be..acc4fa0 100644 --- a/src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java +++ b/src/main/java/it/tdlight/tdlight/utils/ScannerUtils.java @@ -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(); } } }