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