Permalink
Browse files

Fix JaxbSerializer, so that it will work properly with ByteBuffer's w…

…hich are backed by a byte[] arrays shared with someone other
  • Loading branch information...
1 parent 655dd50 commit 54e399d2d4ef016b1d9add6113410516900413d1 clueprints committed with root Sep 21, 2012
View
2 README
@@ -19,7 +19,7 @@ Some features provided by this client:
o high level, simple object oriented interface to cassandra
o failover behavior on the client side
o connection pooling for improved performance and scalability
- o JMX conters for monitoring and management
+ o JMX counters for monitoring and management
o configurable and extensible load balancing with three algorithms to choose from: round robin (the default), least active, and a phi-accrural style response time detector
o complete encapsulation of the underlying Thrift API and structs
o automatic retry of downed hosts
View
4 core/src/main/java/me/prettyprint/cassandra/serializers/JaxbSerializer.java
@@ -110,7 +110,9 @@ public Object fromByteBuffer(ByteBuffer bytes) {
return null;
}
- ByteArrayInputStream bais = new ByteArrayInputStream(bytes.array());
+ int l = bytes.remaining();
+ ByteArrayInputStream bais = new ByteArrayInputStream(bytes.array(),
+ bytes.arrayOffset() + bytes.position(), l);
try {
XMLStreamReader reader = createStreamReader(bais);
Object ret = unmarshaller.get().unmarshal(reader);
View
26 core/src/test/java/me/prettyprint/cassandra/serializers/SerializerBaseTest.java
@@ -1,13 +1,13 @@
package me.prettyprint.cassandra.serializers;
+import java.nio.ByteBuffer;
import java.util.Collection;
import junit.framework.Assert;
+import me.prettyprint.hector.api.Serializer;
import org.junit.Test;
-import me.prettyprint.hector.api.Serializer;
-
/**
* A base test class for {@link Serializer}s. <br>
* <br>
@@ -40,6 +40,28 @@ public void testNullObjectRoundTrip() {
Assert.assertNull(getSerializer().fromByteBuffer(
getSerializer().toByteBuffer(null)));
}
+
+ @Test
+ public void testByteBufferWithSharedBackingArrayIsOk() {
+ for (T object : getTestData()) {
+ byte[] bytes = getSerializer().toBytes(object);
+ ByteBuffer byteBufferWithSharedBackingArray = copyIntoLargerArrayAndWrap(bytes);
+ T deserialized = getSerializer().fromByteBuffer(byteBufferWithSharedBackingArray);
+ Assert.assertEquals(object, deserialized);
+ }
+ }
+
+ private ByteBuffer copyIntoLargerArrayAndWrap(byte[] bytes) {
+ int paddingLeft = 5;
+ int paddingRight = 5;
+ byte[] sharedBytesWithOtherStuff = new byte[bytes.length+paddingLeft+paddingRight];
+ for (int i=0; i<bytes.length; i++)
+ {
+ sharedBytesWithOtherStuff[i+paddingLeft] = bytes[i];
+ }
+ ByteBuffer byteBufferWithSharedBackingArray = ByteBuffer.wrap(sharedBytesWithOtherStuff, paddingLeft, bytes.length);
+ return byteBufferWithSharedBackingArray;
+ }
/**
* @return the serializer under test.

0 comments on commit 54e399d

Please sign in to comment.