From 361d5ffc7ad8d20f292171b1dec994b5c5592d47 Mon Sep 17 00:00:00 2001 From: Andor Molnar Date: Thu, 10 May 2018 13:14:24 -0700 Subject: [PATCH] ZOOKEEPER-3039: TxnLogToolkit uses Scanner badly Fixed by creating a single Scanner for all queries in the main() method. Author: Andor Molnar Reviewers: phunt@apache.org Closes #517 from anmolnar/ZOOKEEPER-3039 and squashes the following commits: a35e2e280 [Andor Molnar] ZOOKEEPER-3039. Optimize imports a196443cb [Andor Molnar] ZOOKEEPER-3039. Use the same Scanner for all queries Change-Id: Icf66888d4e6ad902615ed8ffde58a5a8fdd41237 --- .../server/persistence/TxnLogToolkit.java | 32 ++++++++----------- .../server/persistence/TxnLogToolkitTest.java | 4 +-- 2 files changed, 16 insertions(+), 20 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..887d98bad8f 100644 --- a/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java +++ b/src/java/main/org/apache/zookeeper/server/persistence/TxnLogToolkit.java @@ -39,8 +39,6 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStreamReader; -import java.io.Reader; import java.text.DateFormat; import java.util.Date; import java.util.Scanner; @@ -98,7 +96,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 +129,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 +170,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 +199,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..79a69c4e921 100644 --- a/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java +++ b/src/java/test/org/apache/zookeeper/server/persistence/TxnLogToolkitTest.java @@ -29,7 +29,7 @@ import java.io.FileNotFoundException; 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 +138,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();