From a196443cb7665907ac64bab75c9506d2f0219df0 Mon Sep 17 00:00:00 2001 From: Andor Molnar Date: Wed, 9 May 2018 16:15:59 -0700 Subject: [PATCH] ZOOKEEPER-3039. Use the same Scanner for all queries --- .../server/persistence/TxnLogToolkit.java | 30 +++++++++---------- .../server/persistence/TxnLogToolkitTest.java | 3 +- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java b/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java index c6703eaed08..9d8b6849efa 100644 --- a/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java +++ b/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java @@ -98,7 +98,7 @@ Options getOptions() { */ public static void main(String[] args) throws Exception { try (final TxnLogToolkit lt = parseCommandLine(args)) { - lt.dump(new InputStreamReader(System.in)); + lt.dump(new Scanner(System.in)); lt.printStat(); } catch (TxnLogToolkitParseException e) { System.err.println(e.getMessage() + "\n"); @@ -131,7 +131,7 @@ public TxnLogToolkit(boolean recoveryMode, boolean verbose, String txnLogFileNam } } - public void dump(Reader input) throws Exception { + public void dump(Scanner scanner) throws Exception { crcFixed = 0; FileHeader fhdr = new FileHeader(); @@ -172,7 +172,7 @@ public void dump(Reader input) throws Exception { if (recoveryMode) { if (!force) { printTxn(bytes, "CRC ERROR"); - if (askForFix(input)) { + if (askForFix(scanner)) { crcValue = crc.getValue(); ++crcFixed; } @@ -201,19 +201,17 @@ public void dump(Reader input) throws Exception { } } - private boolean askForFix(Reader input) throws TxnLogToolkitException { - try (Scanner scanner = new Scanner(input)) { - while (true) { - System.out.print("Would you like to fix it (Yes/No/Abort) ? "); - char answer = Character.toUpperCase(scanner.next().charAt(0)); - switch (answer) { - case 'Y': - return true; - case 'N': - return false; - case 'A': - throw new TxnLogToolkitException(0, "Recovery aborted."); - } + private boolean askForFix(Scanner scanner) throws TxnLogToolkitException { + while (true) { + System.out.print("Would you like to fix it (Yes/No/Abort) ? "); + char answer = Character.toUpperCase(scanner.next().charAt(0)); + switch (answer) { + case 'Y': + return true; + case 'N': + return false; + case 'A': + throw new TxnLogToolkitException(0, "Recovery aborted."); } } } diff --git a/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java b/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java index dd34fa5a9b1..ed47c2e1e75 100644 --- a/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java +++ b/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.PrintStream; import java.io.StringReader; +import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -138,7 +139,7 @@ public void testRecoveryInteractiveMode() throws Exception { TxnLogToolkit lt = new TxnLogToolkit(true, false, logfile.toString(), false); // Act - lt.dump(new StringReader("y\n")); + lt.dump(new Scanner("y\n")); // Assert String output = outContent.toString();