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