Review Board 1.7.22


Add copyBytes method to Text and BytesWritable to improve usability

Review Request #182 - Created Dec. 15, 2010 and updated

Owen O'Malley
trunk
https://issues.apache.org/jira/browse/HADOOP-6298
Reviewers
hadoop-common
hadoop-common
Users would like a method that returned byte arrays of precisely the right length, even if it requires a data copy.
Added unit tests and passed Hudson
http://svn.apache.org/repos/asf/hadoop/common/trunk/src/java/org/apache/hadoop/io/BytesWritable.java
Revision 1049968 New Change
[20] 19 lines
[+20]
20

    
   
20

   
21
import java.io.IOException;
21
import java.io.IOException;
22
import java.io.DataInput;
22
import java.io.DataInput;
23
import java.io.DataOutput;
23
import java.io.DataOutput;
24

    
   
24

   
25
import org.apache.commons.logging.Log;

   
26
import org.apache.commons.logging.LogFactory;

   
27
import org.apache.hadoop.classification.InterfaceAudience;
25
import org.apache.hadoop.classification.InterfaceAudience;
28
import org.apache.hadoop.classification.InterfaceStability;
26
import org.apache.hadoop.classification.InterfaceStability;
29

    
   
27

   
30
/** 
28
/** 
31
 * A byte sequence that is usable as a key or value.
29
 * A byte sequence that is usable as a key or value.
32
 * It is resizable and distinguishes between the size of the seqeunce and
30
 * It is resizable and distinguishes between the size of the seqeunce and
33
 * the current capacity. The hash function is the front of the md5 of the 
31
 * the current capacity. The hash function is the front of the md5 of the 
34
 * buffer. The sort order is the same as memcmp.
32
 * buffer. The sort order is the same as memcmp.
35
 */
33
 */
36
@InterfaceAudience.Public
34
@InterfaceAudience.Public
37
@InterfaceStability.Stable
35
@InterfaceStability.Stable
38
public class BytesWritable extends BinaryComparable
36
public class BytesWritable extends BinaryComparable
39
    implements WritableComparable<BinaryComparable> {
37
    implements WritableComparable<BinaryComparable> {
40
  private static final Log LOG = LogFactory.getLog(BytesWritable.class);

   
41
  private static final int LENGTH_BYTES = 4;
38
  private static final int LENGTH_BYTES = 4;
42
  private static final byte[] EMPTY_BYTES = {};
39
  private static final byte[] EMPTY_BYTES = {};
43

    
   
40

   
44
  private int size;
41
  private int size;
45
  private byte[] bytes;
42
  private byte[] bytes;
[+20] [20] 11 lines
[+20] public class BytesWritable extends BinaryComparable
57
    this.bytes = bytes;
54
    this.bytes = bytes;
58
    this.size = bytes.length;
55
    this.size = bytes.length;
59
  }
56
  }
60
  
57
  
61
  /**
58
  /**
62
   * Get the data from the BytesWritable.
59
   * Get a copy of the bytes that is exactly the length of the data.

    
   
60
   */

    
   
61
  public byte[] copyBytes() {

    
   
62
    byte[] result = new byte[size];

    
   
63
    System.arraycopy(bytes, 0, result, 0, size);

    
   
64
    return result;

    
   
65
  }

    
   
66
  

    
   
67
  /**

    
   
68
   * Get the data backing the BytesWritable. Please use copyBytes() if you

    
   
69
   * need the returned array to be the precise length of the data.
63
   * @return The data is only valid between 0 and getLength() - 1.
70
   * @return The data is only valid between 0 and getLength() - 1.
64
   */
71
   */
65
  public byte[] getBytes() {
72
  public byte[] getBytes() {
66
    return bytes;
73
    return bytes;
67
  }
74
  }
[+20] [20] 153 lines
http://svn.apache.org/repos/asf/hadoop/common/trunk/src/java/org/apache/hadoop/io/Text.java
Revision 1049968 New Change
 
http://svn.apache.org/repos/asf/hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestBytesWritable.java
Revision 1049968 New Change
 
http://svn.apache.org/repos/asf/hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestText.java
Revision 1049968 New Change
 
  1. http://svn.apache.org/repos/asf/hadoop/common/trunk/src/java/org/apache/hadoop/io/BytesWritable.java: Loading...
  2. http://svn.apache.org/repos/asf/hadoop/common/trunk/src/java/org/apache/hadoop/io/Text.java: Loading...
  3. http://svn.apache.org/repos/asf/hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestBytesWritable.java: Loading...
  4. http://svn.apache.org/repos/asf/hadoop/common/trunk/src/test/core/org/apache/hadoop/io/TestText.java: Loading...