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