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 UVLongTool{
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 LONG_7_RIGHT_BITS_SET = 127,
40 LONG_8TH_BIT_SET = 128;
41
42 public static final byte[]
43 MAX_VALUE_BYTES = new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, 127 };
44
45
46
47
48 public static int numBytes(long in) {
49 if (in == 0) {
50 return 1;
51 }
52 return (70 - Long.numberOfLeadingZeros(in)) / 7;
53 }
54
55 public static byte[] getBytes(long value) {
56 int numBytes = numBytes(value);
57 byte[] bytes = new byte[numBytes];
58 long remainder = value;
59 for (int i = 0; i < numBytes - 1; ++i) {
60 bytes[i] = (byte) ((remainder & LONG_7_RIGHT_BITS_SET) | LONG_8TH_BIT_SET);
61 remainder >>= 7;
62 }
63 bytes[numBytes - 1] = (byte) (remainder & LONG_7_RIGHT_BITS_SET);
64 return bytes;
65 }
66
67 public static int writeBytes(long value, OutputStream os) throws IOException {
68 int numBytes = numBytes(value);
69 long remainder = value;
70 for (int i = 0; i < numBytes - 1; ++i) {
71
72 os.write((byte) ((remainder & LONG_7_RIGHT_BITS_SET) | LONG_8TH_BIT_SET));
73 remainder >>= 7;
74 }
75
76 os.write((byte) (remainder & LONG_7_RIGHT_BITS_SET));
77 return numBytes;
78 }
79
80
81
82 public static long getLong(byte[] bytes) {
83 return getLong(bytes, 0);
84 }
85
86 public static long getLong(byte[] bytes, int offset) {
87 long value = 0;
88 for (int i = 0;; ++i) {
89 byte b = bytes[offset + i];
90 long shifted = BYTE_7_RIGHT_BITS_SET & b;
91 shifted <<= 7 * i;
92 value |= shifted;
93 if (b >= 0) {
94 break;
95 }
96 }
97 return value;
98 }
99
100 public static long getLong(InputStream is) throws IOException {
101 long value = 0;
102 int i = 0;
103 int b;
104 do {
105 b = is.read();
106 long shifted = BYTE_7_RIGHT_BITS_SET & b;
107 shifted <<= 7 * i;
108 value |= shifted;
109 ++i;
110 } while (b > Byte.MAX_VALUE);
111 return value;
112 }
113 }