View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.util;
19  
20  import com.google.common.collect.Lists;
21  
22  import java.io.IOException;
23  import java.io.OutputStream;
24  import java.util.ArrayList;
25  import java.util.Collection;
26  
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.classification.InterfaceStability;
29  
30  /**
31   * Utility methods for working with {@link ByteRange}.
32   */
33  @InterfaceAudience.Public
34  @InterfaceStability.Evolving
35  public class ByteRangeUtils {
36    public static int numEqualPrefixBytes(ByteRange left, ByteRange right, int rightInnerOffset) {
37      int maxCompares = Math.min(left.getLength(), right.getLength() - rightInnerOffset);
38      final byte[] lbytes = left.getBytes();
39      final byte[] rbytes = right.getBytes();
40      final int loffset = left.getOffset();
41      final int roffset = right.getOffset();
42      for (int i = 0; i < maxCompares; ++i) {
43        if (lbytes[loffset + i] != rbytes[roffset + rightInnerOffset + i]) {
44          return i;
45        }
46      }
47      return maxCompares;
48    }
49  
50    public static ArrayList<byte[]> copyToNewArrays(Collection<ByteRange> ranges) {
51      if (ranges == null) {
52        return new ArrayList<>(0);
53      }
54      ArrayList<byte[]> arrays = Lists.newArrayListWithCapacity(ranges.size());
55      for (ByteRange range : ranges) {
56        arrays.add(range.deepCopyToNewArray());
57      }
58      return arrays;
59    }
60  
61    public static ArrayList<ByteRange> fromArrays(Collection<byte[]> arrays) {
62      if (arrays == null) {
63        return new ArrayList<>(0);
64      }
65      ArrayList<ByteRange> ranges = Lists.newArrayListWithCapacity(arrays.size());
66      for (byte[] array : arrays) {
67        ranges.add(new SimpleMutableByteRange(array));
68      }
69      return ranges;
70    }
71  
72    public static void write(OutputStream os, ByteRange byteRange) throws IOException {
73      os.write(byteRange.getBytes(), byteRange.getOffset(), byteRange.getLength());
74    }
75  
76    public static void write(OutputStream os, ByteRange byteRange, int byteRangeInnerOffset)
77        throws IOException {
78      os.write(byteRange.getBytes(), byteRange.getOffset() + byteRangeInnerOffset,
79        byteRange.getLength() - byteRangeInnerOffset);
80    }
81  }