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.CharArrayComparator;
27  
28  
29  /**
30   * A serializer for a char[].
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class CharArraySerializer extends AbstractElementSerializer<char[]>
35  {
36      /**
37       * Create a new instance of CharArraySerializer
38       */
39      public CharArraySerializer()
40      {
41          super( new CharArrayComparator() );
42      }
43  
44  
45      /**
46       * {@inheritDoc}
47       */
48      public byte[] serialize( char[] element )
49      {
50          int len = -1;
51  
52          if ( element != null )
53          {
54              len = element.length;
55          }
56  
57          byte[] bytes = null;
58  
59          switch ( len )
60          {
61              case 0:
62                  bytes = new byte[4];
63  
64                  bytes[0] = 0x00;
65                  bytes[1] = 0x00;
66                  bytes[2] = 0x00;
67                  bytes[3] = 0x00;
68  
69                  break;
70  
71              case -1:
72                  bytes = new byte[4];
73  
74                  bytes[0] = ( byte ) 0xFF;
75                  bytes[1] = ( byte ) 0xFF;
76                  bytes[2] = ( byte ) 0xFF;
77                  bytes[3] = ( byte ) 0xFF;
78  
79                  break;
80  
81              default:
82                  bytes = new byte[len * 2 + 4];
83                  int pos = 4;
84  
85                  bytes[0] = ( byte ) ( len >>> 24 );
86                  bytes[1] = ( byte ) ( len >>> 16 );
87                  bytes[2] = ( byte ) ( len >>> 8 );
88                  bytes[3] = ( byte ) ( len );
89  
90                  for ( char c : element )
91                  {
92                      bytes[pos++] = ( byte ) ( c >>> 8 );
93                      bytes[pos++] = ( byte ) ( c );
94                  }
95          }
96  
97          return bytes;
98      }
99  
100 
101     /**
102      * A static method used to deserialize a char array from a byte array.
103      * 
104      * @param in The byte array containing the char array
105      * @return A char[]
106      */
107     public static char[] deserialize( byte[] in )
108     {
109         if ( ( in == null ) || ( in.length < 4 ) )
110         {
111             throw new RuntimeException( "Cannot extract a byte[] from a buffer with not enough bytes" );
112         }
113 
114         int len = IntSerializer.deserialize( in );
115 
116         switch ( len )
117         {
118             case 0:
119                 return new char[]
120                     {};
121 
122             case -1:
123                 return null;
124 
125             default:
126                 char[] result = new char[len];
127 
128                 for ( int i = 4; i < len * 2 + 4; i += 2 )
129                 {
130                     result[i] = Character.valueOf( ( char ) ( ( in[i] << 8 ) +
131                         ( in[i + 1] & 0xFF ) ) );
132                 }
133 
134                 return result;
135         }
136     }
137 
138 
139     /**
140      * A method used to deserialize a char array from a byte array.
141      * 
142      * @param in The byte array containing the char array
143      * @return A char[]
144      */
145     public char[] fromBytes( byte[] in, int start )
146     {
147         if ( ( in == null ) || ( in.length - start < 4 ) )
148         {
149             throw new RuntimeException( "Cannot extract a byte[] from a buffer with not enough bytes" );
150         }
151 
152         int len = IntSerializer.deserialize( in, start );
153 
154         switch ( len )
155         {
156             case 0:
157                 return new char[]
158                     {};
159 
160             case -1:
161                 return null;
162 
163             default:
164                 char[] result = new char[len];
165 
166                 for ( int i = 4; i < len * 2 + 4; i += 2 )
167                 {
168                     result[i] = Character.valueOf( ( char ) ( ( in[i] << 8 ) +
169                         ( in[i + 1] & 0xFF ) ) );
170                 }
171 
172                 return result;
173         }
174     }
175 
176 
177     /**
178      * A method used to deserialize a char array from a byte array.
179      * 
180      * @param in The byte array containing the char array
181      * @return A char[]
182      */
183     public char[] fromBytes( byte[] in )
184     {
185         if ( ( in == null ) || ( in.length < 4 ) )
186         {
187             throw new RuntimeException( "Cannot extract a byte[] from a buffer with not enough bytes" );
188         }
189 
190         int len = IntSerializer.deserialize( in );
191 
192         switch ( len )
193         {
194             case 0:
195                 return new char[]
196                     {};
197 
198             case -1:
199                 return null;
200 
201             default:
202                 char[] result = new char[len];
203 
204                 for ( int i = 4; i < len * 2 + 4; i += 2 )
205                 {
206                     result[i] = Character.valueOf( ( char ) ( ( in[i] << 8 ) +
207                         ( in[i + 1] & 0xFF ) ) );
208                 }
209 
210                 return result;
211         }
212     }
213 
214 
215     /**
216      * {@inheritDoc}
217      */
218     public char[] deserialize( BufferHandler bufferHandler ) throws IOException
219     {
220         byte[] in = bufferHandler.read( 4 );
221 
222         int len = IntSerializer.deserialize( in );
223 
224         switch ( len )
225         {
226             case 0:
227                 return new char[]
228                     {};
229 
230             case -1:
231                 return null;
232 
233             default:
234                 char[] result = new char[len];
235                 byte[] buffer = bufferHandler.read( len * 2 );
236 
237                 for ( int i = 0; i < len * 2; i += 2 )
238                 {
239                     result[i] = Character.valueOf( ( char ) ( ( buffer[i] << 8 ) +
240                         ( buffer[i + 1] & 0xFF ) ) );
241                 }
242 
243                 return result;
244         }
245     }
246 
247 
248     /**
249      * {@inheritDoc}
250      */
251     public char[] deserialize( ByteBuffer buffer ) throws IOException
252     {
253         int len = buffer.getInt();
254 
255         switch ( len )
256         {
257             case 0:
258                 return new char[]
259                     {};
260 
261             case -1:
262                 return null;
263 
264             default:
265                 char[] result = new char[len];
266 
267                 for ( int i = 0; i < len; i++ )
268                 {
269                     result[i] = buffer.getChar();
270                 }
271 
272                 return result;
273         }
274     }
275 }