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;
20
21 import org.apache.hadoop.hbase.classification.InterfaceAudience;
22 import org.apache.hadoop.hbase.classification.InterfaceStability;
23
24
25
26
27 @InterfaceAudience.Public
28 @InterfaceStability.Evolving
29 public class SimpleMutableByteRange extends AbstractByteRange {
30
31
32
33
34
35 public SimpleMutableByteRange() {
36 unset();
37 }
38
39
40
41
42
43
44
45
46
47 public SimpleMutableByteRange(int capacity) {
48 this(new byte[capacity]);
49 }
50
51
52
53
54
55
56
57 public SimpleMutableByteRange(byte[] bytes) {
58 set(bytes);
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72 public SimpleMutableByteRange(byte[] bytes, int offset, int length) {
73 set(bytes, offset, length);
74 }
75
76 @Override
77 public ByteRange unset() {
78 clearHashCache();
79 bytes = null;
80 offset = 0;
81 length = 0;
82 return this;
83 }
84
85 @Override
86 public ByteRange put(int index, byte val) {
87 bytes[offset + index] = val;
88 clearHashCache();
89 return this;
90 }
91
92 @Override
93 public ByteRange put(int index, byte[] val) {
94 if (0 == val.length)
95 return this;
96 return put(index, val, 0, val.length);
97 }
98
99 @Override
100 public ByteRange put(int index, byte[] val, int offset, int length) {
101 if (0 == length)
102 return this;
103 System.arraycopy(val, offset, this.bytes, this.offset + index, length);
104 clearHashCache();
105 return this;
106 }
107
108 @Override
109 public ByteRange putShort(int index, short val) {
110
111
112
113 bytes[offset + index + 1] = (byte) val;
114 val >>= 8;
115 bytes[offset + index] = (byte) val;
116 clearHashCache();
117 return this;
118 }
119
120 @Override
121 public ByteRange putInt(int index, int val) {
122
123
124
125 for (int i = Bytes.SIZEOF_INT - 1; i > 0; i--) {
126 bytes[offset + index + i] = (byte) val;
127 val >>>= 8;
128 }
129 bytes[offset + index] = (byte) val;
130 clearHashCache();
131 return this;
132 }
133
134 @Override
135 public ByteRange putLong(int index, long val) {
136
137
138
139 for (int i = Bytes.SIZEOF_LONG - 1; i > 0; i--) {
140 bytes[offset + index + i] = (byte) val;
141 val >>>= 8;
142 }
143 bytes[offset + index] = (byte) val;
144 clearHashCache();
145 return this;
146 }
147
148
149 @Override
150 public int putVLong(int index, long val) {
151 int rPos = 0;
152 while (true) {
153 if ((val & ~0x7F) == 0) {
154 bytes[offset + index + rPos] = (byte) val;
155 break;
156 } else {
157 bytes[offset + index + rPos] = (byte) ((val & 0x7F) | 0x80);
158 val >>>= 7;
159 }
160 rPos++;
161 }
162 clearHashCache();
163 return rPos + 1;
164 }
165
166 @Override
167 public ByteRange deepCopy() {
168 SimpleMutableByteRange clone = new SimpleMutableByteRange(deepCopyToNewArray());
169 if (isHashCached()) {
170 clone.hash = hash;
171 }
172 return clone;
173 }
174
175 @Override
176 public ByteRange shallowCopy() {
177 SimpleMutableByteRange clone = new SimpleMutableByteRange(bytes, offset, length);
178 if (isHashCached()) {
179 clone.hash = hash;
180 }
181 return clone;
182 }
183
184 @Override
185 public ByteRange shallowCopySubRange(int innerOffset, int copyLength) {
186 SimpleMutableByteRange clone = new SimpleMutableByteRange(bytes, offset + innerOffset,
187 copyLength);
188 if (isHashCached()) {
189 clone.hash = hash;
190 }
191 return clone;
192 }
193
194 @Override
195 public boolean equals(Object thatObject) {
196 if (thatObject == null) {
197 return false;
198 }
199 if (this == thatObject) {
200 return true;
201 }
202 if (hashCode() != thatObject.hashCode()) {
203 return false;
204 }
205 if (!(thatObject instanceof SimpleMutableByteRange)) {
206 return false;
207 }
208 SimpleMutableByteRange that = (SimpleMutableByteRange) thatObject;
209 return Bytes.equals(bytes, offset, length, that.bytes, that.offset, that.length);
210 }
211
212 }