Author: elecharny
Date: Tue Apr  2 17:38:29 2013
New Revision: 1463639

URL: http://svn.apache.org/r1463639
Log:
Completed the serializers so that we can use a starting pint in a buffer to 
serialize or deserialize

Modified:
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/BooleanSerializer.java
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteArraySerializer.java
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/LongSerializer.java
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java
    
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java

Modified: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/BooleanSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/BooleanSerializer.java?rev=1463639&r1=1463638&r2=1463639&view=diff
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/BooleanSerializer.java
 (original)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/BooleanSerializer.java
 Tue Apr  2 17:38:29 2013
@@ -48,9 +48,38 @@ public class BooleanSerializer extends A
     public byte[] serialize( Boolean element )
     {
         byte[] bytes = new byte[1];
-        bytes[0] = element.booleanValue() ? ( byte ) 0x01 : ( byte ) 0x00;
 
-        return bytes;
+        return serialize( bytes, 0, element );
+    }
+
+
+    /**
+     * Serialize a boolean
+     * 
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized boolean
+     */
+    public static byte[] serialize( boolean element )
+    {
+        byte[] bytes = new byte[1];
+
+        return serialize( bytes, 0, element );
+    }
+
+
+    /**
+     * Serialize a boolean
+     * 
+     * @param buffer the Buffer that will contain the serialized value
+     * @param start the position in the buffer we will store the serialized 
boolean
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized boolean
+     */
+    public static byte[] serialize( byte[] buffer, int start, boolean element )
+    {
+        buffer[start] = element ? ( byte ) 0x01 : ( byte ) 0x00;
+
+        return buffer;
     }
 
 
@@ -62,12 +91,25 @@ public class BooleanSerializer extends A
      */
     public static Boolean deserialize( byte[] in )
     {
-        if ( ( in == null ) || ( in.length < 1 ) )
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * A static method used to deserialize a Boolean from a byte array.
+     * 
+     * @param in The byte array containing the boolean
+     * @param start the position in the byte[] we will deserialize the boolean 
from
+     * @return A boolean
+     */
+    public static Boolean deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 1 + start ) )
         {
             throw new RuntimeException( "Cannot extract a Boolean from a 
buffer with not enough bytes" );
         }
 
-        return in[0] == 0x01;
+        return in[start] == 0x01;
     }
 
 

Modified: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteArraySerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteArraySerializer.java?rev=1463639&r1=1463638&r2=1463639&view=diff
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteArraySerializer.java
 (original)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteArraySerializer.java
 Tue Apr  2 17:38:29 2013
@@ -94,6 +94,56 @@ public class ByteArraySerializer extends
 
 
     /**
+     * Serialize a byte[]
+     * 
+     * @param buffer the Buffer that will contain the serialized value
+     * @param start the position in the buffer we will store the serialized 
byte[]
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized byte[]
+     */
+    public static byte[] serialize( byte[] buffer, int start, byte[] element )
+    {
+        int len = -1;
+
+        if ( element != null )
+        {
+            len = element.length;
+        }
+
+        switch ( len )
+        {
+            case 0:
+                buffer[start] = 0x00;
+                buffer[start + 1] = 0x00;
+                buffer[start + 2] = 0x00;
+                buffer[start + 3] = 0x00;
+
+                break;
+
+            case -1:
+                buffer[start] = ( byte ) 0xFF;
+                buffer[start + 1] = ( byte ) 0xFF;
+                buffer[start + 2] = ( byte ) 0xFF;
+                buffer[start + 3] = ( byte ) 0xFF;
+
+                break;
+
+            default:
+
+                buffer[start] = ( byte ) ( len >>> 24 );
+                buffer[start + 1] = ( byte ) ( len >>> 16 );
+                buffer[start + 2] = ( byte ) ( len >>> 8 );
+                buffer[start + 3] = ( byte ) ( len );
+
+                System.arraycopy( element, 0, buffer, 4 + start, len );
+        }
+
+        return buffer;
+
+    }
+
+
+    /**
      * A static method used to deserialize a byte array from a byte array.
      * 
      * @param in The byte array containing the byte array
@@ -127,6 +177,40 @@ public class ByteArraySerializer extends
 
 
     /**
+     * A static method used to deserialize a byte array from a byte array.
+     * 
+     * @param in The byte array containing the byte array
+     * @param start the position in the byte[] we will deserialize the byte[] 
from
+     * @return A byte[]
+     */
+    public static byte[] deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 4 + start ) )
+        {
+            throw new RuntimeException( "Cannot extract a byte[] from a buffer 
with not enough bytes" );
+        }
+
+        int len = IntSerializer.deserialize( in, start );
+
+        switch ( len )
+        {
+            case 0:
+                return new byte[]
+                    {};
+
+            case -1:
+                return null;
+
+            default:
+                byte[] result = new byte[len];
+                System.arraycopy( in, 4 + start, result, 0, len );
+
+                return result;
+        }
+    }
+
+
+    /**
      * {@inheritDoc}
      */
     public byte[] deserialize( BufferHandler bufferHandler ) throws IOException

Modified: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java?rev=1463639&r1=1463638&r2=1463639&view=diff
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java
 (original)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ByteSerializer.java
 Tue Apr  2 17:38:29 2013
@@ -48,9 +48,38 @@ public class ByteSerializer extends Abst
     public byte[] serialize( Byte element )
     {
         byte[] bytes = new byte[1];
-        bytes[0] = element.byteValue();
 
-        return bytes;
+        return serialize( bytes, 0, element );
+    }
+
+
+    /**
+     * Serialize a byte
+     * 
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized byte
+     */
+    public static byte[] serialize( byte value )
+    {
+        byte[] bytes = new byte[1];
+
+        return serialize( bytes, 0, value );
+    }
+
+
+    /**
+     * Serialize a byte
+     * 
+     * @param buffer the Buffer that will contain the serialized value
+     * @param start the position in the buffer we will store the serialized 
byte
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized byte
+     */
+    public static byte[] serialize( byte[] buffer, int start, byte value )
+    {
+        buffer[start] = value;
+
+        return buffer;
     }
 
 
@@ -61,12 +90,24 @@ public class ByteSerializer extends Abst
      */
     public static Byte deserialize( byte[] in )
     {
-        if ( ( in == null ) || ( in.length < 1 ) )
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * A static method used to deserialize a Byte from a byte array.
+     * @param in The byte array containing the Byte
+     * @param start the position in the byte[] we will deserialize the byte 
from
+     * @return A Byte
+     */
+    public static Byte deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 1 + start ) )
         {
             throw new RuntimeException( "Cannot extract a Byte from a buffer 
with not enough bytes" );
         }
 
-        return in[0];
+        return in[start];
     }
 
 

Modified: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java?rev=1463639&r1=1463638&r2=1463639&view=diff
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java
 (original)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/CharSerializer.java
 Tue Apr  2 17:38:29 2013
@@ -48,12 +48,39 @@ public class CharSerializer extends Abst
     public byte[] serialize( Character element )
     {
         byte[] bytes = new byte[2];
-        char value = element.charValue();
 
-        bytes[0] = ( byte ) ( value >>> 8 );
-        bytes[1] = ( byte ) ( value );
+        return serialize( bytes, 0, element );
+    }
+
+
+    /**
+     * Serialize a char
+     * 
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized char
+     */
+    public static byte[] serialize( char value )
+    {
+        byte[] bytes = new byte[2];
 
-        return bytes;
+        return serialize( bytes, 0, value );
+    }
+
+
+    /**
+     * Serialize a char
+     * 
+     * @param buffer the Buffer that will contain the serialized value
+     * @param start the position in the buffer we will store the serialized 
char
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized char
+     */
+    public static byte[] serialize( byte[] buffer, int start, char value )
+    {
+        buffer[start] = ( byte ) ( value >>> 8 );
+        buffer[start + 1] = ( byte ) ( value );
+
+        return buffer;
     }
 
 
@@ -64,13 +91,25 @@ public class CharSerializer extends Abst
      */
     public static Character deserialize( byte[] in )
     {
-        if ( ( in == null ) || ( in.length < 2 ) )
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * A static method used to deserialize a Character from a byte array.
+     * @param in The byte array containing the Character
+    * @param start the position in the byte[] we will deserialize the char from
+     * @return A Character
+     */
+    public static Character deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 2 + start ) )
         {
             throw new RuntimeException( "Cannot extract a Character from a 
buffer with not enough bytes" );
         }
 
-        return Character.valueOf( ( char ) ( ( in[0] << 8 ) +
-            ( in[1] & 0xFF ) ) );
+        return Character.valueOf( ( char ) ( ( in[start] << 8 ) +
+            ( in[start + 1] & 0xFF ) ) );
     }
 
 

Modified: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java?rev=1463639&r1=1463638&r2=1463639&view=diff
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java
 (original)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/IntSerializer.java
 Tue Apr  2 17:38:29 2013
@@ -49,15 +49,27 @@ public class IntSerializer extends Abstr
      */
     public static Integer deserialize( byte[] in )
     {
-        if ( ( in == null ) || ( in.length < 4 ) )
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * A static method used to deserialize an Integer from a byte array.
+     * @param in The byte array containing the Integer
+     * @param start the position in the byte[] we will deserialize the int from
+     * @return An Integer
+     */
+    public static Integer deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 4 + start ) )
         {
             throw new RuntimeException( "Cannot extract a Integer from a 
buffer with not enough bytes" );
         }
 
-        return ( in[0] << 24 ) +
-            ( ( in[1] & 0xFF ) << 16 ) +
-            ( ( in[2] & 0xFF ) << 8 ) +
-            ( in[3] & 0xFF );
+        return ( in[start] << 24 ) +
+            ( ( in[start + 1] & 0xFF ) << 16 ) +
+            ( ( in[start + 2] & 0xFF ) << 8 ) +
+            ( in[start + 3] & 0xFF );
     }
 
 
@@ -91,17 +103,34 @@ public class IntSerializer extends Abstr
 
 
     /**
-     * {@inheritDoc}
+     * Serialize an int
+     * 
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized int
      */
     public static byte[] serialize( int value )
     {
         byte[] bytes = new byte[4];
 
-        bytes[0] = ( byte ) ( value >>> 24 );
-        bytes[1] = ( byte ) ( value >>> 16 );
-        bytes[2] = ( byte ) ( value >>> 8 );
-        bytes[3] = ( byte ) ( value );
+        return serialize( bytes, 0, value );
+    }
+
+
+    /**
+     * Serialize an int
+     * 
+     * @param buffer the Buffer that will contain the serialized value
+     * @param start the position in the buffer we will store the serialized int
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized int
+     */
+    public static byte[] serialize( byte[] buffer, int start, int value )
+    {
+        buffer[start] = ( byte ) ( value >>> 24 );
+        buffer[start + 1] = ( byte ) ( value >>> 16 );
+        buffer[start + 2] = ( byte ) ( value >>> 8 );
+        buffer[start + 3] = ( byte ) ( value );
 
-        return bytes;
+        return buffer;
     }
 }

Modified: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/LongSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/LongSerializer.java?rev=1463639&r1=1463638&r2=1463639&view=diff
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/LongSerializer.java
 (original)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/LongSerializer.java
 Tue Apr  2 17:38:29 2013
@@ -52,47 +52,75 @@ public class LongSerializer extends Abst
 
 
     /**
-     * A static method used to derialize a long into a byte array.
-     * @param in The byte array containing the long
-     * @return A long
+     * Serialize an long
+     * 
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized long
      */
     public static byte[] serialize( long value )
     {
         byte[] bytes = new byte[8];
 
-        bytes[0] = ( byte ) ( value >>> 56 );
-        bytes[1] = ( byte ) ( value >>> 48 );
-        bytes[2] = ( byte ) ( value >>> 40 );
-        bytes[3] = ( byte ) ( value >>> 32 );
-        bytes[4] = ( byte ) ( value >>> 24 );
-        bytes[5] = ( byte ) ( value >>> 16 );
-        bytes[6] = ( byte ) ( value >>> 8 );
-        bytes[7] = ( byte ) ( value );
+        return serialize( bytes, 0, value );
+    }
+
+
+    /**
+     * Serialize an long
+     * 
+     * @param buffer the Buffer that will contain the serialized value
+     * @param start the position in the buffer we will store the serialized 
long
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized long
+     */
+    public static byte[] serialize( byte[] buffer, int start, long value )
+    {
+        buffer[start] = ( byte ) ( value >>> 56 );
+        buffer[start + 1] = ( byte ) ( value >>> 48 );
+        buffer[start + 2] = ( byte ) ( value >>> 40 );
+        buffer[start + 3] = ( byte ) ( value >>> 32 );
+        buffer[start + 4] = ( byte ) ( value >>> 24 );
+        buffer[start + 5] = ( byte ) ( value >>> 16 );
+        buffer[start + 6] = ( byte ) ( value >>> 8 );
+        buffer[start + 7] = ( byte ) ( value );
 
-        return bytes;
+        return buffer;
     }
 
 
     /**
      * A static method used to deserialize a Long from a byte array.
      * @param in The byte array containing the Long
+     * @param start the position in the byte[] we will deserialize the long 
from
      * @return A Long
      */
     public static Long deserialize( byte[] in )
     {
-        if ( ( in == null ) || ( in.length < 8 ) )
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * A static method used to deserialize an Integer from a byte array.
+     * @param in The byte array containing the Integer
+     * @param start the position in the byte[] we will deserialize the long 
from
+     * @return An Integer
+     */
+    public static Long deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 8 + start ) )
         {
             throw new RuntimeException( "Cannot extract a Long from a buffer 
with not enough bytes" );
         }
 
-        long result = ( ( long ) in[0] << 56 ) +
-            ( ( in[1] & 0xFFL ) << 48 ) +
-            ( ( in[2] & 0xFFL ) << 40 ) +
-            ( ( in[3] & 0xFFL ) << 32 ) +
-            ( ( in[4] & 0xFFL ) << 24 ) +
-            ( ( in[5] & 0xFFL ) << 16 ) +
-            ( ( in[6] & 0xFFL ) << 8 ) +
-            ( in[7] & 0xFFL );
+        long result = ( ( long ) in[start] << 56 ) +
+            ( ( in[start + 1] & 0xFFL ) << 48 ) +
+            ( ( in[start + 2] & 0xFFL ) << 40 ) +
+            ( ( in[start + 3] & 0xFFL ) << 32 ) +
+            ( ( in[start + 4] & 0xFFL ) << 24 ) +
+            ( ( in[start + 5] & 0xFFL ) << 16 ) +
+            ( ( in[start + 6] & 0xFFL ) << 8 ) +
+            ( in[start + 7] & 0xFFL );
 
         return result;
     }

Modified: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java?rev=1463639&r1=1463638&r2=1463639&view=diff
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java
 (original)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/ShortSerializer.java
 Tue Apr  2 17:38:29 2013
@@ -48,12 +48,39 @@ public class ShortSerializer extends Abs
     public byte[] serialize( Short element )
     {
         byte[] bytes = new byte[2];
-        short value = element.shortValue();
 
-        bytes[0] = ( byte ) ( value >>> 8 );
-        bytes[1] = ( byte ) ( value );
+        return serialize( bytes, 0, element );
+    }
+
+
+    /**
+     * Serialize a short
+     * 
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized short
+     */
+    public static byte[] serialize( short value )
+    {
+        byte[] bytes = new byte[2];
 
-        return bytes;
+        return serialize( bytes, 0, value );
+    }
+
+
+    /**
+     * Serialize a short
+     * 
+     * @param buffer the Buffer that will contain the serialized value
+     * @param start the position in the buffer we will store the serialized 
short
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized short
+     */
+    public static byte[] serialize( byte[] buffer, int start, short value )
+    {
+        buffer[start] = ( byte ) ( value >>> 8 );
+        buffer[start + 1] = ( byte ) ( value );
+
+        return buffer;
     }
 
 
@@ -64,12 +91,24 @@ public class ShortSerializer extends Abs
      */
     public static Short deserialize( byte[] in )
     {
-        if ( ( in == null ) || ( in.length < 2 ) )
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * A static method used to deserialize a Short from a byte array.
+     * @param in The byte array containing the Short
+    * @param start the position in the byte[] we will deserialize the short 
from
+     * @return A Short
+     */
+    public static Short deserialize( byte[] in, int start )
+    {
+        if ( ( in == null ) || ( in.length < 2 + start ) )
         {
             throw new RuntimeException( "Cannot extract a Short from a buffer 
with not enough bytes" );
         }
 
-        return ( short ) ( ( in[0] << 8 ) + ( in[1] & 0xFF ) );
+        return ( short ) ( ( in[start] << 8 ) + ( in[start + 1] & 0xFF ) );
     }
 
 

Modified: 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java
URL: 
http://svn.apache.org/viewvc/labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java?rev=1463639&r1=1463638&r2=1463639&view=diff
==============================================================================
--- 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java
 (original)
+++ 
labs/mavibot/branches/mavibot-multivalue-support/mavibot/src/main/java/org/apache/mavibot/btree/serializer/StringSerializer.java
 Tue Apr  2 17:38:29 2013
@@ -45,6 +45,100 @@ public class StringSerializer extends Ab
 
 
     /**
+     * A static method used to deserialize a String from a byte array.
+     * @param in The byte array containing the String
+     * @return A String
+     */
+    public static String deserialize( byte[] in )
+    {
+        return deserialize( in, 0 );
+    }
+
+
+    /**
+     * A static method used to deserialize a String from a byte array.
+     * @param in The byte array containing the String
+     * @return A String
+     */
+    public static String deserialize( byte[] in, int start )
+    {
+        int length = IntSerializer.deserialize( in, start );
+
+        if ( length == 0xFFFFFFFF )
+        {
+            return null;
+        }
+
+        if ( in.length < length + 4 + start )
+        {
+            throw new RuntimeException( "Cannot extract a String from a buffer 
with not enough bytes" );
+        }
+
+        return Strings.utf8ToString( in, start + 4, length );
+    }
+
+
+    /**
+     * Serialize a String. We store the length on 4 bytes, then the String
+     * 
+     * @param buffer the Buffer that will contain the serialized value
+     * @param start the position in the buffer we will store the serialized 
String
+     * @param value the value to serialize
+     * @return The byte[] containing the serialized String
+     */
+    public static byte[] serialize( byte[] buffer, int start, String element )
+    {
+        int len = -1;
+
+        if ( element != null )
+        {
+            len = element.length();
+        }
+
+        switch ( len )
+        {
+            case 0:
+                buffer[start] = 0x00;
+                buffer[start + 1] = 0x00;
+                buffer[start + 2] = 0x00;
+                buffer[start + 3] = 0x00;
+
+                break;
+
+            case -1:
+                buffer[start] = ( byte ) 0xFF;
+                buffer[start + 1] = ( byte ) 0xFF;
+                buffer[start + 2] = ( byte ) 0xFF;
+                buffer[start + 3] = ( byte ) 0xFF;
+
+                break;
+
+            default:
+                try
+                {
+                    byte[] strBytes = element.getBytes( "UTF-8" );
+
+                    buffer = new byte[strBytes.length + 4];
+
+                    System.arraycopy( strBytes, 0, buffer, 4, strBytes.length 
);
+
+                    buffer[start] = ( byte ) ( strBytes.length >>> 24 );
+                    buffer[start + 1] = ( byte ) ( strBytes.length >>> 16 );
+                    buffer[start + 2] = ( byte ) ( strBytes.length >>> 8 );
+                    buffer[start + 3] = ( byte ) ( strBytes.length );
+                }
+                catch ( UnsupportedEncodingException uee )
+                {
+                    // if this happens something is really strange
+                    throw new RuntimeException( uee );
+                }
+        }
+
+        return buffer;
+    }
+
+
+    /**
      * {@inheritDoc}
      */
     public byte[] serialize( String element )



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to