HBASE-24163 MOB compactor implementations should use format specifiers when calling...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / util / ByteRangeUtils.java
blob9acfa26c05cad07b4e78b4b06c4f854f726db17b
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.util;
20 import java.io.IOException;
21 import java.io.OutputStream;
22 import java.util.ArrayList;
23 import java.util.Collection;
25 import org.apache.yetus.audience.InterfaceAudience;
27 import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
29 /**
30 * Utility methods for working with {@link ByteRange}.
32 @InterfaceAudience.Public
33 public class ByteRangeUtils {
34 public static int numEqualPrefixBytes(ByteRange left, ByteRange right, int rightInnerOffset) {
35 int maxCompares = Math.min(left.getLength(), right.getLength() - rightInnerOffset);
36 final byte[] lbytes = left.getBytes();
37 final byte[] rbytes = right.getBytes();
38 final int loffset = left.getOffset();
39 final int roffset = right.getOffset();
40 for (int i = 0; i < maxCompares; ++i) {
41 if (lbytes[loffset + i] != rbytes[roffset + rightInnerOffset + i]) {
42 return i;
45 return maxCompares;
48 public static ArrayList<byte[]> copyToNewArrays(Collection<ByteRange> ranges) {
49 if (ranges == null) {
50 return new ArrayList<>(0);
52 ArrayList<byte[]> arrays = Lists.newArrayListWithCapacity(ranges.size());
53 for (ByteRange range : ranges) {
54 arrays.add(range.deepCopyToNewArray());
56 return arrays;
59 public static ArrayList<ByteRange> fromArrays(Collection<byte[]> arrays) {
60 if (arrays == null) {
61 return new ArrayList<>(0);
63 ArrayList<ByteRange> ranges = Lists.newArrayListWithCapacity(arrays.size());
64 for (byte[] array : arrays) {
65 ranges.add(new SimpleMutableByteRange(array));
67 return ranges;
70 public static void write(OutputStream os, ByteRange byteRange) throws IOException {
71 os.write(byteRange.getBytes(), byteRange.getOffset(), byteRange.getLength());
74 public static void write(OutputStream os, ByteRange byteRange, int byteRangeInnerOffset)
75 throws IOException {
76 os.write(byteRange.getBytes(), byteRange.getOffset() + byteRangeInnerOffset,
77 byteRange.getLength() - byteRangeInnerOffset);