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.IntComparator;
27 import org.apache.directory.mavibot.btree.exception.SerializerCreationException;
28
29
30
31
32
33
34
35 public class IntSerializer extends AbstractElementSerializer<Integer>
36 {
37
38
39
40 public IntSerializer()
41 {
42 super( new IntComparator() );
43 }
44
45
46
47
48
49
50
51 public static Integer deserialize( byte[] in )
52 {
53 return deserialize( in, 0 );
54 }
55
56
57
58
59
60
61
62
63 public static Integer deserialize( byte[] in, int start )
64 {
65 if ( ( in == null ) || ( in.length < 4 + start ) )
66 {
67 throw new SerializerCreationException( "Cannot extract a Integer from a buffer with not enough bytes" );
68 }
69
70 return ( in[start] << 24 ) +
71 ( ( in[start + 1] & 0xFF ) << 16 ) +
72 ( ( in[start + 2] & 0xFF ) << 8 ) +
73 ( in[start + 3] & 0xFF );
74 }
75
76
77
78
79
80
81
82 public Integer fromBytes( byte[] in )
83 {
84 return deserialize( in, 0 );
85 }
86
87
88
89
90
91
92
93
94 public Integer fromBytes( byte[] in, int start )
95 {
96 if ( ( in == null ) || ( in.length < 4 + start ) )
97 {
98 throw new SerializerCreationException( "Cannot extract a Integer from a buffer with not enough bytes" );
99 }
100
101 return ( in[start] << 24 ) +
102 ( ( in[start + 1] & 0xFF ) << 16 ) +
103 ( ( in[start + 2] & 0xFF ) << 8 ) +
104 ( in[start + 3] & 0xFF );
105 }
106
107
108
109
110
111 public Integer deserialize( ByteBuffer buffer ) throws IOException
112 {
113 return buffer.getInt();
114 }
115
116
117
118
119
120 public Integer deserialize( BufferHandler bufferHandler ) throws IOException
121 {
122 byte[] in = bufferHandler.read( 4 );
123
124 return deserialize( in );
125 }
126
127
128
129
130
131 public byte[] serialize( Integer element )
132 {
133 return serialize( element.intValue() );
134 }
135
136
137
138
139
140
141
142
143 public static byte[] serialize( int value )
144 {
145 byte[] bytes = new byte[4];
146
147 return serialize( bytes, 0, value );
148 }
149
150
151
152
153
154
155
156
157
158
159 public static byte[] serialize( byte[] buffer, int start, int value )
160 {
161 buffer[start] = ( byte ) ( value >>> 24 );
162 buffer[start + 1] = ( byte ) ( value >>> 16 );
163 buffer[start + 2] = ( byte ) ( value >>> 8 );
164 buffer[start + 3] = ( byte ) ( value );
165
166 return buffer;
167 }
168 }