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 public final static LongSerializer INSTANCE = new LongSerializer();
39
40
41
42
43 private LongSerializer()
44 {
45 super( LongComparator.INSTANCE );
46 }
47
48
49
50
51
52 public byte[] serialize( Long element )
53 {
54 return serialize( element.longValue() );
55 }
56
57
58
59
60
61
62
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
74
75
76
77
78
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
97
98
99
100
101 public static Long deserialize( byte[] in )
102 {
103 return deserialize( in, 0 );
104 }
105
106
107
108
109
110
111
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
135
136
137
138
139 public Long fromBytes( byte[] in )
140 {
141 return deserialize( in, 0 );
142 }
143
144
145
146
147
148
149
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
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
184
185 public Long deserialize( ByteBuffer buffer ) throws IOException
186 {
187 return buffer.getLong();
188 }
189 }