This project has retired. For details please refer to its
Attic page.
Varint xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.utils;
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 import java.io.DataInput;
40 import java.io.DataOutput;
41 import java.io.IOException;
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 public final class Varint {
57
58
59
60
61 private Varint() {
62 }
63
64
65
66
67
68
69
70
71
72
73
74 private static void writeVarLong(
75 long value,
76 DataOutput out
77 ) throws IOException {
78 while (true) {
79 int bits = ((int) value) & 0x7f;
80 value >>>= 7;
81 if (value == 0) {
82 out.writeByte((byte) bits);
83 return;
84 }
85 out.writeByte((byte) (bits | 0x80));
86 }
87 }
88
89
90
91
92
93
94
95
96
97
98
99 public static void writeUnsignedVarLong(
100 long value,
101 DataOutput out
102 ) throws IOException {
103 if (value < 0) {
104 throw new IllegalStateException(
105 "Negative value passed into writeUnsignedVarLong - " + value);
106 }
107 writeVarLong(value, out);
108 }
109
110
111
112
113
114
115
116
117
118 public static void writeSignedVarLong(
119 long value,
120 DataOutput out
121 ) throws IOException {
122 writeVarLong((value << 1) ^ (value >> 63), out);
123 }
124
125
126
127
128
129
130
131 private static void writeVarInt(
132 int value,
133 DataOutput out
134 ) throws IOException {
135 while (true) {
136 int bits = value & 0x7f;
137 value >>>= 7;
138 if (value == 0) {
139 out.writeByte((byte) bits);
140 return;
141 }
142 out.writeByte((byte) (bits | 0x80));
143 }
144 }
145
146
147
148
149
150
151
152 public static void writeUnsignedVarInt(
153 int value,
154 DataOutput out
155 ) throws IOException {
156 if (value < 0) {
157 throw new IllegalStateException(
158 "Negative value passed into writeUnsignedVarInt - " + value);
159 }
160 writeVarInt(value, out);
161 }
162
163
164
165
166
167
168
169
170
171 public static void writeSignedVarInt(
172 int value,
173 DataOutput out
174 ) throws IOException {
175 writeVarInt((value << 1) ^ (value >> 31), out);
176 }
177
178
179
180
181
182
183
184 public static long readUnsignedVarLong(DataInput in) throws IOException {
185 long tmp;
186
187 if ((tmp = in.readByte()) >= 0) {
188 return tmp;
189 }
190 long result = tmp & 0x7f;
191 if ((tmp = in.readByte()) >= 0) {
192 result |= tmp << 7;
193 } else {
194 result |= (tmp & 0x7f) << 7;
195 if ((tmp = in.readByte()) >= 0) {
196 result |= tmp << 14;
197 } else {
198 result |= (tmp & 0x7f) << 14;
199 if ((tmp = in.readByte()) >= 0) {
200 result |= tmp << 21;
201 } else {
202 result |= (tmp & 0x7f) << 21;
203 if ((tmp = in.readByte()) >= 0) {
204 result |= tmp << 28;
205 } else {
206 result |= (tmp & 0x7f) << 28;
207 if ((tmp = in.readByte()) >= 0) {
208 result |= tmp << 35;
209 } else {
210 result |= (tmp & 0x7f) << 35;
211 if ((tmp = in.readByte()) >= 0) {
212 result |= tmp << 42;
213 } else {
214 result |= (tmp & 0x7f) << 42;
215 if ((tmp = in.readByte()) >= 0) {
216 result |= tmp << 49;
217 } else {
218 result |= (tmp & 0x7f) << 49;
219 if ((tmp = in.readByte()) >= 0) {
220 result |= tmp << 56;
221 } else {
222 result |= (tmp & 0x7f) << 56;
223 result |= ((long) in.readByte()) << 63;
224 }
225 }
226 }
227 }
228 }
229 }
230 }
231 }
232
233 return result;
234 }
235
236
237
238
239
240
241
242 public static long readSignedVarLong(DataInput in) throws IOException {
243 long raw = readUnsignedVarLong(in);
244 long temp = (((raw << 63) >> 63) ^ raw) >> 1;
245 return temp ^ (raw & (1L << 63));
246 }
247
248
249
250
251
252
253
254 public static int readUnsignedVarInt(DataInput in) throws IOException {
255 int tmp;
256
257 if ((tmp = in.readByte()) >= 0) {
258 return tmp;
259 }
260 int result = tmp & 0x7f;
261 if ((tmp = in.readByte()) >= 0) {
262 result |= tmp << 7;
263 } else {
264 result |= (tmp & 0x7f) << 7;
265 if ((tmp = in.readByte()) >= 0) {
266 result |= tmp << 14;
267 } else {
268 result |= (tmp & 0x7f) << 14;
269 if ((tmp = in.readByte()) >= 0) {
270 result |= tmp << 21;
271 } else {
272 result |= (tmp & 0x7f) << 21;
273 result |= (in.readByte()) << 28;
274 }
275 }
276 }
277
278 return result;
279 }
280
281
282
283
284
285
286
287 public static int readSignedVarInt(DataInput in) throws IOException {
288 int raw = readUnsignedVarInt(in);
289 int temp = (((raw << 31) >> 31) ^ raw) >> 1;
290 return temp ^ (raw & (1 << 31));
291 }
292
293
294
295
296
297
298
299
300 public static long sizeOfUnsignedVarLong(long value) throws IOException {
301 int result = 0;
302 do {
303 result++;
304 value >>>= 7;
305 } while (value != 0);
306 return result;
307 }
308
309
310
311
312
313
314
315
316 public static long sizeOfSignedVarLong(long value) throws IOException {
317 return sizeOfUnsignedVarLong((value << 1) ^ (value >> 63));
318 }
319
320
321
322
323
324
325
326
327 public static int sizeOfUnsignedVarInt(int value) throws IOException {
328 int cnt = 0;
329 do {
330 cnt++;
331 value >>>= 7;
332 } while (value != 0);
333 return cnt;
334 }
335
336
337
338
339
340
341
342
343 public static int sizeOfSignedVarInt(int value) throws IOException {
344 return sizeOfUnsignedVarInt((value << 1) ^ (value >> 31));
345 }
346
347 }