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:dev@directory.apache.org">Apache Directory 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      * A method used to deserialize a Character from a byte array.
118      * @param in The byte array containing the Character
119      * @return A Character
120      */
121     public Character fromBytes( byte[] in )
122     {
123         return deserialize( in, 0 );
124     }
125 
126 
127     /**
128      * A static method used to deserialize a Character from a byte array.
129      * @param in The byte array containing the Character
130     * @param start the position in the byte[] we will deserialize the char from
131      * @return A Character
132      */
133     public Character fromBytes( byte[] in, int start )
134     {
135         if ( ( in == null ) || ( in.length < 2 + start ) )
136         {
137             throw new RuntimeException( "Cannot extract a Character from a buffer with not enough bytes" );
138         }
139 
140         return Character.valueOf( ( char ) ( ( in[start] << 8 ) +
141             ( in[start + 1] & 0xFF ) ) );
142     }
143 
144 
145     /**
146      * {@inheritDoc}
147      */
148     public Character deserialize( ByteBuffer buffer ) throws IOException
149     {
150         return buffer.getChar();
151     }
152 
153 
154     /**
155      * {@inheritDoc}
156      */
157     public Character deserialize( BufferHandler bufferHandler ) throws IOException
158     {
159         byte[] in = bufferHandler.read( 2 );
160 
161         return deserialize( in );
162     }
163 }