Bladeren bron

Merge -c 1299142 from branch-1 to branch-1.0 to fix MAPREDUCE-764. Fix TypedBytesInput.readRaw to preserve custom type codes. Contributed by Klaas Bosteels.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-1.0@1299143 13f79535-47bb-0310-9956-ffa450edef68
Arun Murthy 13 jaren geleden
bovenliggende
commit
c42c816f34

+ 3 - 0
CHANGES.txt

@@ -31,6 +31,9 @@ Release 1.0.2 - unreleased
     HDFS-3006. In WebHDFS, when the return body is empty, set the Content-Type
     to application/octet-stream instead of application/json.  (szetszwo)
 
+    MAPREDUCE-764. Fix TypedBytesInput.readRaw to preserve custom type codes.  
+    (Klaas Bosteels via acmurthy) 
+
 Release 1.0.1 - 2012.02.14
 
   NEW FEATURES

+ 14 - 4
src/contrib/streaming/src/java/org/apache/hadoop/typedbytes/TypedBytesInput.java

@@ -149,7 +149,7 @@ public class TypedBytesInput {
     } else if (code == Type.MARKER.code) {
       return null;
     } else if (50 <= code && code <= 200) { // application-specific typecodes
-      return readRawBytes();
+      return readRawBytes(code);
     } else {
       throw new RuntimeException("unknown type");
     }
@@ -202,14 +202,15 @@ public class TypedBytesInput {
   }
 
   /**
-   * Reads the raw bytes following a <code>Type.BYTES</code> code.
+   * Reads the raw bytes following a custom code.
+   * @param code the custom type code
    * @return the obtained bytes sequence
    * @throws IOException
    */
-  public byte[] readRawBytes() throws IOException {
+  public byte[] readRawBytes(int code) throws IOException {
     int length = in.readInt();
     byte[] bytes = new byte[5 + length];
-    bytes[0] = (byte) Type.BYTES.code;
+    bytes[0] = (byte) code;
     bytes[1] = (byte) (0xff & (length >> 24));
     bytes[2] = (byte) (0xff & (length >> 16));
     bytes[3] = (byte) (0xff & (length >> 8));
@@ -217,6 +218,15 @@ public class TypedBytesInput {
     in.readFully(bytes, 5, length);
     return bytes;
   }
+  
+  /**
+   * Reads the raw bytes following a <code>Type.BYTES</code> code.
+   * @return the obtained bytes sequence
+   * @throws IOException
+   */
+  public byte[] readRawBytes() throws IOException {
+    return readRawBytes(Type.BYTES.code);
+  }
 
   /**
    * Reads the byte following a <code>Type.BYTE</code> code.

+ 19 - 0
src/contrib/streaming/src/test/org/apache/hadoop/typedbytes/TestIO.java

@@ -27,6 +27,7 @@ import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -125,6 +126,24 @@ public class TestIO extends TestCase {
     istream.close();
   }
 
+  public void testCustomTypesIO() throws IOException {
+    byte[] rawBytes = new byte[] { 100, 0, 0, 0, 3, 1, 2, 3 };
+    
+    FileOutputStream ostream = new FileOutputStream(tmpfile);
+    DataOutputStream dostream = new DataOutputStream(ostream);
+    TypedBytesOutput out = new TypedBytesOutput(dostream);
+    out.writeRaw(rawBytes);
+    dostream.close();
+    ostream.close();
+
+    FileInputStream istream = new FileInputStream(tmpfile);
+    DataInputStream distream = new DataInputStream(istream);
+    TypedBytesInput in = new TypedBytesInput(distream);
+    assertTrue(Arrays.equals(rawBytes, in.readRaw()));
+    distream.close();
+    istream.close();
+  }
+  
   public void testRecordIO() throws IOException {
     RecRecord1 r1 = new RecRecord1();
     r1.setBoolVal(true);