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  
19  package org.apache.hadoop.hbase.util;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  
29  import com.google.common.collect.Lists;
30  
31  /**
32   * Utility methods for working with {@link ByteRange}.
33   */
34  @InterfaceAudience.Public
35  @InterfaceStability.Evolving
36  public class ByteRangeUtils {
37  
38    public static int numEqualPrefixBytes(ByteRange left, ByteRange right, int rightInnerOffset) {
39      int maxCompares = Math.min(left.getLength(), right.getLength() - rightInnerOffset);
40      final byte[] lbytes = left.getBytes(), rbytes = right.getBytes();
41      final int loffset = left.getOffset(), 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<byte[]>(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<ByteRange>(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  
82  }