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