Sfoglia il codice sorgente

HADOOP-628. Fix a problem with 'fs -cat' command. Contributed by Wendy.

git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@492276 13f79535-47bb-0310-9956-ffa450edef68
Doug Cutting 18 anni fa
parent
commit
4427f66fbb
2 ha cambiato i file con 37 aggiunte e 35 eliminazioni
  1. 3 0
      CHANGES.txt
  2. 34 35
      src/java/org/apache/hadoop/fs/FsShell.java

+ 3 - 0
CHANGES.txt

@@ -168,6 +168,9 @@ Trunk (unreleased changes)
 47. HADOOP-525.  Add raw comparators to record types.  This greatly
     improves record sort performance.  (Milind Bhandarkar via cutting)
 
+48. HADOOP-628.  Fix a problem with 'fs -cat' command, where some
+    characters were replaced with question marks.  (Wendy Chien via cutting)
+
 
 Release 0.9.2 - 2006-12-15
 

+ 34 - 35
src/java/org/apache/hadoop/fs/FsShell.java

@@ -39,6 +39,22 @@ public class FsShell extends ToolBase {
         this.fs = FileSystem.get(conf);
     }
 
+    /**
+     * Copies from one stream to another.
+     */
+    private void copyBytes(InputStream in, OutputStream out) throws IOException {
+      PrintStream ps = out instanceof PrintStream ? (PrintStream)out : null;
+      byte buf[] = new byte[conf.getInt("io.file.buffer.size", 4096)];
+      int bytesRead = in.read(buf);
+      while (bytesRead >= 0) {
+        out.write(buf, 0, bytesRead);
+        if ((ps != null) && ps.checkError()) {
+          throw new IOException("Unable to write to output stream.");
+        }
+        bytesRead = in.read(buf);
+      }
+    }
+      
     /**
      * Copies from stdin to the indicated file.
      */
@@ -50,18 +66,29 @@ public class FsShell extends ToolBase {
         throw new IOException("Target " + dst.toString() + " already exists.");
       }
       FSDataOutputStream out = fs.create(dst); 
-      byte buf[] = new byte[conf.getInt("io.file.buffer.size", 4096)];
       try {
-        int bytesRead = System.in.read(buf);
-        while (bytesRead >= 0) {
-          out.write(buf, 0, bytesRead);
-          bytesRead = System.in.read(buf);
-        }
+        copyBytes(System.in, out);
       } finally {
         out.close();
       }
     }
 
+    /** 
+     * Print from src to stdout.
+     */
+    private void printToStdout(Path src) throws IOException {
+      if (fs.isDirectory(src)) {
+        throw new IOException("Source must be a file.");
+      }
+      FSDataInputStream in = fs.open(src);
+      try {
+        copyBytes(in, System.out);
+      } finally {
+        in.close();
+      }
+
+    }
+
     /**
      * Add a local file to the indicated FileSystem name. src is kept.
      */
@@ -178,38 +205,10 @@ public class FsShell extends ToolBase {
     void cat(String srcf) throws IOException {
       Path [] srcs = fs.globPaths( new Path( srcf ) );
       for( int i=0; i<srcs.length; i++ ) {
-        cat(srcs[i]);
+        printToStdout(srcs[i]);
       }
     }
     
-
-    /* print the content of src to screen */
-    private void cat(Path src) throws IOException {
-      FSDataInputStream in = fs.open(src);
-      try {
-        BufferedReader din = new BufferedReader(new InputStreamReader(in));
-        String line;
-        int checkFactor = 0;
-        while((line = din.readLine()) != null) {
-          System.out.println(line);      
-
-          //
-          // Peridically check if the output encountered an error. This can
-          // happen if the output stream has been disconnected
-          //
-          if (checkFactor == 0) {
-            if (System.out.checkError()) {
-              throw new IOException("Unable to write to output stream");
-            }
-            checkFactor = 10000;
-          }
-          checkFactor--;
-        }
-      } finally {
-        in.close();
-      }
-    }
-
     /**
      * Parse the incoming command string
      * @param cmd