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