Author: cutting
Date: Fri Mar 2 21:50:20 2012
New Revision: 1296486
URL: http://svn.apache.org/viewvc?rev=1296486&view=rev
Log:
AVRO-1041. Java: Fix Utf8 to reuse array in more cases. Contributed by Dave
Irving.
Modified:
avro/trunk/CHANGES.txt
avro/trunk/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java
avro/trunk/lang/java/avro/src/test/java/org/apache/avro/util/TestUtf8.java
Modified: avro/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1296486&r1=1296485&r2=1296486&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Mar 2 21:50:20 2012
@@ -29,6 +29,9 @@ Avro 1.6.3 (unreleased)
AVRO-1039. C: Don't use nonexistent codecs in test cases. (dcreager)
+ AVRO-1041. Java: Fix Utf8 to reuse array in more cases.
+ (Dave Irving via cutting)
+
Avro 1.6.2 (13 February 2012)
NEW FEATURES
Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java?rev=1296486&r1=1296485&r2=1296486&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java
(original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/util/Utf8.java Fri
Mar 2 21:50:20 2012
@@ -73,7 +73,7 @@ public class Utf8 implements Comparable<
/** Set length in bytes. Should called whenever byte content changes, even
* if the length does not change, as this also clears the cached String. */
public Utf8 setByteLength(int newLength) {
- if (this.length < newLength) {
+ if (this.bytes.length < newLength) {
byte[] newBytes = new byte[newLength];
System.arraycopy(bytes, 0, newBytes, 0, this.length);
this.bytes = newBytes;
Modified:
avro/trunk/lang/java/avro/src/test/java/org/apache/avro/util/TestUtf8.java
URL:
http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/util/TestUtf8.java?rev=1296486&r1=1296485&r2=1296486&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/util/TestUtf8.java
(original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/util/TestUtf8.java
Fri Mar 2 21:50:20 2012
@@ -17,7 +17,13 @@
*/
package org.apache.avro.util;
+import java.io.UnsupportedEncodingException;
+
+import junit.framework.Assert;
+
import org.junit.Test;
+
+import static junit.framework.Assert.assertSame;
import static org.junit.Assert.assertEquals;
public class TestUtf8 {
@@ -29,4 +35,17 @@ public class TestUtf8 {
assertEquals(bs[i], u.getBytes()[i]);
}
}
+
+ @Test public void testArrayReusedWhenLargerThanRequestedSize() throws
UnsupportedEncodingException {
+ byte[] bs = "55555".getBytes("UTF-8");
+ Utf8 u = new Utf8(bs);
+ assertEquals(5, u.getByteLength());
+ byte[] content = u.getBytes();
+ u.setByteLength(3);
+ assertEquals(3, u.getByteLength());
+ assertSame(content, u.getBytes());
+ u.setByteLength(4);
+ assertEquals(4, u.getByteLength());
+ assertSame(content, u.getBytes());
+ }
}