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 final class ByteRangeUtils { 034 private ByteRangeUtils() { 035 } 036 037 public static int numEqualPrefixBytes(ByteRange left, ByteRange right, int rightInnerOffset) { 038 int maxCompares = Math.min(left.getLength(), right.getLength() - rightInnerOffset); 039 final byte[] lbytes = left.getBytes(); 040 final byte[] rbytes = right.getBytes(); 041 final int loffset = left.getOffset(); 042 final int roffset = right.getOffset(); 043 for (int i = 0; i < maxCompares; ++i) { 044 if (lbytes[loffset + i] != rbytes[roffset + rightInnerOffset + i]) { 045 return i; 046 } 047 } 048 return maxCompares; 049 } 050 051 public static ArrayList<byte[]> copyToNewArrays(Collection<ByteRange> ranges) { 052 if (ranges == null) { 053 return new ArrayList<>(0); 054 } 055 ArrayList<byte[]> arrays = Lists.newArrayListWithCapacity(ranges.size()); 056 for (ByteRange range : ranges) { 057 arrays.add(range.deepCopyToNewArray()); 058 } 059 return arrays; 060 } 061 062 public static ArrayList<ByteRange> fromArrays(Collection<byte[]> arrays) { 063 if (arrays == null) { 064 return new ArrayList<>(0); 065 } 066 ArrayList<ByteRange> ranges = Lists.newArrayListWithCapacity(arrays.size()); 067 for (byte[] array : arrays) { 068 ranges.add(new SimpleMutableByteRange(array)); 069 } 070 return ranges; 071 } 072 073 public static void write(OutputStream os, ByteRange byteRange) throws IOException { 074 os.write(byteRange.getBytes(), byteRange.getOffset(), byteRange.getLength()); 075 } 076 077 public static void write(OutputStream os, ByteRange byteRange, int byteRangeInnerOffset) 078 throws IOException { 079 os.write(byteRange.getBytes(), byteRange.getOffset() + byteRangeInnerOffset, 080 byteRange.getLength() - byteRangeInnerOffset); 081 } 082}