1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util.vint;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import org.apache.hadoop.hbase.classification.InterfaceAudience;
26
27
28
29
30
31 @InterfaceAudience.Private
32 public class UVIntTool {
33
34 public static final byte
35 BYTE_7_RIGHT_BITS_SET = 127,
36 BYTE_LEFT_BIT_SET = -128;
37
38 public static final long
39 INT_7_RIGHT_BITS_SET = 127,
40 INT_8TH_BIT_SET = 128;
41
42 public static final byte[]
43 MAX_VALUE_BYTES = new byte[] { -1, -1, -1, -1, 7 };
44
45
46
47 public static int numBytes(int in) {
48 if (in == 0) {
49
50 return 1;
51 }
52 return (38 - Integer.numberOfLeadingZeros(in)) / 7;
53 }
54
55 public static byte[] getBytes(int value) {
56 int numBytes = numBytes(value);
57 byte[] bytes = new byte[numBytes];
58 int remainder = value;
59 for (int i = 0; i < numBytes - 1; ++i) {
60
61 bytes[i] = (byte) ((remainder & INT_7_RIGHT_BITS_SET) | INT_8TH_BIT_SET);
62 remainder >>= 7;
63 }
64
65 bytes[numBytes - 1] = (byte) (remainder & INT_7_RIGHT_BITS_SET);
66 return bytes;
67 }
68
69 public static int writeBytes(int value, OutputStream os) throws IOException {
70 int numBytes = numBytes(value);
71 int remainder = value;
72 for (int i = 0; i < numBytes - 1; ++i) {
73
74 os.write((byte) ((remainder & INT_7_RIGHT_BITS_SET) | INT_8TH_BIT_SET));
75 remainder >>= 7;
76 }
77
78 os.write((byte) (remainder & INT_7_RIGHT_BITS_SET));
79 return numBytes;
80 }
81
82
83
84 public static int getInt(byte[] bytes) {
85 return getInt(bytes, 0);
86 }
87
88 public static int getInt(byte[] bytes, int offset) {
89 int value = 0;
90 for (int i = 0;; ++i) {
91 byte b = bytes[offset + i];
92 int shifted = BYTE_7_RIGHT_BITS_SET & b;
93 shifted <<= 7 * i;
94 value |= shifted;
95 if (b >= 0) {
96 break;
97 }
98 }
99 return value;
100 }
101
102 public static int getInt(InputStream is) throws IOException {
103 int value = 0;
104 int i = 0;
105 int b;
106 do{
107 b = is.read();
108 int shifted = BYTE_7_RIGHT_BITS_SET & b;
109 shifted <<= 7 * i;
110 value |= shifted;
111 ++i;
112 }while(b > Byte.MAX_VALUE);
113 return value;
114 }
115 }