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