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 */
018package org.apache.hadoop.hbase.util;
019
020import java.io.IOException;
021import java.io.OutputStream;
022import java.util.ArrayList;
023import java.util.Collection;
024
025import org.apache.yetus.audience.InterfaceAudience;
026
027import org.apache.hbase.thirdparty.com.google.common.collect.Lists;
028
029/**
030 * Utility methods for working with {@link ByteRange}.
031 */
032@InterfaceAudience.Public
033public class ByteRangeUtils {
034  public static int numEqualPrefixBytes(ByteRange left, ByteRange right, int rightInnerOffset) {
035    int maxCompares = Math.min(left.getLength(), right.getLength() - rightInnerOffset);
036    final byte[] lbytes = left.getBytes();
037    final byte[] rbytes = right.getBytes();
038    final int loffset = left.getOffset();
039    final int 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}