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