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  import java.util.Comparator;
26  
27  import org.apache.directory.mavibot.btree.comparator.ByteArrayComparator;
28  
29  
30  /**
31   * A serializer for a byte[].
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class ByteArraySerializer extends AbstractElementSerializer<byte[]>
36  {
37      /**
38       * Create a new instance of ByteArraySerializer
39       */
40      public ByteArraySerializer()
41      {
42          super( new ByteArrayComparator() );
43      }
44  
45  
46      /**
47       * Create a new instance of ByteArraySerializer with custom comparator
48       */
49      public ByteArraySerializer( Comparator<byte[]> comparator )
50      {
51          super( comparator );
52      }
53  
54  
55      /**
56       * {@inheritDoc}
57       */
58      public byte[] serialize( byte[] element )
59      {
60          int len = -1;
61  
62          if ( element != null )
63          {
64              len = element.length;
65          }
66  
67          byte[] bytes = null;
68  
69          switch ( len )
70          {
71              case 0:
72                  bytes = new byte[4];
73  
74                  bytes[0] = 0x00;
75                  bytes[1] = 0x00;
76                  bytes[2] = 0x00;
77                  bytes[3] = 0x00;
78  
79                  break;
80  
81              case -1:
82                  bytes = new byte[4];
83  
84                  bytes[0] = ( byte ) 0xFF;
85                  bytes[1] = ( byte ) 0xFF;
86                  bytes[2] = ( byte ) 0xFF;
87                  bytes[3] = ( byte ) 0xFF;
88  
89                  break;
90  
91              default:
92                  bytes = new byte[len + 4];
93  
94                  System.arraycopy( element, 0, bytes, 4, len );
95  
96                  bytes[0] = ( byte ) ( len >>> 24 );
97                  bytes[1] = ( byte ) ( len >>> 16 );
98                  bytes[2] = ( byte ) ( len >>> 8 );
99                  bytes[3] = ( byte ) ( len );
100         }
101 
102         return bytes;
103     }
104 
105 
106     /**
107      * Serialize a byte[]
108      * 
109      * @param buffer the Buffer that will contain the serialized value
110      * @param start the position in the buffer we will store the serialized byte[]
111      * @param value the value to serialize
112      * @return The byte[] containing the serialized byte[]
113      */
114     public static byte[] serialize( byte[] buffer, int start, byte[] element )
115     {
116         int len = -1;
117 
118         if ( element != null )
119         {
120             len = element.length;
121         }
122 
123         switch ( len )
124         {
125             case 0:
126                 buffer[start] = 0x00;
127                 buffer[start + 1] = 0x00;
128                 buffer[start + 2] = 0x00;
129                 buffer[start + 3] = 0x00;
130 
131                 break;
132 
133             case -1:
134                 buffer[start] = ( byte ) 0xFF;
135                 buffer[start + 1] = ( byte ) 0xFF;
136                 buffer[start + 2] = ( byte ) 0xFF;
137                 buffer[start + 3] = ( byte ) 0xFF;
138 
139                 break;
140 
141             default:
142 
143                 buffer[start] = ( byte ) ( len >>> 24 );
144                 buffer[start + 1] = ( byte ) ( len >>> 16 );
145                 buffer[start + 2] = ( byte ) ( len >>> 8 );
146                 buffer[start + 3] = ( byte ) ( len );
147 
148                 System.arraycopy( element, 0, buffer, 4 + start, len );
149         }
150 
151         return buffer;
152 
153     }
154 
155 
156     /**
157      * A static method used to deserialize a byte array from a byte array.
158      * 
159      * @param in The byte array containing the byte array
160      * @return A byte[]
161      */
162     public static byte[] deserialize( byte[] in )
163     {
164         if ( ( in == null ) || ( in.length < 4 ) )
165         {
166             throw new RuntimeException( "Cannot extract a byte[] from a buffer with not enough bytes" );
167         }
168 
169         int len = IntSerializer.deserialize( in );
170 
171         switch ( len )
172         {
173             case 0:
174                 return new byte[]
175                     {};
176 
177             case -1:
178                 return null;
179 
180             default:
181                 byte[] result = new byte[len];
182                 System.arraycopy( in, 4, result, 0, len );
183 
184                 return result;
185         }
186     }
187 
188 
189     /**
190      * A static method used to deserialize a byte array from a byte array.
191      * 
192      * @param in The byte array containing the byte array
193      * @param start the position in the byte[] we will deserialize the byte[] from
194      * @return A byte[]
195      */
196     public static byte[] deserialize( byte[] in, int start )
197     {
198         if ( ( in == null ) || ( in.length < 4 + start ) )
199         {
200             throw new RuntimeException( "Cannot extract a byte[] from a buffer with not enough bytes" );
201         }
202 
203         int len = IntSerializer.deserialize( in, start );
204 
205         switch ( len )
206         {
207             case 0:
208                 return new byte[]
209                     {};
210 
211             case -1:
212                 return null;
213 
214             default:
215                 byte[] result = new byte[len];
216                 System.arraycopy( in, 4 + start, result, 0, len );
217 
218                 return result;
219         }
220     }
221 
222 
223     /**
224      * A method used to deserialize a byte array from a byte array.
225      * 
226      * @param in The byte array containing the byte array
227      * @return A byte[]
228      */
229     public byte[] fromBytes( byte[] in )
230     {
231         if ( ( in == null ) || ( in.length < 4 ) )
232         {
233             throw new RuntimeException( "Cannot extract a byte[] from a buffer with not enough bytes" );
234         }
235 
236         int len = IntSerializer.deserialize( in );
237 
238         switch ( len )
239         {
240             case 0:
241                 return new byte[]
242                     {};
243 
244             case -1:
245                 return null;
246 
247             default:
248                 byte[] result = new byte[len];
249                 System.arraycopy( in, 4, result, 0, len );
250 
251                 return result;
252         }
253     }
254 
255 
256     /**
257      * A method used to deserialize a byte array from a byte array.
258      * 
259      * @param in The byte array containing the byte array
260      * @param start the position in the byte[] we will deserialize the byte[] from
261      * @return A byte[]
262      */
263     public byte[] fromBytes( byte[] in, int start )
264     {
265         if ( ( in == null ) || ( in.length < 4 + start ) )
266         {
267             throw new RuntimeException( "Cannot extract a byte[] from a buffer with not enough bytes" );
268         }
269 
270         int len = IntSerializer.deserialize( in, start );
271 
272         switch ( len )
273         {
274             case 0:
275                 return new byte[]
276                     {};
277 
278             case -1:
279                 return null;
280 
281             default:
282                 byte[] result = new byte[len];
283                 System.arraycopy( in, 4 + start, result, 0, len );
284 
285                 return result;
286         }
287     }
288 
289 
290     /**
291      * {@inheritDoc}
292      */
293     public byte[] deserialize( BufferHandler bufferHandler ) throws IOException
294     {
295         byte[] in = bufferHandler.read( 4 );
296 
297         int len = IntSerializer.deserialize( in );
298 
299         switch ( len )
300         {
301             case 0:
302                 return new byte[]
303                     {};
304 
305             case -1:
306                 return null;
307 
308             default:
309                 in = bufferHandler.read( len );
310 
311                 return in;
312         }
313     }
314 
315 
316     /**
317      * {@inheritDoc}
318      */
319     public byte[] deserialize( ByteBuffer buffer ) throws IOException
320     {
321         int len = buffer.getInt();
322 
323         switch ( len )
324         {
325             case 0:
326                 return new byte[]
327                     {};
328 
329             case -1:
330                 return null;
331 
332             default:
333                 byte[] bytes = new byte[len];
334 
335                 buffer.get( bytes );
336 
337                 return bytes;
338         }
339     }
340 }