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 com.google.common.collect.Lists;
27  
28  /**
29   * Utility methods {@link ByteRange}.
30   */
31  public class ByteRangeTool {
32  
33    public static ArrayList<byte[]> copyToNewArrays(Collection<ByteRange> ranges) {
34      if (ranges == null) {
35        return new ArrayList<byte[]>(0);
36      }
37      ArrayList<byte[]> arrays = Lists.newArrayListWithCapacity(ranges.size());
38      for (ByteRange range : ranges) {
39        arrays.add(range.deepCopyToNewArray());
40      }
41      return arrays;
42    }
43  
44    public static ArrayList<ByteRange> fromArrays(Collection<byte[]> arrays) {
45      if (arrays == null) {
46        return new ArrayList<ByteRange>(0);
47      }
48      ArrayList<ByteRange> ranges = Lists.newArrayListWithCapacity(arrays.size());
49      for (byte[] array : arrays) {
50        ranges.add(new ByteRange(array));
51      }
52      return ranges;
53    }
54  
55    public static void write(OutputStream os, ByteRange byteRange) throws IOException {
56      os.write(byteRange.getBytes(), byteRange.getOffset(), byteRange.getLength());
57    }
58  
59    public static void write(OutputStream os, ByteRange byteRange, int byteRangeInnerOffset)
60        throws IOException {
61      os.write(byteRange.getBytes(), byteRange.getOffset() + byteRangeInnerOffset,
62        byteRange.getLength() - byteRangeInnerOffset);
63    }
64  
65  }