1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.accumulo.core.data;
18
19 import java.io.Serializable;
20
21 public class ArrayByteSequence extends ByteSequence implements Serializable {
22
23 private static final long serialVersionUID = 1L;
24
25 protected byte data[];
26 protected int offset;
27 protected int length;
28
29 public ArrayByteSequence(byte data[]) {
30 this.data = data;
31 this.offset = 0;
32 this.length = data.length;
33 }
34
35 public ArrayByteSequence(byte data[], int offset, int length) {
36
37 if (offset < 0 || offset > data.length || length < 0 || (offset + length) > data.length) {
38 throw new IllegalArgumentException(" Bad offset and/or length data.length = " + data.length + " offset = " + offset + " length = " + length);
39 }
40
41 this.data = data;
42 this.offset = offset;
43 this.length = length;
44
45 }
46
47 public ArrayByteSequence(String s) {
48 this(s.getBytes());
49 }
50
51 @Override
52 public byte byteAt(int i) {
53
54 if (i < 0) {
55 throw new IllegalArgumentException("i < 0, " + i);
56 }
57
58 if (i >= length) {
59 throw new IllegalArgumentException("i >= length, " + i + " >= " + length);
60 }
61
62 return data[offset + i];
63 }
64
65 @Override
66 public byte[] getBackingArray() {
67 return data;
68 }
69
70 @Override
71 public boolean isBackedByArray() {
72 return true;
73 }
74
75 @Override
76 public int length() {
77 return length;
78 }
79
80 @Override
81 public int offset() {
82 return offset;
83 }
84
85 @Override
86 public ByteSequence subSequence(int start, int end) {
87
88 if (start > end || start < 0 || end > length) {
89 throw new IllegalArgumentException("Bad start and/end start = " + start + " end=" + end + " offset=" + offset + " length=" + length);
90 }
91
92 return new ArrayByteSequence(data, offset + start, end - start);
93 }
94
95 @Override
96 public byte[] toArray() {
97 if (offset == 0 && length == data.length)
98 return data;
99
100 byte[] copy = new byte[length];
101 System.arraycopy(data, offset, copy, 0, length);
102 return copy;
103 }
104
105 public String toString() {
106 return new String(data, offset, length);
107 }
108 }