View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 }