Skip to content

Commit bb716f2

Browse files
committed
HADOOP-6298. Add copyBytes to Text and BytesWritable. (omalley)
git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1050070 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0c48e0b commit bb716f2

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

CHANGES.txt

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ Release 0.22.0 - Unreleased
211211
HADOOP-7009. MD5Hash provides a public factory method that creates an
212212
instance of thread local MessageDigest. (hairong)
213213

214-
HADOOP-7008. Enable test-patch.sh to have a configured number of acceptable
215-
findbugs and javadoc warnings. (nigel and gkesavan)
214+
HADOOP-7008. Enable test-patch.sh to have a configured number of
215+
acceptable findbugs and javadoc warnings. (nigel and gkesavan)
216216

217217
HADOOP-6818. Provides a JNI implementation of group resolution. (ddas)
218218

@@ -227,17 +227,22 @@ Release 0.22.0 - Unreleased
227227
HADOOP-7024. Create a test method for adding file systems during tests.
228228
(Kan Zhang via jghoman)
229229

230-
HADOOP-6903. Make AbstractFSileSystem methods and some FileContext methods to be public. (Sanjay Radia via Sanjay Radia)
230+
HADOOP-6903. Make AbstractFSileSystem methods and some FileContext methods
231+
to be public. (Sanjay Radia)
231232

232-
HADOOP-7034. Add TestPath tests to cover dot, dot dot, and slash normalization. (eli)
233+
HADOOP-7034. Add TestPath tests to cover dot, dot dot, and slash
234+
normalization. (eli)
233235

234236
HADOOP-7032. Assert type constraints in the FileStatus constructor. (eli)
235237

236-
HADOOP-6562. FileContextSymlinkBaseTest should use FileContextTestHelper. (eli)
238+
HADOOP-6562. FileContextSymlinkBaseTest should use FileContextTestHelper.
239+
(eli)
237240

238241
HADOOP-7028. ant eclipse does not include requisite ant.jar in the
239242
classpath. (Patrick Angeles via eli)
240243

244+
HADOOP-6298. Add copyBytes to Text and BytesWritable. (omalley)
245+
241246
OPTIMIZATIONS
242247

243248
HADOOP-6884. Add LOG.isDebugEnabled() guard for each LOG.debug(..).

src/java/org/apache/hadoop/io/BytesWritable.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.io.DataInput;
2323
import java.io.DataOutput;
2424

25-
import org.apache.commons.logging.Log;
26-
import org.apache.commons.logging.LogFactory;
2725
import org.apache.hadoop.classification.InterfaceAudience;
2826
import org.apache.hadoop.classification.InterfaceStability;
2927

@@ -37,7 +35,6 @@
3735
@InterfaceStability.Stable
3836
public class BytesWritable extends BinaryComparable
3937
implements WritableComparable<BinaryComparable> {
40-
private static final Log LOG = LogFactory.getLog(BytesWritable.class);
4138
private static final int LENGTH_BYTES = 4;
4239
private static final byte[] EMPTY_BYTES = {};
4340

@@ -59,7 +56,18 @@ public BytesWritable(byte[] bytes) {
5956
}
6057

6158
/**
62-
* Get the data from the BytesWritable.
59+
* Get a copy of the bytes that is exactly the length of the data.
60+
* See {@link #getBytes()} for faster access to the underlying array.
61+
*/
62+
public byte[] copyBytes() {
63+
byte[] result = new byte[size];
64+
System.arraycopy(bytes, 0, result, 0, size);
65+
return result;
66+
}
67+
68+
/**
69+
* Get the data backing the BytesWritable. Please use {@link #copyBytes()}
70+
* if you need the returned array to be precisely the length of the data.
6371
* @return The data is only valid between 0 and getLength() - 1.
6472
*/
6573
public byte[] getBytes() {

src/java/org/apache/hadoop/io/Text.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535

3636
import org.apache.avro.reflect.Stringable;
3737

38-
import org.apache.commons.logging.Log;
39-
import org.apache.commons.logging.LogFactory;
4038
import org.apache.hadoop.classification.InterfaceAudience;
4139
import org.apache.hadoop.classification.InterfaceStability;
4240

@@ -54,7 +52,6 @@
5452
@InterfaceStability.Stable
5553
public class Text extends BinaryComparable
5654
implements WritableComparable<BinaryComparable> {
57-
private static final Log LOG= LogFactory.getLog(Text.class);
5855

5956
private static ThreadLocal<CharsetEncoder> ENCODER_FACTORY =
6057
new ThreadLocal<CharsetEncoder>() {
@@ -100,9 +97,20 @@ public Text(byte[] utf8) {
10097
set(utf8);
10198
}
10299

100+
/**
101+
* Get a copy of the bytes that is exactly the length of the data.
102+
* See {@link #getBytes()} for faster access to the underlying array.
103+
*/
104+
public byte[] copyBytes() {
105+
byte[] result = new byte[length];
106+
System.arraycopy(bytes, 0, result, 0, length);
107+
return result;
108+
}
109+
103110
/**
104111
* Returns the raw bytes; however, only data up to {@link #getLength()} is
105-
* valid.
112+
* valid. Please use {@link #copyBytes()} if you
113+
* need the returned array to be precisely the length of the data.
106114
*/
107115
public byte[] getBytes() {
108116
return bytes;

src/test/core/org/apache/hadoop/io/TestBytesWritable.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public void testSizeChange() throws Exception {
4040
for(int i=0; i < size*2; ++i) {
4141
assertEquals(hadoop[i%size], buf.getBytes()[i]);
4242
}
43+
// ensure that copyBytes is exactly the right length
44+
assertEquals(size*4, buf.copyBytes().length);
4345
// shrink the buffer
4446
buf.setCapacity(1);
4547
// make sure the size has been cut down too

src/test/core/org/apache/hadoop/io/TestText.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ public void testTextText() throws CharacterCodingException {
215215
a.append("xdefgxxx".getBytes(), 1, 4);
216216
assertEquals("modified aliased string", "abc", b.toString());
217217
assertEquals("appended string incorrectly", "abcdefg", a.toString());
218+
// add an extra byte so that capacity = 14 and length = 8
219+
a.append(new byte[]{'d'}, 0, 1);
220+
assertEquals(14, a.getBytes().length);
221+
assertEquals(8, a.copyBytes().length);
218222
}
219223

220224
private class ConcurrentEncodeDecodeThread extends Thread {

0 commit comments

Comments
 (0)