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:labs@labs.apache.org">Mavibot labs 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 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      * {@inheritDoc}
225      */
226     public byte[] deserialize( BufferHandler bufferHandler ) throws IOException
227     {
228         byte[] in = bufferHandler.read( 4 );
229 
230         int len = IntSerializer.deserialize( in );
231 
232         switch ( len )
233         {
234             case 0:
235                 return new byte[]
236                     {};
237 
238             case -1:
239                 return null;
240 
241             default:
242                 in = bufferHandler.read( len );
243 
244                 return in;
245         }
246     }
247 
248 
249     /**
250      * {@inheritDoc}
251      */
252     public byte[] deserialize( ByteBuffer buffer ) throws IOException
253     {
254         int len = buffer.getInt();
255 
256         switch ( len )
257         {
258             case 0:
259                 return new byte[]
260                     {};
261 
262             case -1:
263                 return null;
264 
265             default:
266                 byte[] bytes = new byte[len];
267 
268                 buffer.get( bytes );
269 
270                 return bytes;
271         }
272     }
273 }