001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.hbase.util;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.util.ArrayList;
024import java.util.Collection;
025
026import org.apache.yetus.audience.InterfaceAudience;
027
028import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
029
030/**
031 * Utility methods for working with {@link ByteRange}.
032 */
033@InterfaceAudience.Public
034public class ByteRangeUtils {
035
036  public static int numEqualPrefixBytes(ByteRange left, ByteRange right, int rightInnerOffset) {
037    int maxCompares = Math.min(left.getLength(), right.getLength() - rightInnerOffset);
038    final byte[] lbytes = left.getBytes(), rbytes = right.getBytes();
039    final int loffset = left.getOffset(), roffset = right.getOffset();
040    for (int i = 0; i < maxCompares; ++i) {
041      if (lbytes[loffset + i] != rbytes[roffset + rightInnerOffset + i]) {
042        return i;
043      }
044    }
045    return maxCompares;
046  }
047
048  public static ArrayList<byte[]> copyToNewArrays(Collection<ByteRange> ranges) {
049    if (ranges == null) {
050      return new ArrayList<>(0);
051    }
052    ArrayList<byte[]> arrays = Lists.newArrayListWithCapacity(ranges.size());
053    for (ByteRange range : ranges) {
054      arrays.add(range.deepCopyToNewArray());
055    }
056    return arrays;
057  }
058
059  public static ArrayList<ByteRange> fromArrays(Collection<byte[]> arrays) {
060    if (arrays == null) {
061      return new ArrayList<>(0);
062    }
063    ArrayList<ByteRange> ranges = Lists.newArrayListWithCapacity(arrays.size());
064    for (byte[] array : arrays) {
065      ranges.add(new SimpleMutableByteRange(array));
066    }
067    return ranges;
068  }
069
070  public static void write(OutputStream os, ByteRange byteRange) throws IOException {
071    os.write(byteRange.getBytes(), byteRange.getOffset(), byteRange.getLength());
072  }
073
074  public static void write(OutputStream os, ByteRange byteRange, int byteRangeInnerOffset)
075      throws IOException {
076    os.write(byteRange.getBytes(), byteRange.getOffset() + byteRangeInnerOffset,
077      byteRange.getLength() - byteRangeInnerOffset);
078  }
079
080}