-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
VarInt.java
223 lines (210 loc) · 6.41 KB
/
VarInt.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.util;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
/**
* Common methods to encode and decode varints and varlongs into ByteBuffers and
* arrays.
*/
public class VarInt {
/**
* Maximum encoded size of 32-bit positive integers (in bytes)
*/
public static final int MAX_VARINT_SIZE = 5;
/**
* maximum encoded size of 64-bit longs, and negative 32-bit ints (in bytes)
*/
public static final int MAX_VARLONG_SIZE = 10;
private VarInt() { }
/** Returns the encoding size in bytes of its input value.
* @param i the integer to be measured
* @return the encoding size in bytes of its input value
*/
public static int varIntSize(int i) {
int result = 0;
do {
result++;
i >>>= 7;
} while (i != 0);
return result;
}
/**
* Reads a varint from the current position of the given ByteBuffer and returns the decoded value
* as 32 bit integer.
*
* <p>The position of the buffer is advanced to the first byte after the decoded varint.
*
* @param src the ByteBuffer to get the var int from
* @return The integer value of the decoded varint
*/
public static int getVarInt(ByteBuffer src) {
int tmp;
if ((tmp = src.get()) >= 0) {
return tmp;
}
int result = tmp & 0x7f;
if ((tmp = src.get()) >= 0) {
result |= tmp << 7;
} else {
result |= (tmp & 0x7f) << 7;
if ((tmp = src.get()) >= 0) {
result |= tmp << 14;
} else {
result |= (tmp & 0x7f) << 14;
if ((tmp = src.get()) >= 0) {
result |= tmp << 21;
} else {
result |= (tmp & 0x7f) << 21;
result |= (tmp = src.get()) << 28;
while (tmp < 0) {
// We get into this loop only in the case of overflow.
// By doing this, we can call getVarInt() instead of
// getVarLong() when we only need an int.
tmp = src.get();
}
}
}
}
return result;
}
/**
* Encodes an integer in a variable-length encoding, 7 bits per byte, into a destination byte[],
* following the protocol buffer convention.
*
* @param v the int value to write to sink
* @param sink the sink buffer to write to
* @param offset the offset within sink to begin writing
* @return the updated offset after writing the varint
*/
public static int putVarInt(int v, byte[] sink, int offset) {
do {
// Encode next 7 bits + terminator bit
int bits = v & 0x7F;
v >>>= 7;
byte b = (byte) (bits + ((v != 0) ? 0x80 : 0));
sink[offset++] = b;
} while (v != 0);
return offset;
}
/**
* Encodes an integer in a variable-length encoding, 7 bits per byte, and writes it to the given
* OutputStream.
*
* @param v the value to encode
* @param outputStream the OutputStream to write to
*/
public static void putVarInt(int v, OutputStream outputStream) throws IOException {
byte[] bytes = new byte[varIntSize(v)];
putVarInt(v, bytes, 0);
outputStream.write(bytes);
}
/**
* Returns the encoding size in bytes of its input value.
*
* @param v the long to be measured
* @return the encoding size in bytes of a given long value.
*/
public static int varLongSize(long v) {
int result = 0;
do {
result++;
v >>>= 7;
} while (v != 0);
return result;
}
/**
* Reads an up to 64 bit long varint from the current position of the
* given ByteBuffer and returns the decoded value as long.
*
* <p>The position of the buffer is advanced to the first byte after the
* decoded varint.
*
* @param src the ByteBuffer to get the var int from
* @return The integer value of the decoded long varint
*/
public static long getVarLong(ByteBuffer src) {
long tmp;
if ((tmp = src.get()) >= 0) {
return tmp;
}
long result = tmp & 0x7f;
if ((tmp = src.get()) >= 0) {
result |= tmp << 7;
} else {
result |= (tmp & 0x7f) << 7;
if ((tmp = src.get()) >= 0) {
result |= tmp << 14;
} else {
result |= (tmp & 0x7f) << 14;
if ((tmp = src.get()) >= 0) {
result |= tmp << 21;
} else {
result |= (tmp & 0x7f) << 21;
if ((tmp = src.get()) >= 0) {
result |= tmp << 28;
} else {
result |= (tmp & 0x7f) << 28;
if ((tmp = src.get()) >= 0) {
result |= tmp << 35;
} else {
result |= (tmp & 0x7f) << 35;
if ((tmp = src.get()) >= 0) {
result |= tmp << 42;
} else {
result |= (tmp & 0x7f) << 42;
if ((tmp = src.get()) >= 0) {
result |= tmp << 49;
} else {
result |= (tmp & 0x7f) << 49;
if ((tmp = src.get()) >= 0) {
result |= tmp << 56;
} else {
result |= (tmp & 0x7f) << 56;
result |= ((long) src.get()) << 63;
}
}
}
}
}
}
}
}
return result;
}
/**
* Encodes a long integer in a variable-length encoding, 7 bits per byte, to a
* ByteBuffer sink.
* @param v the value to encode
* @param sink the ByteBuffer to add the encoded value
*/
public static void putVarLong(long v, ByteBuffer sink) {
while (true) {
int bits = ((int) v) & 0x7f;
v >>>= 7;
if (v == 0) {
sink.put((byte) bits);
return;
}
sink.put((byte) (bits | 0x80));
}
}
public static void putVarLong(long v, OutputStream outputStream) throws IOException {
byte[] bytes = new byte[varLongSize(v)];
ByteBuffer sink = ByteBuffer.wrap(bytes);
putVarLong(v, sink);
outputStream.write(bytes);
}
}