浏览代码

HADOOP-6224. Adds methods to read strings safely, makes the Buffer class in DataOutputBuffer public, and introduces public constructors there. These changes are required for MAPREDUCE-318. Contributed by Jothi Padmanabhan and Arun Murthy.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@809491 13f79535-47bb-0310-9956-ffa450edef68
Devaraj Das 16 年之前
父节点
当前提交
4c66882a69
共有 3 个文件被更改,包括 40 次插入1 次删除
  1. 4 0
      CHANGES.txt
  2. 11 1
      src/java/org/apache/hadoop/io/DataOutputBuffer.java
  3. 25 0
      src/java/org/apache/hadoop/io/Text.java

+ 4 - 0
CHANGES.txt

@@ -507,6 +507,10 @@ Trunk (unreleased changes)
 
 
     HADOOP-6184. Provide an API to dump Configuration in a JSON format.
     HADOOP-6184. Provide an API to dump Configuration in a JSON format.
     (V.V.Chaitanya Krishna via yhemanth)
     (V.V.Chaitanya Krishna via yhemanth)
+
+    HADOOP-6224. Adds methods to read strings safely, makes the Buffer class
+    in DataOutputBuffer public, and introduces public constructors there. These changes
+    are required for MAPREDUCE-318. (Jothi Padmanabhan and Arun Murthy via ddas)
  
  
   OPTIMIZATIONS
   OPTIMIZATIONS
 
 

+ 11 - 1
src/java/org/apache/hadoop/io/DataOutputBuffer.java

@@ -20,6 +20,8 @@ package org.apache.hadoop.io;
 
 
 import java.io.*;
 import java.io.*;
 
 
+import org.apache.hadoop.io.DataOutputBuffer.Buffer;
+
 /** A reusable {@link DataOutput} implementation that writes to an in-memory
 /** A reusable {@link DataOutput} implementation that writes to an in-memory
  * buffer.
  * buffer.
  *
  *
@@ -41,7 +43,7 @@ import java.io.*;
  */
  */
 public class DataOutputBuffer extends DataOutputStream {
 public class DataOutputBuffer extends DataOutputStream {
 
 
-  private static class Buffer extends ByteArrayOutputStream {
+  public static class Buffer extends ByteArrayOutputStream {
     public byte[] getData() { return buf; }
     public byte[] getData() { return buf; }
     public int getLength() { return count; }
     public int getLength() { return count; }
 
 
@@ -53,6 +55,10 @@ public class DataOutputBuffer extends DataOutputStream {
       super(size);
       super(size);
     }
     }
     
     
+    public Buffer(byte[] buf) {
+      super.buf = buf;
+    }
+
     public void write(DataInput in, int len) throws IOException {
     public void write(DataInput in, int len) throws IOException {
       int newcount = count + len;
       int newcount = count + len;
       if (newcount > buf.length) {
       if (newcount > buf.length) {
@@ -76,6 +82,10 @@ public class DataOutputBuffer extends DataOutputStream {
     this(new Buffer(size));
     this(new Buffer(size));
   }
   }
   
   
+  public DataOutputBuffer(byte[] buf) {
+    this(new Buffer(buf));
+  }
+
   private DataOutputBuffer(Buffer buffer) {
   private DataOutputBuffer(Buffer buffer) {
     super(buffer);
     super(buffer);
     this.buffer = buffer;
     this.buffer = buffer;

+ 25 - 0
src/java/org/apache/hadoop/io/Text.java

@@ -35,6 +35,7 @@ import java.util.Arrays;
 
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.io.WritableUtils;
 
 
 /** This class stores text using standard UTF8 encoding.  It provides methods
 /** This class stores text using standard UTF8 encoding.  It provides methods
  * to serialize, deserialize, and compare texts at byte level.  The type of
  * to serialize, deserialize, and compare texts at byte level.  The type of
@@ -403,6 +404,30 @@ public class Text extends BinaryComparable
     in.readFully(bytes, 0, length);
     in.readFully(bytes, 0, length);
     return decode(bytes);
     return decode(bytes);
   }
   }
+  /**
+   * Read a string, but check it for sanity. The format consists of a vint
+   * followed by the given number of bytes.
+   * @param in the stream to read from
+   * @param maxLength the largest acceptable length of string
+   * @return the bytes as a string
+   * @throws IOException if reading from the DataInput fails
+   * @throws IllegalArgumentException if the string length is negative or 
+   *         larger than maxSize. Only the vint is read.
+   */
+  public static String readStringSafely(DataInput in,
+                                        int maxLength
+                                        ) throws IOException, 
+                                                 IllegalArgumentException {
+    int length = WritableUtils.readVInt(in);
+    if (length < 0 || length > maxLength) {
+      throw new IllegalArgumentException("String size was " + length + 
+                                         ", which is outside of 0.." +
+                                         maxLength);
+    }
+    byte [] bytes = new byte[length];
+    in.readFully(bytes, 0, length);
+    return decode(bytes);
+  }
 
 
   /** Write a UTF8 encoded string to out
   /** Write a UTF8 encoded string to out
    */
    */