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:labs@labs.apache.org">Mavibot labs 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       * {@inheritDoc}
78       */
79      public Integer deserialize( ByteBuffer buffer ) throws IOException
80      {
81          return buffer.getInt();
82      }
83  
84  
85      /**
86       * {@inheritDoc}
87       */
88      public Integer deserialize( BufferHandler bufferHandler ) throws IOException
89      {
90          byte[] in = bufferHandler.read( 4 );
91  
92          return deserialize( in );
93      }
94  
95  
96      /**
97       * {@inheritDoc}
98       */
99      public byte[] serialize( Integer element )
100     {
101         return serialize( element.intValue() );
102     }
103 
104 
105     /**
106      * Serialize an int
107      * 
108      * @param value the value to serialize
109      * @return The byte[] containing the serialized int
110      */
111     public static byte[] serialize( int value )
112     {
113         byte[] bytes = new byte[4];
114 
115         return serialize( bytes, 0, value );
116     }
117 
118 
119     /**
120      * Serialize an int
121      * 
122      * @param buffer the Buffer that will contain the serialized value
123      * @param start the position in the buffer we will store the serialized int
124      * @param value the value to serialize
125      * @return The byte[] containing the serialized int
126      */
127     public static byte[] serialize( byte[] buffer, int start, int value )
128     {
129         buffer[start] = ( byte ) ( value >>> 24 );
130         buffer[start + 1] = ( byte ) ( value >>> 16 );
131         buffer[start + 2] = ( byte ) ( value >>> 8 );
132         buffer[start + 3] = ( byte ) ( value );
133 
134         return buffer;
135     }
136 }