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  import org.apache.directory.mavibot.btree.exception.SerializerCreationException;
28  
29  
30  /**
31   * The Character serializer.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class CharSerializer extends AbstractElementSerializer<Character>
36  {
37      /**
38       * Create a new instance of CharSerializer
39       */
40      public CharSerializer()
41      {
42          super( new CharComparator() );
43      }
44  
45  
46      /**
47       * {@inheritDoc}
48       */
49      public byte[] serialize( Character element )
50      {
51          byte[] bytes = new byte[2];
52  
53          return serialize( bytes, 0, element );
54      }
55  
56  
57      /**
58       * Serialize a char
59       *
60       * @param value the value to serialize
61       * @return The byte[] containing the serialized char
62       */
63      public static byte[] serialize( char value )
64      {
65          byte[] bytes = new byte[2];
66  
67          return serialize( bytes, 0, value );
68      }
69  
70  
71      /**
72       * Serialize a char
73       *
74       * @param buffer the Buffer that will contain the serialized value
75       * @param start the position in the buffer we will store the serialized char
76       * @param value the value to serialize
77       * @return The byte[] containing the serialized char
78       */
79      public static byte[] serialize( byte[] buffer, int start, char value )
80      {
81          buffer[start] = ( byte ) ( value >>> 8 );
82          buffer[start + 1] = ( byte ) ( value );
83  
84          return buffer;
85      }
86  
87  
88      /**
89       * A static method used to deserialize a Character from a byte array.
90       * @param in The byte array containing the Character
91       * @return A Character
92       */
93      public static Character deserialize( byte[] in )
94      {
95          return deserialize( in, 0 );
96      }
97  
98  
99      /**
100      * A static method used to deserialize a Character from a byte array.
101      * @param in The byte array containing the Character
102     * @param start the position in the byte[] we will deserialize the char from
103      * @return A Character
104      */
105     public static Character deserialize( byte[] in, int start )
106     {
107         if ( ( in == null ) || ( in.length < 2 + start ) )
108         {
109             throw new SerializerCreationException( "Cannot extract a Character from a buffer with not enough bytes" );
110         }
111 
112         return Character.valueOf( ( char ) ( ( in[start] << 8 ) +
113             ( in[start + 1] & 0xFF ) ) );
114     }
115 
116 
117     /**
118      * A method used to deserialize a Character from a byte array.
119      * @param in The byte array containing the Character
120      * @return A Character
121      */
122     public Character fromBytes( byte[] in )
123     {
124         return deserialize( in, 0 );
125     }
126 
127 
128     /**
129      * A static method used to deserialize a Character from a byte array.
130      * @param in The byte array containing the Character
131     * @param start the position in the byte[] we will deserialize the char from
132      * @return A Character
133      */
134     public Character fromBytes( byte[] in, int start )
135     {
136         if ( ( in == null ) || ( in.length < 2 + start ) )
137         {
138             throw new SerializerCreationException( "Cannot extract a Character from a buffer with not enough bytes" );
139         }
140 
141         return Character.valueOf( ( char ) ( ( in[start] << 8 ) +
142             ( in[start + 1] & 0xFF ) ) );
143     }
144 
145 
146     /**
147      * {@inheritDoc}
148      */
149     public Character deserialize( ByteBuffer buffer ) throws IOException
150     {
151         return buffer.getChar();
152     }
153 
154 
155     /**
156      * {@inheritDoc}
157      */
158     public Character deserialize( BufferHandler bufferHandler ) throws IOException
159     {
160         byte[] in = bufferHandler.read( 2 );
161 
162         return deserialize( in );
163     }
164 }