1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
32
33
34
35 public class LongSerializer extends AbstractElementSerializer<Long>
36 {
37
38
39
40 public LongSerializer()
41 {
42 super( new LongComparator() );
43 }
44
45
46
47
48
49 public byte[] serialize( Long element )
50 {
51 return serialize( element.longValue() );
52 }
53
54
55
56
57
58
59
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
71
72
73
74
75
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
94
95
96
97
98 public static Long deserialize( byte[] in )
99 {
100 return deserialize( in, 0 );
101 }
102
103
104
105
106
107
108
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
132
133
134
135
136 public Long fromBytes( byte[] in )
137 {
138 return deserialize( in, 0 );
139 }
140
141
142
143
144
145
146
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
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
181
182 public Long deserialize( ByteBuffer buffer ) throws IOException
183 {
184 return buffer.getLong();
185 }
186 }