Ensure streams are closed and use utf8 encoding.

Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo
Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/d2660591
Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/d2660591
Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/d2660591

Branch: refs/heads/2292-findbugs
Commit: d26605911fcecaaa95af9650920055886b131b6b
Parents: 10bd1d3
Author: Josh Elser <els...@apache.org>
Authored: Fri Jan 31 12:35:54 2014 -0500
Committer: Josh Elser <els...@apache.org>
Committed: Fri Jan 31 22:18:52 2014 -0500

----------------------------------------------------------------------
 .../util/shell/commands/HistoryCommand.java     | 33 +++++++++++++-------
 1 file changed, 22 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/accumulo/blob/d2660591/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java
----------------------------------------------------------------------
diff --git 
a/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java
 
b/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java
index 6de8c39..43dc64f 100644
--- 
a/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java
+++ 
b/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java
@@ -17,12 +17,16 @@
 package org.apache.accumulo.core.util.shell.commands;
 
 import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 
+import org.apache.accumulo.core.Constants;
 import org.apache.accumulo.core.util.shell.Shell;
 import org.apache.accumulo.core.util.shell.Shell.Command;
 import org.apache.commons.cli.CommandLine;
@@ -43,22 +47,25 @@ public class HistoryCommand extends Command {
     int counter = 0;
     
     if (cl.hasOption(clearHist.getOpt())) {
-      
+      PrintWriter out = null;
       try {
-        
-        final FileWriter outFile = new FileWriter(histDir + 
"/shell_history.txt");
-        final PrintWriter out = new PrintWriter(outFile);
-        out.close();
-        
-      } catch (IOException e) {
-        
+        FileOutputStream file = new FileOutputStream(histDir + 
"/shell_history.txt");
+        final BufferedWriter fileWriter = new BufferedWriter(new 
OutputStreamWriter(file, Constants.UTF8));
+        out = new PrintWriter(fileWriter);
+      } catch (FileNotFoundException e) { 
         e.printStackTrace();
+      } finally {
+        // If the file existed, closing the 
+        if (null != out) {
+          out.close();
+        }
       }
     }
     
     else {
+      BufferedReader in = null;
       try {
-        final BufferedReader in = new BufferedReader(new FileReader(histDir + 
"/shell_history.txt"));
+        in = new BufferedReader(new InputStreamReader(new 
FileInputStream(histDir + "/shell_history.txt"), Constants.UTF8));
         String Line;
         try {
           Line = in.readLine();
@@ -74,6 +81,10 @@ public class HistoryCommand extends Command {
       } catch (FileNotFoundException e) {
         
         e.printStackTrace();
+      } finally {
+        if (null != in) {
+          in.close();
+        }
       }
     }
     

Reply via email to