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