1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.accumulo.core.file.rfile.bcfile;
19
20 import org.apache.hadoop.io.BytesWritable;
21
22 /**
23 * Adaptor class to wrap byte-array backed objects (including java byte array) as RawComparable objects.
24 */
25 public final class ByteArray implements RawComparable {
26 private final byte[] buffer;
27 private final int offset;
28 private final int len;
29
30 /**
31 * Constructing a ByteArray from a {@link BytesWritable}.
32 *
33 * @param other
34 */
35 public ByteArray(BytesWritable other) {
36 this(other.getBytes(), 0, other.getLength());
37 }
38
39 /**
40 * Wrap a whole byte array as a RawComparable.
41 *
42 * @param buffer
43 * the byte array buffer.
44 */
45 public ByteArray(byte[] buffer) {
46 this(buffer, 0, buffer.length);
47 }
48
49 /**
50 * Wrap a partial byte array as a RawComparable.
51 *
52 * @param buffer
53 * the byte array buffer.
54 * @param offset
55 * the starting offset
56 * @param len
57 * the length of the consecutive bytes to be wrapped.
58 */
59 public ByteArray(byte[] buffer, int offset, int len) {
60 if ((offset | len | (buffer.length - offset - len)) < 0) {
61 throw new IndexOutOfBoundsException();
62 }
63 this.buffer = buffer;
64 this.offset = offset;
65 this.len = len;
66 }
67
68 /**
69 * @return the underlying buffer.
70 */
71 @Override
72 public byte[] buffer() {
73 return buffer;
74 }
75
76 /**
77 * @return the offset in the buffer.
78 */
79 @Override
80 public int offset() {
81 return offset;
82 }
83
84 /**
85 * @return the size of the byte array.
86 */
87 @Override
88 public int size() {
89 return len;
90 }
91 }