Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add BytesArraySerializer. Work was actually done by Patricio

  • Loading branch information...
commit d300accc3963999aabcef7d0e5d3bf3941c9a0e8 1 parent 1c7d3b7
@rantav rantav authored
View
39 src/main/java/me/prettyprint/cassandra/serializers/BytesArraySerializer.java
@@ -0,0 +1,39 @@
+package me.prettyprint.cassandra.serializers;
+
+import java.nio.ByteBuffer;
+
+import me.prettyprint.hector.api.Serializer;
+
+/**
+ * A BytesArraySerializer translates the byte[] to and from ByteBuffer.
+ *
+ * @author Patricio Echague
+ *
+ */
+public final class BytesArraySerializer extends AbstractSerializer<byte[]> implements
+ Serializer<byte[]> {
+
+ private static final BytesArraySerializer instance = new BytesArraySerializer();
+
+ public static BytesArraySerializer get() {
+ return instance;
+ }
+
+ @Override
+ public ByteBuffer toByteBuffer(byte[] obj) {
+ if (obj == null) {
+ return null;
+ }
+ return ByteBuffer.wrap(obj);
+ }
+
+ @Override
+ public byte[] fromByteBuffer(ByteBuffer byteBuffer) {
+ if (byteBuffer == null) {
+ return null;
+ }
+ byte[] bytes = new byte[byteBuffer.remaining()];
+ byteBuffer.get(bytes, 0, bytes.length);
+ return bytes;
+ }
+}
View
31 src/test/java/me/prettyprint/cassandra/serializers/BytesArraySerializerTest.java
@@ -0,0 +1,31 @@
+package me.prettyprint.cassandra.serializers;
+
+
+import static me.prettyprint.cassandra.utils.StringUtils.bytes;
+import static org.junit.Assert.assertArrayEquals;
+
+import org.junit.Test;
+
+/**
+ * @author Patricio Echague
+ * @author Ran Tavory
+ *
+ */
+public class BytesArraySerializerTest {
+
+ @Test
+ public void testConversions() {
+ test(null);
+ test(new byte[]{});
+ test(new byte[]{1});
+ test(new byte[]{1,2,3,4,5});
+ // and also some string oriented tests:
+ test(bytes(""));
+ test(bytes("123"));
+ }
+
+ private void test(byte[] bytes) {
+ BytesArraySerializer e = new BytesArraySerializer();
+ assertArrayEquals(bytes, e.fromByteBuffer(e.toByteBuffer(bytes))) ;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.