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