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
60
61
62
63 @InterfaceAudience.Public
64 @InterfaceStability.Evolving
65 @edu.umd.cs.findbugs.annotations.SuppressWarnings("EQ_DOESNT_OVERRIDE_EQUALS")
66 public class SimplePositionedMutableByteRange extends AbstractPositionedByteRange {
67
68
69
70
71 public SimplePositionedMutableByteRange() {
72 super();
73 }
74
75
76
77
78
79
80
81
82
83 public SimplePositionedMutableByteRange(int capacity) {
84 this(new byte[capacity]);
85 }
86
87
88
89
90
91
92
93 public SimplePositionedMutableByteRange(byte[] bytes) {
94 set(bytes);
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108 public SimplePositionedMutableByteRange(byte[] bytes, int offset, int length) {
109 set(bytes, offset, length);
110 }
111
112 @Override
113 public PositionedByteRange unset() {
114 this.position = 0;
115 clearHashCache();
116 bytes = null;
117 offset = 0;
118 length = 0;
119 return this;
120 }
121
122 @Override
123 public PositionedByteRange set(int capacity) {
124 this.position = 0;
125 super.set(capacity);
126 this.limit = capacity;
127 return this;
128 }
129
130 @Override
131 public PositionedByteRange set(byte[] bytes) {
132 this.position = 0;
133 super.set(bytes);
134 this.limit = bytes.length;
135 return this;
136 }
137
138 @Override
139 public PositionedByteRange set(byte[] bytes, int offset, int length) {
140 this.position = 0;
141 super.set(bytes, offset, length);
142 limit = length;
143 return this;
144 }
145
146
147
148
149
150
151
152
153
154 @Override
155 public PositionedByteRange setOffset(int offset) {
156 this.position = 0;
157 super.setOffset(offset);
158 return this;
159 }
160
161
162
163
164
165
166
167
168
169
170 @Override
171 public PositionedByteRange setLength(int length) {
172 this.position = Math.min(position, length);
173 super.setLength(length);
174 return this;
175 }
176
177 @Override
178 public PositionedByteRange put(byte val) {
179 put(position++, val);
180 return this;
181 }
182
183 @Override
184 public PositionedByteRange put(byte[] val) {
185 if (0 == val.length)
186 return this;
187 return this.put(val, 0, val.length);
188 }
189
190 @Override
191 public PositionedByteRange put(byte[] val, int offset, int length) {
192 if (0 == length)
193 return this;
194 put(position, val, offset, length);
195 this.position += length;
196 return this;
197 }
198
199 @Override
200 public PositionedByteRange get(int index, byte[] dst) {
201 super.get(index, dst);
202 return this;
203 }
204
205 @Override
206 public PositionedByteRange get(int index, byte[] dst, int offset, int length) {
207 super.get(index, dst, offset, length);
208 return this;
209 }
210
211 @Override
212 public PositionedByteRange put(int index, byte val) {
213 bytes[offset + index] = val;
214 return this;
215 }
216
217 @Override
218 public PositionedByteRange put(int index, byte[] val) {
219 if (0 == val.length)
220 return this;
221 return put(index, val, 0, val.length);
222 }
223
224 @Override
225 public PositionedByteRange put(int index, byte[] val, int offset, int length) {
226 if (0 == length)
227 return this;
228 System.arraycopy(val, offset, this.bytes, this.offset + index, length);
229 return this;
230 }
231
232 @Override
233 public PositionedByteRange deepCopy() {
234 SimplePositionedMutableByteRange clone = new SimplePositionedMutableByteRange(
235 deepCopyToNewArray());
236 clone.position = this.position;
237 return clone;
238 }
239
240 @Override
241 public PositionedByteRange shallowCopy() {
242 SimplePositionedMutableByteRange clone = new SimplePositionedMutableByteRange(bytes, offset,
243 length);
244 clone.position = this.position;
245 return clone;
246 }
247
248 @Override
249 public PositionedByteRange shallowCopySubRange(int innerOffset, int copyLength) {
250 SimplePositionedMutableByteRange clone = new SimplePositionedMutableByteRange(bytes, offset
251 + innerOffset, copyLength);
252 clone.position = this.position;
253 return clone;
254 }
255
256 @Override
257 public PositionedByteRange putShort(short val) {
258 putShort(position, val);
259 position += Bytes.SIZEOF_SHORT;
260 return this;
261 }
262
263 @Override
264 public PositionedByteRange putInt(int val) {
265 putInt(position, val);
266 position += Bytes.SIZEOF_INT;
267 return this;
268 }
269
270 @Override
271 public PositionedByteRange putLong(long val) {
272 putLong(position, val);
273 position += Bytes.SIZEOF_LONG;
274 return this;
275 }
276
277 @Override
278 public int putVLong(long val) {
279 int len = putVLong(position, val);
280 position += len;
281 return len;
282 }
283
284 @Override
285 public PositionedByteRange putShort(int index, short val) {
286
287
288
289 bytes[offset + index + 1] = (byte) val;
290 val >>= 8;
291 bytes[offset + index] = (byte) val;
292 clearHashCache();
293 return this;
294 }
295
296 @Override
297 public PositionedByteRange putInt(int index, int val) {
298
299
300
301 for (int i = Bytes.SIZEOF_INT - 1; i > 0; i--) {
302 bytes[offset + index + i] = (byte) val;
303 val >>>= 8;
304 }
305 bytes[offset + index] = (byte) val;
306 clearHashCache();
307 return this;
308 }
309
310 @Override
311 public PositionedByteRange putLong(int index, long val) {
312
313
314
315 for (int i = Bytes.SIZEOF_LONG - 1; i > 0; i--) {
316 bytes[offset + index + i] = (byte) val;
317 val >>>= 8;
318 }
319 bytes[offset + index] = (byte) val;
320 clearHashCache();
321 return this;
322 }
323
324
325 @Override
326 public int putVLong(int index, long val) {
327 int rPos = 0;
328 while (true) {
329 if ((val & ~0x7F) == 0) {
330 bytes[offset + index + rPos] = (byte) val;
331 break;
332 } else {
333 bytes[offset + index + rPos] = (byte) ((val & 0x7F) | 0x80);
334 val >>>= 7;
335 }
336 rPos++;
337 }
338 clearHashCache();
339 return rPos + 1;
340 }
341
342
343 }