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 package org.apache.hadoop.hbase.util;
52
53 import org.apache.hadoop.hbase.classification.InterfaceAudience;
54 import org.apache.hadoop.hbase.classification.InterfaceStability;
55
56
57
58
59 @InterfaceAudience.Public
60 @InterfaceStability.Evolving
61 public class SimpleMutableByteRange extends AbstractByteRange {
62
63
64
65
66
67 public SimpleMutableByteRange() {
68 unset();
69 }
70
71
72
73
74
75
76
77
78
79 public SimpleMutableByteRange(int capacity) {
80 this(new byte[capacity]);
81 }
82
83
84
85
86
87
88
89 public SimpleMutableByteRange(byte[] bytes) {
90 set(bytes);
91 }
92
93
94
95
96
97
98
99
100
101
102
103
104 public SimpleMutableByteRange(byte[] bytes, int offset, int length) {
105 set(bytes, offset, length);
106 }
107
108 @Override
109 public ByteRange unset() {
110 clearHashCache();
111 bytes = null;
112 offset = 0;
113 length = 0;
114 return this;
115 }
116
117 @Override
118 public ByteRange put(int index, byte val) {
119 bytes[offset + index] = val;
120 clearHashCache();
121 return this;
122 }
123
124 @Override
125 public ByteRange put(int index, byte[] val) {
126 if (0 == val.length)
127 return this;
128 return put(index, val, 0, val.length);
129 }
130
131 @Override
132 public ByteRange put(int index, byte[] val, int offset, int length) {
133 if (0 == length)
134 return this;
135 System.arraycopy(val, offset, this.bytes, this.offset + index, length);
136 clearHashCache();
137 return this;
138 }
139
140 @Override
141 public ByteRange putShort(int index, short val) {
142
143
144
145 bytes[offset + index + 1] = (byte) val;
146 val >>= 8;
147 bytes[offset + index] = (byte) val;
148 clearHashCache();
149 return this;
150 }
151
152 @Override
153 public ByteRange putInt(int index, int val) {
154
155
156
157 for (int i = Bytes.SIZEOF_INT - 1; i > 0; i--) {
158 bytes[offset + index + i] = (byte) val;
159 val >>>= 8;
160 }
161 bytes[offset + index] = (byte) val;
162 clearHashCache();
163 return this;
164 }
165
166 @Override
167 public ByteRange putLong(int index, long val) {
168
169
170
171 for (int i = Bytes.SIZEOF_LONG - 1; i > 0; i--) {
172 bytes[offset + index + i] = (byte) val;
173 val >>>= 8;
174 }
175 bytes[offset + index] = (byte) val;
176 clearHashCache();
177 return this;
178 }
179
180
181 @Override
182 public int putVLong(int index, long val) {
183 int rPos = 0;
184 while (true) {
185 if ((val & ~0x7F) == 0) {
186 bytes[offset + index + rPos] = (byte) val;
187 break;
188 } else {
189 bytes[offset + index + rPos] = (byte) ((val & 0x7F) | 0x80);
190 val >>>= 7;
191 }
192 rPos++;
193 }
194 clearHashCache();
195 return rPos + 1;
196 }
197
198
199 @Override
200 public ByteRange deepCopy() {
201 SimpleMutableByteRange clone = new SimpleMutableByteRange(deepCopyToNewArray());
202 if (isHashCached()) {
203 clone.hash = hash;
204 }
205 return clone;
206 }
207
208 @Override
209 public ByteRange shallowCopy() {
210 SimpleMutableByteRange clone = new SimpleMutableByteRange(bytes, offset, length);
211 if (isHashCached()) {
212 clone.hash = hash;
213 }
214 return clone;
215 }
216
217 @Override
218 public ByteRange shallowCopySubRange(int innerOffset, int copyLength) {
219 SimpleMutableByteRange clone = new SimpleMutableByteRange(bytes, offset + innerOffset,
220 copyLength);
221 if (isHashCached()) {
222 clone.hash = hash;
223 }
224 return clone;
225 }
226
227 @Override
228 public boolean equals(Object thatObject) {
229 if (thatObject == null) {
230 return false;
231 }
232 if (this == thatObject) {
233 return true;
234 }
235 if (hashCode() != thatObject.hashCode()) {
236 return false;
237 }
238 if (!(thatObject instanceof SimpleMutableByteRange)) {
239 return false;
240 }
241 SimpleMutableByteRange that = (SimpleMutableByteRange) thatObject;
242 return Bytes.equals(bytes, offset, length, that.bytes, that.offset, that.length);
243 }
244
245 }