diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/VectoredReadUtils.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/VectoredReadUtils.java index fa0440620a..2f99edc910 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/VectoredReadUtils.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/VectoredReadUtils.java @@ -308,7 +308,7 @@ public static List extends FileRange> validateAndSortRanges( validateRangeRequest(input.get(0)); sortedRanges = input; } else { - sortedRanges = sortRanges(input); + sortedRanges = sortRangeList(input); FileRange prev = null; for (final FileRange current : sortedRanges) { validateRangeRequest(current); @@ -341,12 +341,25 @@ public static List extends FileRange> validateAndSortRanges( * @param input input ranges. * @return a new list of the ranges, sorted by offset. */ - public static List extends FileRange> sortRanges(List extends FileRange> input) { + public static List extends FileRange> sortRangeList(List extends FileRange> input) { final List extends FileRange> l = new ArrayList<>(input); l.sort(Comparator.comparingLong(FileRange::getOffset)); return l; } + /** + * Sort the input ranges by offset; no validation is done. + *
+ * This method is used externally and must be retained with
+ * the signature unchanged.
+ * @param input input ranges.
+ * @return a new list of the ranges, sorted by offset.
+ */
+ @InterfaceStability.Stable
+ public static FileRange[] sortRanges(List extends FileRange> input) {
+ return sortRangeList(input).toArray(new FileRange[0]);
+ }
+
/**
* Merge sorted ranges to optimize the access from the underlying file
* system.
diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/impl/TestVectoredReadUtils.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/impl/TestVectoredReadUtils.java
index 3fd3fe4d1f..b08fc95279 100644
--- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/impl/TestVectoredReadUtils.java
+++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/impl/TestVectoredReadUtils.java
@@ -23,6 +23,7 @@
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
@@ -47,6 +48,7 @@
import static org.apache.hadoop.fs.VectoredReadUtils.mergeSortedRanges;
import static org.apache.hadoop.fs.VectoredReadUtils.readRangeFrom;
import static org.apache.hadoop.fs.VectoredReadUtils.readVectored;
+import static org.apache.hadoop.fs.VectoredReadUtils.sortRangeList;
import static org.apache.hadoop.fs.VectoredReadUtils.sortRanges;
import static org.apache.hadoop.fs.VectoredReadUtils.validateAndSortRanges;
import static org.apache.hadoop.test.LambdaTestUtils.intercept;
@@ -196,7 +198,7 @@ public void testSortAndMerge() {
);
assertIsNotOrderedDisjoint(input, 100, 800);
final List