avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1301818 - in /avro/trunk: ./ lang/java/avro/src/main/java/org/apache/avro/generic/ lang/java/avro/src/test/java/org/apache/avro/generic/ lang/java/ipc/src/test/java/org/apache/avro/specific/
Date Fri, 16 Mar 2012 23:12:09 GMT
Author: cutting
Date: Fri Mar 16 23:12:08 2012
New Revision: 1301818

URL: http://svn.apache.org/viewvc?rev=1301818&view=rev
Log:
AVRO-1045. Java: Fix a bug in GenericData#deepCopy() of ByteBuffer values.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
    avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1301818&r1=1301817&r2=1301818&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Mar 16 23:12:08 2012
@@ -17,6 +17,9 @@ Avro 1.7.0 (unreleased)
 
   BUG FIXES
 
+    AVRO-1045. Java: Fix a bug in GenericData#deepCopy() of ByteBuffer values.
+    (cutting)
+
 Avro 1.6.3 (5 March 2012)
 
   IMPROVEMENTS

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java?rev=1301818&r1=1301817&r2=1301818&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java Fri Mar
16 23:12:08 2012
@@ -784,11 +784,12 @@ public class GenericData {
         return new Boolean((Boolean)value);
       case BYTES:
         ByteBuffer byteBufferValue = (ByteBuffer) value;
-        byte[] bytesCopy = new byte[byteBufferValue.capacity()];
-        byteBufferValue.rewind();
-        byteBufferValue.get(bytesCopy);
-        byteBufferValue.rewind();
-        return ByteBuffer.wrap(bytesCopy);
+        int start = byteBufferValue.position();
+        int length = byteBufferValue.limit() - start;
+        byte[] bytesCopy = new byte[length];
+        byteBufferValue.get(bytesCopy, 0, length);
+        byteBufferValue.position(start);
+        return ByteBuffer.wrap(bytesCopy, 0, length);
       case DOUBLE:
         return new Double((Double)value);
       case ENUM:

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java?rev=1301818&r1=1301817&r2=1301818&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java Fri
Mar 16 23:12:08 2012
@@ -19,6 +19,7 @@ package org.apache.avro.generic;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Collection;
@@ -333,4 +334,25 @@ public class TestGenericData {
       fail("IOException while writing records to output stream.");
     }
   }
+  
+  @Test
+  public void testByteBufferDeepCopy() {
+    // Test that a deep copy of a byte buffer respects the byte buffer
+    // limits and capacity.
+    byte[] buffer_value = {0, 1, 2, 3, 0, 0, 0};
+    ByteBuffer buffer = ByteBuffer.wrap(buffer_value, 1, 4);
+    Schema schema = Schema.createRecord("my_record", "doc", "mytest", false);
+    Field byte_field =
+      new Field("bytes", Schema.create(Type.BYTES), null, null);
+    schema.setFields(Arrays.asList(byte_field));
+    
+    GenericRecord record = new GenericData.Record(schema);
+    record.put(byte_field.name(), buffer);
+    
+    GenericRecord copy =
+      (GenericRecord) GenericData.get().deepCopy(schema, record);
+    ByteBuffer buffer_copy = (ByteBuffer) copy.get(byte_field.name());
+
+    assertEquals(buffer, buffer_copy);
+  }
 }

Modified: avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java?rev=1301818&r1=1301817&r2=1301818&view=diff
==============================================================================
--- avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java
(original)
+++ avro/trunk/lang/java/ipc/src/test/java/org/apache/avro/specific/TestSpecificRecordBuilder.java
Fri Mar 16 23:12:08 2012
@@ -141,7 +141,6 @@ public class TestSpecificRecordBuilder {
     Assert.assertEquals(interop.getArrayField().size(), copy.getArrayField().size());
     Assert.assertEquals(interop.getArrayField(), copy.getArrayField());
     Assert.assertEquals(interop.getBoolField(), copy.getBoolField());
-    Assert.assertEquals(interop.getBytesField().capacity(), copy.getBytesField().capacity());
     Assert.assertEquals(interop.getBytesField(), copy.getBytesField());
     Assert.assertEquals(interop.getDoubleField(), copy.getDoubleField());
     Assert.assertEquals(interop.getEnumField(), copy.getEnumField());



Mime
View raw message