View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.directory.mavibot.btree.serializer;
21  
22  
23  import java.io.IOException;
24  import java.nio.ByteBuffer;
25  
26  import org.apache.directory.mavibot.btree.comparator.CharComparator;
27  
28  
29  /**
30   * The Character serializer.
31   * 
32   * @author <a href="mailto:labs@labs.apache.org">Mavibot labs Project</a>
33   */
34  public class CharSerializer extends AbstractElementSerializer<Character>
35  {
36      /**
37       * Create a new instance of CharSerializer
38       */
39      public CharSerializer()
40      {
41          super( new CharComparator() );
42      }
43  
44  
45      /**
46       * {@inheritDoc}
47       */
48      public byte[] serialize( Character element )
49      {
50          byte[] bytes = new byte[2];
51  
52          return serialize( bytes, 0, element );
53      }
54  
55  
56      /**
57       * Serialize a char
58       * 
59       * @param value the value to serialize
60       * @return The byte[] containing the serialized char
61       */
62      public static byte[] serialize( char value )
63      {
64          byte[] bytes = new byte[2];
65  
66          return serialize( bytes, 0, value );
67      }
68  
69  
70      /**
71       * Serialize a char
72       * 
73       * @param buffer the Buffer that will contain the serialized value
74       * @param start the position in the buffer we will store the serialized char
75       * @param value the value to serialize
76       * @return The byte[] containing the serialized char
77       */
78      public static byte[] serialize( byte[] buffer, int start, char value )
79      {
80          buffer[start] = ( byte ) ( value >>> 8 );
81          buffer[start + 1] = ( byte ) ( value );
82  
83          return buffer;
84      }
85  
86  
87      /**
88       * A static method used to deserialize a Character from a byte array.
89       * @param in The byte array containing the Character
90       * @return A Character
91       */
92      public static Character deserialize( byte[] in )
93      {
94          return deserialize( in, 0 );
95      }
96  
97  
98      /**
99       * A static method used to deserialize a Character from a byte array.
100      * @param in The byte array containing the Character
101     * @param start the position in the byte[] we will deserialize the char from
102      * @return A Character
103      */
104     public static Character deserialize( byte[] in, int start )
105     {
106         if ( ( in == null ) || ( in.length < 2 + start ) )
107         {
108             throw new RuntimeException( "Cannot extract a Character from a buffer with not enough bytes" );
109         }
110 
111         return Character.valueOf( ( char ) ( ( in[start] << 8 ) +
112             ( in[start + 1] & 0xFF ) ) );
113     }
114 
115 
116     /**
117      * {@inheritDoc}
118      */
119     public Character deserialize( ByteBuffer buffer ) throws IOException
120     {
121         return buffer.getChar();
122     }
123 
124 
125     /**
126      * {@inheritDoc}
127      */
128     public Character deserialize( BufferHandler bufferHandler ) throws IOException
129     {
130         byte[] in = bufferHandler.read( 2 );
131 
132         return deserialize( in );
133     }
134 }