1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.io;
20
21 import java.io.IOException;
22 import java.io.DataInput;
23 import java.io.DataOutput;
24 import java.util.Arrays;
25 import java.util.List;
26
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.hbase.classification.InterfaceStability;
29 import org.apache.hadoop.io.BytesWritable;
30 import org.apache.hadoop.io.WritableComparable;
31 import org.apache.hadoop.io.WritableComparator;
32
33
34
35
36
37
38
39
40
41
42 @InterfaceAudience.Public
43 @InterfaceStability.Stable
44 @edu.umd.cs.findbugs.annotations.SuppressWarnings(
45 value="EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS",
46 justification="It has been like this forever")
47 public class ImmutableBytesWritable
48 implements WritableComparable<ImmutableBytesWritable> {
49 private byte[] bytes;
50 private int offset;
51 private int length;
52
53
54
55
56 public ImmutableBytesWritable() {
57 super();
58 }
59
60
61
62
63
64 public ImmutableBytesWritable(byte[] bytes) {
65 this(bytes, 0, bytes.length);
66 }
67
68
69
70
71
72
73 public ImmutableBytesWritable(final ImmutableBytesWritable ibw) {
74 this(ibw.get(), ibw.getOffset(), ibw.getLength());
75 }
76
77
78
79
80
81
82
83 public ImmutableBytesWritable(final byte[] bytes, final int offset,
84 final int length) {
85 this.bytes = bytes;
86 this.offset = offset;
87 this.length = length;
88 }
89
90
91
92
93
94 public byte [] get() {
95 if (this.bytes == null) {
96 throw new IllegalStateException("Uninitialiized. Null constructor " +
97 "called w/o accompaying readFields invocation");
98 }
99 return this.bytes;
100 }
101
102
103
104
105 public void set(final byte [] b) {
106 set(b, 0, b.length);
107 }
108
109
110
111
112
113
114 public void set(final byte [] b, final int offset, final int length) {
115 this.bytes = b;
116 this.offset = offset;
117 this.length = length;
118 }
119
120
121
122
123
124
125
126 @Deprecated
127 public int getSize() {
128 if (this.bytes == null) {
129 throw new IllegalStateException("Uninitialiized. Null constructor " +
130 "called w/o accompaying readFields invocation");
131 }
132 return this.length;
133 }
134
135
136
137
138 public int getLength() {
139 if (this.bytes == null) {
140 throw new IllegalStateException("Uninitialiized. Null constructor " +
141 "called w/o accompaying readFields invocation");
142 }
143 return this.length;
144 }
145
146
147
148
149 public int getOffset(){
150 return this.offset;
151 }
152
153 @Override
154 public void readFields(final DataInput in) throws IOException {
155 this.length = in.readInt();
156 this.bytes = new byte[this.length];
157 in.readFully(this.bytes, 0, this.length);
158 this.offset = 0;
159 }
160
161 @Override
162 public void write(final DataOutput out) throws IOException {
163 out.writeInt(this.length);
164 out.write(this.bytes, this.offset, this.length);
165 }
166
167
168 @Override
169 public int hashCode() {
170 int hash = 1;
171 for (int i = offset; i < offset + length; i++)
172 hash = (31 * hash) + (int)bytes[i];
173 return hash;
174 }
175
176
177
178
179
180
181
182 @Override
183 public int compareTo(ImmutableBytesWritable that) {
184 return WritableComparator.compareBytes(
185 this.bytes, this.offset, this.length,
186 that.bytes, that.offset, that.length);
187 }
188
189
190
191
192
193
194
195 public int compareTo(final byte [] that) {
196 return WritableComparator.compareBytes(
197 this.bytes, this.offset, this.length,
198 that, 0, that.length);
199 }
200
201
202
203
204 @Override
205 public boolean equals(Object right_obj) {
206 if (right_obj instanceof byte []) {
207 return compareTo((byte [])right_obj) == 0;
208 }
209 if (right_obj instanceof ImmutableBytesWritable) {
210 return compareTo((ImmutableBytesWritable)right_obj) == 0;
211 }
212 return false;
213 }
214
215
216
217
218 @Override
219 public String toString() {
220 StringBuilder sb = new StringBuilder(3*this.length);
221 final int endIdx = this.offset + this.length;
222 for (int idx = this.offset; idx < endIdx ; idx++) {
223 sb.append(' ');
224 String num = Integer.toHexString(0xff & this.bytes[idx]);
225
226 if (num.length() < 2) {
227 sb.append('0');
228 }
229 sb.append(num);
230 }
231 return sb.length() > 0 ? sb.substring(1) : "";
232 }
233
234
235
236 @InterfaceAudience.Public
237 @InterfaceStability.Stable
238 public static class Comparator extends WritableComparator {
239 private BytesWritable.Comparator comparator =
240 new BytesWritable.Comparator();
241
242
243 public Comparator() {
244 super(ImmutableBytesWritable.class);
245 }
246
247
248
249
250 @Override
251 public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
252 return comparator.compare(b1, s1, l1, b2, s2, l2);
253 }
254 }
255
256 static {
257 WritableComparator.define(ImmutableBytesWritable.class, new Comparator());
258 }
259
260
261
262
263
264 public static byte [][] toArray(final List<byte []> array) {
265
266 byte[][] results = new byte[array.size()][];
267 for (int i = 0; i < array.size(); i++) {
268 results[i] = array.get(i);
269 }
270 return results;
271 }
272
273
274
275
276 public byte[] copyBytes() {
277 return Arrays.copyOfRange(bytes, offset, offset+length);
278 }
279 }