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 this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * 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, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
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  }