Author: elecharny
Date: Wed Aug 1 12:08:48 2012
New Revision: 1367949
URL: http://svn.apache.org/viewvc?rev=1367949&view=rev
Log:
Added the StringSerializer class and tests
Added:
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/StringSerializerTest.java
Added:
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java
URL:
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java?rev=1367949&view=auto
==============================================================================
---
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java
(added)
+++
labs/mavibot/trunk/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java
Wed Aug 1 12:08:48 2012
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mavibot.btree.serializer;
+
+
+import java.io.UnsupportedEncodingException;
+
+
+/**
+ * The String serializer.
+ *
+ * @author <a href="mailto:[email protected]">Mavibot labs Project</a>
+ */
+public class StringSerializer implements ElementSerializer<String>
+{
+ /**
+ * {@inheritDoc}
+ */
+ public byte[] serialize( String element )
+ {
+ int len = -1;
+
+ if ( element != null )
+ {
+ len = element.length();
+ }
+
+ byte[] bytes = null;
+
+ switch ( len )
+ {
+ case 0:
+ bytes = new byte[4];
+
+ bytes[0] = 0x00;
+ bytes[1] = 0x00;
+ bytes[2] = 0x00;
+ bytes[3] = 0x00;
+
+ break;
+
+ case -1:
+ bytes = new byte[4];
+
+ bytes[0] = ( byte ) 0xFF;
+ bytes[1] = ( byte ) 0xFF;
+ bytes[2] = ( byte ) 0xFF;
+ bytes[3] = ( byte ) 0xFF;
+
+ break;
+
+ default:
+ try
+ {
+ byte[] strBytes = element.getBytes( "UTF-8" );
+
+ bytes = new byte[strBytes.length + 4];
+
+ System.arraycopy( strBytes, 0, bytes, 4, strBytes.length );
+
+ bytes[0] = ( byte ) ( strBytes.length >>> 24 );
+ bytes[1] = ( byte ) ( strBytes.length >>> 16 );
+ bytes[2] = ( byte ) ( strBytes.length >>> 8 );
+ bytes[3] = ( byte ) ( strBytes.length );
+ }
+ catch ( UnsupportedEncodingException uee )
+ {
+ // if this happens something is really strange
+ throw new RuntimeException( uee );
+ }
+ }
+
+ return bytes;
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ public String deserialize( byte[] in )
+ {
+ int len = ( in[0] << 24 ) +
+ ( ( in[1] & 0xFF ) << 16 ) +
+ ( ( in[2] & 0xFF ) << 8 ) +
+ ( in[3] & 0xFF );
+
+ switch ( len )
+ {
+ case 0:
+ return "";
+
+ case -1:
+ return null;
+
+ default:
+ try
+ {
+ return new String( in, 4, len, "UTF-8" );
+ }
+ catch ( UnsupportedEncodingException uee )
+ {
+ // if this happens something is really strange
+ throw new RuntimeException( uee );
+ }
+ }
+ }
+}
Added:
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/StringSerializerTest.java
URL:
http://svn.apache.org/viewvc/labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/StringSerializerTest.java?rev=1367949&view=auto
==============================================================================
---
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/StringSerializerTest.java
(added)
+++
labs/mavibot/trunk/mavibot/src/test/java/org/apache/mavibot/btree/serializer/StringSerializerTest.java
Wed Aug 1 12:08:48 2012
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.mavibot.btree.serializer;
+
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+
+/**
+ * Test the StringSerializer class
+ *
+ * @author <a href="mailto:[email protected]">Mavibot labs Project</a>
+ */
+public class StringSerializerTest
+{
+ private static StringSerializer serializer = new StringSerializer();
+
+
+ @Test
+ public void testStringSerializer()
+ {
+ String value = null;
+ byte[] result = serializer.serialize( value );
+
+ assertEquals( 4, result.length );
+ assertEquals( ( byte ) 0xFF, result[0] );
+ assertEquals( ( byte ) 0xFF, result[1] );
+ assertEquals( ( byte ) 0xFF, result[2] );
+ assertEquals( ( byte ) 0xFF, result[3] );
+
+ assertEquals( value, serializer.deserialize( result ) );
+
+ // ------------------------------------------------------------------
+ value = "";
+ result = serializer.serialize( value );
+
+ assertEquals( 4, result.length );
+ assertEquals( ( byte ) 0x00, result[0] );
+ assertEquals( ( byte ) 0x00, result[1] );
+ assertEquals( ( byte ) 0x00, result[2] );
+ assertEquals( ( byte ) 0x00, result[3] );
+
+ assertEquals( value, serializer.deserialize( result ) );
+
+ // ------------------------------------------------------------------
+ value = "test";
+ result = serializer.serialize( value );
+
+ assertEquals( 8, result.length );
+ assertEquals( ( byte ) 0x00, result[0] );
+ assertEquals( ( byte ) 0x00, result[1] );
+ assertEquals( ( byte ) 0x00, result[2] );
+ assertEquals( ( byte ) 0x04, result[3] );
+ assertEquals( 't', result[4] );
+ assertEquals( 'e', result[5] );
+ assertEquals( 's', result[6] );
+ assertEquals( 't', result[7] );
+
+ assertEquals( value, serializer.deserialize( result ) );
+
+ // ------------------------------------------------------------------
+ value = "Lécharny";
+ result = serializer.serialize( value );
+
+ assertEquals( 13, result.length );
+ assertEquals( ( byte ) 0x00, result[0] );
+ assertEquals( ( byte ) 0x00, result[1] );
+ assertEquals( ( byte ) 0x00, result[2] );
+ assertEquals( ( byte ) 0x09, result[3] );
+ assertEquals( 'L', result[4] );
+ assertEquals( ( byte ) 0xC3, result[5] );
+ assertEquals( ( byte ) 0xA9, result[6] );
+ assertEquals( 'c', result[7] );
+ assertEquals( 'h', result[8] );
+ assertEquals( 'a', result[9] );
+ assertEquals( 'r', result[10] );
+ assertEquals( 'n', result[11] );
+ assertEquals( 'y', result[12] );
+
+ assertEquals( value, serializer.deserialize( result ) );
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]