Apache Lucene open-source search software
LUCENE-8165: Ban copyOf and copyOfRange.
Nhat Nguyen8a6f1bf5ada4
diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.javaindex 7885daab8d8..f42aac3e474 100644--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/hunspell/Dictionary.java@@ -62,7 +62,6 @@ import org.apache.lucene.util.IntsRefBuilder;import org.apache.lucene.util.OfflineSorter.ByteSequencesReader;import org.apache.lucene.util.OfflineSorter.ByteSequencesWriter;import org.apache.lucene.util.OfflineSorter;-import org.apache.lucene.util.RamUsageEstimator;import org.apache.lucene.util.automaton.CharacterRunAutomaton;import org.apache.lucene.util.automaton.RegExp;import org.apache.lucene.util.fst.Builder;@@ -927,10 +926,7 @@ public class Dictionary {if (hasStemExceptions && end+1 < line.length()) {String stemException = parseStemException(line.substring(end+1));if (stemException != null) {- if (stemExceptionCount == stemExceptions.length) {- int newSize = ArrayUtil.oversize(stemExceptionCount+1, RamUsageEstimator.NUM_BYTES_OBJECT_REF);- stemExceptions = Arrays.copyOf(stemExceptions, newSize);- }+ stemExceptions = ArrayUtil.grow(stemExceptions, stemExceptionCount+1);stemExceptionID = stemExceptionCount+1; // we use '0' to indicate no exception for the formstemExceptions[stemExceptionCount++] = stemException;}@@ -1125,7 +1121,7 @@ public class Dictionary {}if (upto < flags.length) {- flags = Arrays.copyOf(flags, upto);+ flags = ArrayUtil.copyOfSubArray(flags, 0, upto);}return flags;}diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/WordDelimiterFilter.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/WordDelimiterFilter.javaindex 16edb3dbef7..4124e846c46 100644--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/WordDelimiterFilter.java+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/miscellaneous/WordDelimiterFilter.java@@ -17,7 +17,6 @@package org.apache.lucene.analysis.miscellaneous;import java.io.IOException;-import java.util.Arrays;import org.apache.lucene.analysis.CharArraySet;import org.apache.lucene.analysis.TokenFilter;@@ -427,9 +426,9 @@ public final class WordDelimiterFilter extends TokenFilter {private void buffer() {if (bufferedLen == buffered.length) {int newSize = ArrayUtil.oversize(bufferedLen+1, 8);- buffered = Arrays.copyOf(buffered, newSize);- startOff = Arrays.copyOf(startOff, newSize);- posInc = Arrays.copyOf(posInc, newSize);+ buffered = ArrayUtil.growExact(buffered, newSize);+ startOff = ArrayUtil.growExact(startOff, newSize);+ posInc = ArrayUtil.growExact(posInc, newSize);}startOff[bufferedLen] = offsetAttribute.startOffset();posInc[bufferedLen] = posIncAttribute.getPositionIncrement();diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymFilter.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymFilter.javaindex ec2676f7804..a51edb5dba0 100644--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymFilter.java+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/SynonymFilter.java@@ -17,7 +17,6 @@package org.apache.lucene.analysis.synonym;import java.io.IOException;-import java.util.Arrays;import org.apache.lucene.analysis.TokenFilter;import org.apache.lucene.analysis.TokenStream;@@ -33,7 +32,6 @@ import org.apache.lucene.util.AttributeSource;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.CharsRef;import org.apache.lucene.util.CharsRefBuilder;-import org.apache.lucene.util.RamUsageEstimator;import org.apache.lucene.util.fst.FST;/**@@ -206,7 +204,7 @@ public final class SynonymFilter extends TokenFilter {public void add(char[] output, int offset, int len, int endOffset, int posLength) {if (count == outputs.length) {- outputs = Arrays.copyOf(outputs, ArrayUtil.oversize(1+count, RamUsageEstimator.NUM_BYTES_OBJECT_REF));+ outputs = ArrayUtil.grow(outputs, count+1);}if (count == endOffsets.length) {final int[] next = new int[ArrayUtil.oversize(1+count, Integer.BYTES)];diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/WordnetSynonymParser.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/WordnetSynonymParser.javaindex b74e37185d3..a4183d711b3 100644--- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/WordnetSynonymParser.java+++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/WordnetSynonymParser.java@@ -21,9 +21,9 @@ import java.io.IOException;import java.io.LineNumberReader;import java.io.Reader;import java.text.ParseException;-import java.util.Arrays;import org.apache.lucene.analysis.Analyzer;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.CharsRef;import org.apache.lucene.util.CharsRefBuilder;@@ -59,10 +59,7 @@ public class WordnetSynonymParser extends SynonymMap.Parser {synsetSize = 0;}- if (synset.length <= synsetSize+1) {- synset = Arrays.copyOf(synset, synset.length * 2);- }-+ synset = ArrayUtil.grow(synset, synsetSize + 1);synset[synsetSize] = parseSynonym(line, new CharsRefBuilder());synsetSize++;lastSynSetID = synSetID;diff --git a/lucene/analysis/common/src/test/org/apache/lucene/analysis/ngram/NGramTokenizerTest.java b/lucene/analysis/common/src/test/org/apache/lucene/analysis/ngram/NGramTokenizerTest.javaindex 46a0c1c143e..cb54fa2fc44 100644--- a/lucene/analysis/common/src/test/org/apache/lucene/analysis/ngram/NGramTokenizerTest.java+++ b/lucene/analysis/common/src/test/org/apache/lucene/analysis/ngram/NGramTokenizerTest.java@@ -29,6 +29,7 @@ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;import org.apache.lucene.analysis.tokenattributes.OffsetAttribute;import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute;import org.apache.lucene.analysis.tokenattributes.PositionLengthAttribute;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.TestUtil;import com.carrotsearch.randomizedtesting.generators.RandomStrings;@@ -179,7 +180,7 @@ public class NGramTokenizerTest extends BaseTokenStreamTestCase {}}assertTrue(grams.incrementToken());- assertArrayEquals(Arrays.copyOfRange(codePoints, start, end), toCodePoints(termAtt));+ assertArrayEquals(ArrayUtil.copyOfSubArray(codePoints, start, end), toCodePoints(termAtt));assertEquals(1, posIncAtt.getPositionIncrement());assertEquals(1, posLenAtt.getPositionLength());assertEquals(offsets[start], offsetAtt.startOffset());diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.javaindex 9ed87b57a90..9de9d732a25 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/blockterms/BlockTermsWriter.java@@ -20,7 +20,6 @@ package org.apache.lucene.codecs.blockterms;import java.io.Closeable;import java.io.IOException;import java.util.ArrayList;-import java.util.Arrays;import java.util.List;import org.apache.lucene.codecs.BlockTermState;@@ -44,7 +43,6 @@ import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.BytesRefBuilder;import org.apache.lucene.util.FixedBitSet;import org.apache.lucene.util.IOUtils;-import org.apache.lucene.util.RamUsageEstimator;// TODO: currently we encode all terms between two indexed// terms as a block; but, we could decouple the two, ie@@ -260,11 +258,9 @@ public class BlockTermsWriter extends FieldsConsumer implements Closeable {//System.out.println(" index term!");}- if (pendingTerms.length == pendingCount) {- pendingTerms = Arrays.copyOf(pendingTerms, ArrayUtil.oversize(pendingCount+1, RamUsageEstimator.NUM_BYTES_OBJECT_REF));- for(int i=pendingCount;i<pendingTerms.length;i++) {- pendingTerms[i] = new TermEntry();- }+ pendingTerms = ArrayUtil.grow(pendingTerms, pendingCount + 1);+ for (int i = pendingCount; i < pendingTerms.length; i++) {+ pendingTerms[i] = new TermEntry();}final TermEntry te = pendingTerms[pendingCount];te.term.copyBytes(text);diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDWriter.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDWriter.javaindex 034202b10d8..cd74acaf1e8 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDWriter.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextBKDWriter.java@@ -505,7 +505,7 @@ final class SimpleTextBKDWriter implements Closeable {if (leafBlockFPs.size() > 0) {// Save the first (minimum) value in each leaf block except the first, to build the split value index in the end:- leafBlockStartValues.add(Arrays.copyOf(leafValues, packedBytesLength));+ leafBlockStartValues.add(ArrayUtil.copyOfSubArray(leafValues, 0, packedBytesLength));}leafBlockFPs.add(out.getFilePointer());checkMaxLeafNodeCount(leafBlockFPs.size());@@ -539,8 +539,8 @@ final class SimpleTextBKDWriter implements Closeable {return scratch;}};- assert valuesInOrderAndBounds(leafCount, 0, Arrays.copyOf(leafValues, packedBytesLength),- Arrays.copyOfRange(leafValues, (leafCount - 1) * packedBytesLength, leafCount * packedBytesLength),+ assert valuesInOrderAndBounds(leafCount, 0, ArrayUtil.copyOfSubArray(leafValues, 0, packedBytesLength),+ ArrayUtil.copyOfSubArray(leafValues, (leafCount - 1) * packedBytesLength, leafCount * packedBytesLength),packedValues, leafDocs, 0);writeLeafBlockPackedValues(out, commonPrefixLengths, leafCount, 0, packedValues);}@@ -1206,8 +1206,8 @@ final class SimpleTextBKDWriter implements Closeable {reader.getValue(mid, scratchBytesRef1);System.arraycopy(scratchBytesRef1.bytes, scratchBytesRef1.offset + splitDim * bytesPerDim, splitPackedValues, address + 1, bytesPerDim);- byte[] minSplitPackedValue = Arrays.copyOf(minPackedValue, packedBytesLength);- byte[] maxSplitPackedValue = Arrays.copyOf(maxPackedValue, packedBytesLength);+ byte[] minSplitPackedValue = ArrayUtil.copyOfSubArray(minPackedValue, 0, packedBytesLength);+ byte[] maxSplitPackedValue = ArrayUtil.copyOfSubArray(maxPackedValue, 0, packedBytesLength);System.arraycopy(scratchBytesRef1.bytes, scratchBytesRef1.offset + splitDim * bytesPerDim,minSplitPackedValue, splitDim * bytesPerDim, bytesPerDim);System.arraycopy(scratchBytesRef1.bytes, scratchBytesRef1.offset + splitDim * bytesPerDim,diff --git a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSegmentInfoFormat.java b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSegmentInfoFormat.javaindex 8a71c6df7dc..6d5bfe4c3b0 100644--- a/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSegmentInfoFormat.java+++ b/lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextSegmentInfoFormat.java@@ -42,6 +42,7 @@ import org.apache.lucene.store.Directory;import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.BytesRefBuilder;import org.apache.lucene.util.StringHelper;@@ -158,7 +159,7 @@ public class SimpleTextSegmentInfoFormat extends SegmentInfoFormat {SimpleTextUtil.readLine(input, scratch);assert StringHelper.startsWith(scratch.get(), SI_ID);- final byte[] id = Arrays.copyOfRange(scratch.bytes(), SI_ID.length, scratch.length());+ final byte[] id = ArrayUtil.copyOfSubArray(scratch.bytes(), SI_ID.length, scratch.length());if (!Arrays.equals(segmentID, id)) {throw new CorruptIndexException("file mismatch, expected: " + StringHelper.idToString(segmentID)diff --git a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexReader.java b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexReader.javaindex 0685d799497..61410b68e5f 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexReader.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsIndexReader.java@@ -21,7 +21,6 @@ import static org.apache.lucene.util.BitUtil.zigZagDecode;import java.io.IOException;import java.util.ArrayList;-import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.List;@@ -73,12 +72,12 @@ public final class CompressingStoredFieldsIndexReader implements Cloneable, Acco}if (blockCount == docBases.length) {final int newSize = ArrayUtil.oversize(blockCount + 1, 8);- docBases = Arrays.copyOf(docBases, newSize);- startPointers = Arrays.copyOf(startPointers, newSize);- avgChunkDocs = Arrays.copyOf(avgChunkDocs, newSize);- avgChunkSizes = Arrays.copyOf(avgChunkSizes, newSize);- docBasesDeltas = Arrays.copyOf(docBasesDeltas, newSize);- startPointersDeltas = Arrays.copyOf(startPointersDeltas, newSize);+ docBases = ArrayUtil.growExact(docBases, newSize);+ startPointers = ArrayUtil.growExact(startPointers, newSize);+ avgChunkDocs = ArrayUtil.growExact(avgChunkDocs, newSize);+ avgChunkSizes = ArrayUtil.growExact(avgChunkSizes, newSize);+ docBasesDeltas = ArrayUtil.growExact(docBasesDeltas, newSize);+ startPointersDeltas = ArrayUtil.growExact(startPointersDeltas, newSize);}// doc bases@@ -102,12 +101,12 @@ public final class CompressingStoredFieldsIndexReader implements Cloneable, Acco++blockCount;}- this.docBases = Arrays.copyOf(docBases, blockCount);- this.startPointers = Arrays.copyOf(startPointers, blockCount);- this.avgChunkDocs = Arrays.copyOf(avgChunkDocs, blockCount);- this.avgChunkSizes = Arrays.copyOf(avgChunkSizes, blockCount);- this.docBasesDeltas = Arrays.copyOf(docBasesDeltas, blockCount);- this.startPointersDeltas = Arrays.copyOf(startPointersDeltas, blockCount);+ this.docBases = ArrayUtil.copyOfSubArray(docBases, 0, blockCount);+ this.startPointers = ArrayUtil.copyOfSubArray(startPointers, 0, blockCount);+ this.avgChunkDocs = ArrayUtil.copyOfSubArray(avgChunkDocs, 0, blockCount);+ this.avgChunkSizes = ArrayUtil.copyOfSubArray(avgChunkSizes, 0, blockCount);+ this.docBasesDeltas = ArrayUtil.copyOfSubArray(docBasesDeltas, 0, blockCount);+ this.startPointersDeltas = ArrayUtil.copyOfSubArray(startPointersDeltas, 0, blockCount);}private int block(int docID) {diff --git a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsWriter.javaindex 8cd8ccbc279..5b8eb9e1249 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsWriter.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingStoredFieldsWriter.java@@ -19,7 +19,6 @@ package org.apache.lucene.codecs.compressing;import java.io.IOException;import java.util.ArrayList;-import java.util.Arrays;import java.util.List;import org.apache.lucene.codecs.CodecUtil;@@ -158,8 +157,8 @@ public final class CompressingStoredFieldsWriter extends StoredFieldsWriter {public void finishDocument() throws IOException {if (numBufferedDocs == this.numStoredFields.length) {final int newLength = ArrayUtil.oversize(numBufferedDocs + 1, 4);- this.numStoredFields = Arrays.copyOf(this.numStoredFields, newLength);- endOffsets = Arrays.copyOf(endOffsets, newLength);+ this.numStoredFields = ArrayUtil.growExact(this.numStoredFields, newLength);+ endOffsets = ArrayUtil.growExact(endOffsets, newLength);}this.numStoredFields[numBufferedDocs] = numStoredFieldsInDoc;numStoredFieldsInDoc = 0;diff --git a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingTermVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingTermVectorsWriter.javaindex ee948c3e3e7..4f8d004e200 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingTermVectorsWriter.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/compressing/CompressingTermVectorsWriter.java@@ -176,8 +176,8 @@ public final class CompressingTermVectorsWriter extends TermVectorsWriter {if (hasOffsets) {if (offStart + totalPositions == startOffsetsBuf.length) {final int newLength = ArrayUtil.oversize(offStart + totalPositions, 4);- startOffsetsBuf = Arrays.copyOf(startOffsetsBuf, newLength);- lengthsBuf = Arrays.copyOf(lengthsBuf, newLength);+ startOffsetsBuf = ArrayUtil.growExact(startOffsetsBuf, newLength);+ lengthsBuf = ArrayUtil.growExact(lengthsBuf, newLength);}startOffsetsBuf[offStart + totalPositions] = startOffset;lengthsBuf[offStart + totalPositions] = length;@@ -705,8 +705,8 @@ public final class CompressingTermVectorsWriter extends TermVectorsWriter {final int offStart = curField.offStart + curField.totalPositions;if (offStart + numProx > startOffsetsBuf.length) {final int newLength = ArrayUtil.oversize(offStart + numProx, 4);- startOffsetsBuf = Arrays.copyOf(startOffsetsBuf, newLength);- lengthsBuf = Arrays.copyOf(lengthsBuf, newLength);+ startOffsetsBuf = ArrayUtil.growExact(startOffsetsBuf, newLength);+ lengthsBuf = ArrayUtil.growExact(lengthsBuf, newLength);}int lastOffset = 0, startOffset, endOffset;for (int i = 0; i < numProx; ++i) {diff --git a/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.java b/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.javaindex 700090a48fd..71a14a5cb7c 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortedSetDocValuesWriter.java@@ -164,7 +164,7 @@ class SortedSetDocValuesWriter extends DocValuesWriter {}docOrds[upto++] = ord;}- ords[newDocID] = Arrays.copyOfRange(docOrds, 0, upto);+ ords[newDocID] = ArrayUtil.copyOfSubArray(docOrds, 0, upto);}return ords;}diff --git a/lucene/core/src/java/org/apache/lucene/index/SortingLeafReader.java b/lucene/core/src/java/org/apache/lucene/index/SortingLeafReader.javaindex eb9f7ed8a52..55e4d200164 100644--- a/lucene/core/src/java/org/apache/lucene/index/SortingLeafReader.java+++ b/lucene/core/src/java/org/apache/lucene/index/SortingLeafReader.java@@ -890,8 +890,8 @@ class SortingLeafReader extends FilterLeafReader {while ((doc = in.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {if (i == docs.length) {final int newLength = ArrayUtil.oversize(i + 1, 4);- docs = Arrays.copyOf(docs, newLength);- offsets = Arrays.copyOf(offsets, newLength);+ docs = ArrayUtil.growExact(docs, newLength);+ offsets = ArrayUtil.growExact(offsets, newLength);}docs[i] = docMap.oldToNew(doc);offsets[i] = out.getFilePointer();@@ -1230,7 +1230,7 @@ class SortingLeafReader extends FilterLeafReader {}docOrds[upto++] = ord;}- ords[newDocID] = Arrays.copyOfRange(docOrds, 0, upto);+ ords[newDocID] = ArrayUtil.copyOfSubArray(docOrds, 0, upto);}cachedSortedSetDVs.put(field, ords);}diff --git a/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java b/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.javaindex cca667575a4..8f85e25b442 100644--- a/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java@@ -102,9 +102,9 @@ public final class BlendedTermQuery extends Query {/** Build the {@link BlendedTermQuery}. */public BlendedTermQuery build() {return new BlendedTermQuery(- Arrays.copyOf(terms, numTerms),- Arrays.copyOf(boosts, numTerms),- Arrays.copyOf(contexts, numTerms),+ ArrayUtil.copyOfSubArray(terms, 0, numTerms),+ ArrayUtil.copyOfSubArray(boosts, 0, numTerms),+ ArrayUtil.copyOfSubArray(contexts, 0, numTerms),rewriteMethod);}@@ -263,7 +263,7 @@ public final class BlendedTermQuery extends Query {@Overridepublic final Query rewrite(IndexReader reader) throws IOException {- final TermStates[] contexts = Arrays.copyOf(this.contexts, this.contexts.length);+ final TermStates[] contexts = ArrayUtil.copyOfSubArray(this.contexts, 0, this.contexts.length);for (int i = 0; i < contexts.length; ++i) {if (contexts[i] == null || contexts[i].wasBuiltFor(reader.getContext()) == false) {contexts[i] = TermStates.build(reader.getContext(), terms[i], true);diff --git a/lucene/core/src/java/org/apache/lucene/search/CachingCollector.java b/lucene/core/src/java/org/apache/lucene/search/CachingCollector.javaindex 3bed88dd998..6681c5997aa 100644--- a/lucene/core/src/java/org/apache/lucene/search/CachingCollector.java+++ b/lucene/core/src/java/org/apache/lucene/search/CachingCollector.java@@ -18,7 +18,6 @@ package org.apache.lucene.search;import java.io.IOException;import java.util.ArrayList;-import java.util.Arrays;import java.util.List;import org.apache.lucene.index.LeafReaderContext;@@ -213,7 +212,7 @@ public abstract class CachingCollector extends FilterCollector {}protected void grow(int newLen) {- docs = Arrays.copyOf(docs, newLen);+ docs = ArrayUtil.growExact(docs, newLen);}protected void invalidate() {@@ -250,7 +249,7 @@ public abstract class CachingCollector extends FilterCollector {}int[] cachedDocs() {- return docs == null ? null : Arrays.copyOf(docs, docCount);+ return docs == null ? null : ArrayUtil.copyOfSubArray(docs, 0, docCount);}}@@ -274,7 +273,7 @@ public abstract class CachingCollector extends FilterCollector {@Overrideprotected void grow(int newLen) {super.grow(newLen);- scores = Arrays.copyOf(scores, newLen);+ scores = ArrayUtil.growExact(scores, newLen);}@Override@@ -290,7 +289,7 @@ public abstract class CachingCollector extends FilterCollector {}float[] cachedScores() {- return docs == null ? null : Arrays.copyOf(scores, docCount);+ return docs == null ? null : ArrayUtil.copyOfSubArray(scores, 0, docCount);}}diff --git a/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java b/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.javaindex 17e4efc0c16..51fee1acf4e 100644--- a/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java+++ b/lucene/core/src/java/org/apache/lucene/search/MaxScoreCache.java@@ -53,7 +53,7 @@ final class MaxScoreCache {if (maxScoreCache.length < size) {int oldLength = maxScoreCache.length;maxScoreCache = ArrayUtil.grow(maxScoreCache, size);- maxScoreCacheUpTo = Arrays.copyOf(maxScoreCacheUpTo, maxScoreCache.length);+ maxScoreCacheUpTo = ArrayUtil.growExact(maxScoreCacheUpTo, maxScoreCache.length);Arrays.fill(maxScoreCacheUpTo, oldLength, maxScoreCacheUpTo.length, -1);}}diff --git a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.javaindex 16642e51f11..70d2e09ef3b 100644--- a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java@@ -413,7 +413,7 @@ public class PhraseQuery extends Query {}}if (termUpTo > 0) {- return similarity.scorer(boost, searcher.collectionStatistics(field), Arrays.copyOf(termStats, termUpTo));+ return similarity.scorer(boost, searcher.collectionStatistics(field), ArrayUtil.copyOfSubArray(termStats, 0, termUpTo));} else {return null; // no terms at all, we won't use similarity}diff --git a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.javaindex 689d64a50d7..149fa2080ae 100644--- a/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/PointInSetQuery.java@@ -333,7 +333,7 @@ public abstract class PointInSetQuery extends Query {upto++;BytesRef next = iterator.next();- return Arrays.copyOfRange(next.bytes, next.offset, next.length);+ return BytesRef.deepCopyOf(next).bytes;}};}diff --git a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.javaindex 7e48383b472..3af001b4443 100644--- a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java@@ -26,6 +26,7 @@ import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.index.PointValues;import org.apache.lucene.index.PointValues.IntersectVisitor;import org.apache.lucene.index.PointValues.Relation;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.BitSetIterator;import org.apache.lucene.util.DocIdSetBuilder;import org.apache.lucene.util.FixedBitSet;@@ -392,9 +393,9 @@ public abstract class PointRangeQuery extends Query {int startOffset = bytesPerDim * i;sb.append('[');- sb.append(toString(i, Arrays.copyOfRange(lowerPoint, startOffset, startOffset + bytesPerDim)));+ sb.append(toString(i, ArrayUtil.copyOfSubArray(lowerPoint, startOffset, startOffset + bytesPerDim)));sb.append(" TO ");- sb.append(toString(i, Arrays.copyOfRange(upperPoint, startOffset, startOffset + bytesPerDim)));+ sb.append(toString(i, ArrayUtil.copyOfSubArray(upperPoint, startOffset, startOffset + bytesPerDim)));sb.append(']');}diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanWeight.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanWeight.javaindex 35c36d77c73..0313d56510c 100644--- a/lucene/core/src/java/org/apache/lucene/search/spans/SpanWeight.java+++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanWeight.java@@ -18,7 +18,6 @@ package org.apache.lucene.search.spans;import java.io.IOException;-import java.util.Arrays;import java.util.Map;import org.apache.lucene.index.LeafReaderContext;@@ -32,6 +31,7 @@ import org.apache.lucene.search.LeafSimScorer;import org.apache.lucene.search.TermStatistics;import org.apache.lucene.search.Weight;import org.apache.lucene.search.similarities.Similarity;+import org.apache.lucene.util.ArrayUtil;/*** Expert-only. Public for use by other weight implementations@@ -103,7 +103,7 @@ public abstract class SpanWeight extends Weight {}CollectionStatistics collectionStats = searcher.collectionStatistics(query.getField());if (termUpTo > 0) {- return similarity.scorer(boost, collectionStats, Arrays.copyOf(termStats, termUpTo));+ return similarity.scorer(boost, collectionStats, ArrayUtil.copyOfSubArray(termStats, 0, termUpTo));} else {return null; // no terms at all exist, we won't use similarity}diff --git a/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java b/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.javaindex 3c5897f6c85..f6bab105911 100644--- a/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java+++ b/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java@@ -16,7 +16,7 @@*/package org.apache.lucene.util;-import java.util.Arrays;+import java.lang.reflect.Array;import java.util.Comparator;/**@@ -211,94 +211,172 @@ public final class ArrayUtil {}}+ /** Returns a new array whose size is exact the specified {@code newLength} without over-allocating */+ public static <T> T[] growExact(T[] array, int newLength) {+ Class<? extends Object[]> type = array.getClass();+ @SuppressWarnings("unchecked")+ T[] copy = (type == Object[].class)+ ? (T[]) new Object[newLength]+ : (T[]) Array.newInstance(type.getComponentType(), newLength);+ System.arraycopy(array, 0, copy, 0, array.length);+ return copy;+ }++ /** Returns an array whose size is at least {@code minSize}, generally over-allocating exponentially */public static <T> T[] grow(T[] array, int minSize) {- assert minSize >= 0: "size must be positive (got " + minSize + "): likely integer overflow?";+ assert minSize >= 0 : "size must be positive (got " + minSize + "): likely integer overflow?";if (array.length < minSize) {- return Arrays.copyOf(array, oversize(minSize, RamUsageEstimator.NUM_BYTES_OBJECT_REF));+ final int newLength = oversize(minSize, RamUsageEstimator.NUM_BYTES_OBJECT_REF);+ return growExact(array, newLength);} elsereturn array;}+ /** Returns a new array whose size is exact the specified {@code newLength} without over-allocating */+ public static short[] growExact(short[] array, int newLength) {+ short[] copy = new short[newLength];+ System.arraycopy(array, 0, copy, 0, array.length);+ return copy;+ }++ /** Returns an array whose size is at least {@code minSize}, generally over-allocating exponentially */public static short[] grow(short[] array, int minSize) {assert minSize >= 0: "size must be positive (got " + minSize + "): likely integer overflow?";if (array.length < minSize) {- return Arrays.copyOf(array, oversize(minSize, Short.BYTES));+ return growExact(array, oversize(minSize, Short.BYTES));} elsereturn array;}+ /** Returns a larger array, generally over-allocating exponentially */public static short[] grow(short[] array) {return grow(array, 1 + array.length);}-++ /** Returns a new array whose size is exact the specified {@code newLength} without over-allocating */+ public static float[] growExact(float[] array, int newLength) {+ float[] copy = new float[newLength];+ System.arraycopy(array, 0, copy, 0, array.length);+ return copy;+ }++ /** Returns an array whose size is at least {@code minSize}, generally over-allocating exponentially */public static float[] grow(float[] array, int minSize) {assert minSize >= 0: "size must be positive (got " + minSize + "): likely integer overflow?";if (array.length < minSize) {- return Arrays.copyOf(array, oversize(minSize, Float.BYTES));+ float[] copy = new float[oversize(minSize, Float.BYTES)];+ System.arraycopy(array, 0, copy, 0, array.length);+ return copy;} elsereturn array;}+ /** Returns a larger array, generally over-allocating exponentially */public static float[] grow(float[] array) {return grow(array, 1 + array.length);}+ /** Returns a new array whose size is exact the specified {@code newLength} without over-allocating */+ public static double[] growExact(double[] array, int newLength) {+ double[] copy = new double[newLength];+ System.arraycopy(array, 0, copy, 0, array.length);+ return copy;+ }++ /** Returns an array whose size is at least {@code minSize}, generally over-allocating exponentially */public static double[] grow(double[] array, int minSize) {assert minSize >= 0: "size must be positive (got " + minSize + "): likely integer overflow?";if (array.length < minSize) {- return Arrays.copyOf(array, oversize(minSize, Double.BYTES));+ return growExact(array, oversize(minSize, Double.BYTES));} elsereturn array;}+ /** Returns a larger array, generally over-allocating exponentially */public static double[] grow(double[] array) {return grow(array, 1 + array.length);}+ /** Returns a new array whose size is exact the specified {@code newLength} without over-allocating */+ public static int[] growExact(int[] array, int newLength) {+ int[] copy = new int[newLength];+ System.arraycopy(array, 0, copy, 0, array.length);+ return copy;+ }++ /** Returns an array whose size is at least {@code minSize}, generally over-allocating exponentially */public static int[] grow(int[] array, int minSize) {assert minSize >= 0: "size must be positive (got " + minSize + "): likely integer overflow?";if (array.length < minSize) {- return Arrays.copyOf(array, oversize(minSize, Integer.BYTES));+ return growExact(array, oversize(minSize, Integer.BYTES));} elsereturn array;}+ /** Returns a larger array, generally over-allocating exponentially */public static int[] grow(int[] array) {return grow(array, 1 + array.length);}+ /** Returns a new array whose size is exact the specified {@code newLength} without over-allocating */+ public static long[] growExact(long[] array, int newLength) {+ long[] copy = new long[newLength];+ System.arraycopy(array, 0, copy, 0, array.length);+ return copy;+ }++ /** Returns an array whose size is at least {@code minSize}, generally over-allocating exponentially */public static long[] grow(long[] array, int minSize) {assert minSize >= 0: "size must be positive (got " + minSize + "): likely integer overflow?";if (array.length < minSize) {- return Arrays.copyOf(array, oversize(minSize, Long.BYTES));+ return growExact(array, oversize(minSize, Long.BYTES));} elsereturn array;}+ /** Returns a larger array, generally over-allocating exponentially */public static long[] grow(long[] array) {return grow(array, 1 + array.length);}+ /** Returns a new array whose size is exact the specified {@code newLength} without over-allocating */+ public static byte[] growExact(byte[] array, int newLength) {+ byte[] copy = new byte[newLength];+ System.arraycopy(array, 0, copy, 0, array.length);+ return copy;+ }++ /** Returns an array whose size is at least {@code minSize}, generally over-allocating exponentially */public static byte[] grow(byte[] array, int minSize) {assert minSize >= 0: "size must be positive (got " + minSize + "): likely integer overflow?";if (array.length < minSize) {- return Arrays.copyOf(array, oversize(minSize, Byte.BYTES));+ return growExact(array, oversize(minSize, Byte.BYTES));} elsereturn array;}+ /** Returns a larger array, generally over-allocating exponentially */public static byte[] grow(byte[] array) {return grow(array, 1 + array.length);}+ /** Returns a new array whose size is exact the specified {@code newLength} without over-allocating */+ public static char[] growExact(char[] array, int newLength) {+ char[] copy = new char[newLength];+ System.arraycopy(array, 0, copy, 0, array.length);+ return copy;+ }++ /** Returns an array whose size is at least {@code minSize}, generally over-allocating exponentially */public static char[] grow(char[] array, int minSize) {assert minSize >= 0: "size must be positive (got " + minSize + "): likely integer overflow?";if (array.length < minSize) {- return Arrays.copyOf(array, oversize(minSize, Character.BYTES));+ return growExact(array, oversize(minSize, Character.BYTES));} elsereturn array;}+ /** Returns a larger array, generally over-allocating exponentially */public static char[] grow(char[] array) {return grow(array, 1 + array.length);}@@ -429,4 +507,104 @@ public final class ArrayUtil {}.select(from, to, k);}+ /**+ * Copies the specified range of the given array into a new sub array.+ * @param array the input array+ * @param from the initial index of range to be copied (inclusive)+ * @param to the final index of range to be copied (exclusive)+ */+ public static byte[] copyOfSubArray(byte[] array, int from, int to) {+ final byte[] copy = new byte[to-from];+ System.arraycopy(array, from, copy, 0, to-from);+ return copy;+ }++ /**+ * Copies the specified range of the given array into a new sub array.+ * @param array the input array+ * @param from the initial index of range to be copied (inclusive)+ * @param to the final index of range to be copied (exclusive)+ */+ public static char[] copyOfSubArray(char[] array, int from, int to) {+ final char[] copy = new char[to-from];+ System.arraycopy(array, from, copy, 0, to-from);+ return copy;+ }++ /**+ * Copies the specified range of the given array into a new sub array.+ * @param array the input array+ * @param from the initial index of range to be copied (inclusive)+ * @param to the final index of range to be copied (exclusive)+ */+ public static short[] copyOfSubArray(short[] array, int from, int to) {+ final short[] copy = new short[to-from];+ System.arraycopy(array, from, copy, 0, to-from);+ return copy;+ }++ /**+ * Copies the specified range of the given array into a new sub array.+ * @param array the input array+ * @param from the initial index of range to be copied (inclusive)+ * @param to the final index of range to be copied (exclusive)+ */+ public static int[] copyOfSubArray(int[] array, int from, int to) {+ final int[] copy = new int[to-from];+ System.arraycopy(array, from, copy, 0, to-from);+ return copy;+ }++ /**+ * Copies the specified range of the given array into a new sub array.+ * @param array the input array+ * @param from the initial index of range to be copied (inclusive)+ * @param to the final index of range to be copied (exclusive)+ */+ public static long[] copyOfSubArray(long[] array, int from, int to) {+ final long[] copy = new long[to-from];+ System.arraycopy(array, from, copy, 0, to-from);+ return copy;+ }++ /**+ * Copies the specified range of the given array into a new sub array.+ * @param array the input array+ * @param from the initial index of range to be copied (inclusive)+ * @param to the final index of range to be copied (exclusive)+ */+ public static float[] copyOfSubArray(float[] array, int from, int to) {+ final float[] copy = new float[to-from];+ System.arraycopy(array, from, copy, 0, to-from);+ return copy;+ }++ /**+ * Copies the specified range of the given array into a new sub array.+ * @param array the input array+ * @param from the initial index of range to be copied (inclusive)+ * @param to the final index of range to be copied (exclusive)+ */+ public static double[] copyOfSubArray(double[] array, int from, int to) {+ final double[] copy = new double[to-from];+ System.arraycopy(array, from, copy, 0, to-from);+ return copy;+ }++ /**+ * Copies the specified range of the given array into a new sub array.+ * @param array the input array+ * @param from the initial index of range to be copied (inclusive)+ * @param to the final index of range to be copied (exclusive)+ */+ public static <T> T[] copyOfSubArray(T[] array, int from, int to) {+ final int subLength = to - from;+ final Class<? extends Object[]> type = array.getClass();+ @SuppressWarnings("unchecked")+ final T[] copy = (type == Object[].class)+ ? (T[]) new Object[subLength]+ : (T[]) Array.newInstance(type.getComponentType(), subLength);+ System.arraycopy(array, from, copy, 0, subLength);+ return copy;+ }}diff --git a/lucene/core/src/java/org/apache/lucene/util/BytesRef.java b/lucene/core/src/java/org/apache/lucene/util/BytesRef.javaindex 42c4e87ad58..a9a05e6ce92 100644--- a/lucene/core/src/java/org/apache/lucene/util/BytesRef.java+++ b/lucene/core/src/java/org/apache/lucene/util/BytesRef.java@@ -16,9 +16,6 @@*/package org.apache.lucene.util;--import java.util.Arrays;-/** Represents byte[], as a slice (offset + length) into an* existing byte[]. The {@link #bytes} member should never be null;* use {@link #EMPTY_BYTES} if necessary.@@ -172,11 +169,7 @@ public final class BytesRef implements Comparable<BytesRef>,Cloneable {* and an offset of zero.*/public static BytesRef deepCopyOf(BytesRef other) {- BytesRef copy = new BytesRef();- copy.bytes = Arrays.copyOfRange(other.bytes, other.offset, other.offset + other.length);- copy.offset = 0;- copy.length = other.length;- return copy;+ return new BytesRef(ArrayUtil.copyOfSubArray(other.bytes, other.offset, other.offset + other.length), 0, other.length);}/**diff --git a/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java b/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.javaindex 08fda910a55..6abd8660791 100644--- a/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java+++ b/lucene/core/src/java/org/apache/lucene/util/BytesRefBuilder.java@@ -17,8 +17,6 @@package org.apache.lucene.util;-import java.util.Arrays;-/*** A builder for {@link BytesRef} instances.* @lucene.internal@@ -170,7 +168,7 @@ public class BytesRefBuilder {* Build a new {@link BytesRef} that has the same content as this buffer.*/public BytesRef toBytesRef() {- return new BytesRef(Arrays.copyOf(ref.bytes, ref.length));+ return new BytesRef(ArrayUtil.copyOfSubArray(ref.bytes, 0, ref.length));}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/util/CharsRef.java b/lucene/core/src/java/org/apache/lucene/util/CharsRef.javaindex eb839a85fa0..7cb8ce900c9 100644--- a/lucene/core/src/java/org/apache/lucene/util/CharsRef.java+++ b/lucene/core/src/java/org/apache/lucene/util/CharsRef.java@@ -17,7 +17,6 @@package org.apache.lucene.util;-import java.util.Arrays;import java.util.Comparator;/**@@ -202,7 +201,7 @@ public final class CharsRef implements Comparable<CharsRef>, CharSequence, Clone* and an offset of zero.*/public static CharsRef deepCopyOf(CharsRef other) {- return new CharsRef(Arrays.copyOfRange(other.chars, other.offset, other.offset + other.length), 0, other.length);+ return new CharsRef(ArrayUtil.copyOfSubArray(other.chars, other.offset, other.offset + other.length), 0, other.length);}/**diff --git a/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java b/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.javaindex 09830e692cf..fdc16f4f0c7 100644--- a/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java+++ b/lucene/core/src/java/org/apache/lucene/util/CharsRefBuilder.java@@ -17,8 +17,6 @@package org.apache.lucene.util;-import java.util.Arrays;-/*** A builder for {@link CharsRef} instances.* @lucene.internal@@ -153,7 +151,7 @@ public class CharsRefBuilder implements Appendable {/** Build a new {@link CharsRef} that has the same content as this builder. */public CharsRef toCharsRef() {- return new CharsRef(Arrays.copyOf(ref.chars, ref.length), 0, ref.length);+ return new CharsRef(ArrayUtil.copyOfSubArray(ref.chars, 0, ref.length), 0, ref.length);}@Overridediff --git a/lucene/core/src/java/org/apache/lucene/util/DocIdSetBuilder.java b/lucene/core/src/java/org/apache/lucene/util/DocIdSetBuilder.javaindex 954614b3ad5..e57292b08c9 100644--- a/lucene/core/src/java/org/apache/lucene/util/DocIdSetBuilder.java+++ b/lucene/core/src/java/org/apache/lucene/util/DocIdSetBuilder.java@@ -18,7 +18,6 @@ package org.apache.lucene.util;import java.io.IOException;import java.util.ArrayList;-import java.util.Arrays;import java.util.List;import org.apache.lucene.index.PointValues;@@ -226,7 +225,7 @@ public final class DocIdSetBuilder {}private void growBuffer(Buffer buffer, int additionalCapacity) {- buffer.array = Arrays.copyOf(buffer.array, buffer.array.length + additionalCapacity);+ buffer.array = ArrayUtil.growExact(buffer.array, buffer.array.length + additionalCapacity);totalAllocated += additionalCapacity;}@@ -297,7 +296,7 @@ public final class DocIdSetBuilder {}int[] docs = largestBuffer.array;if (docs.length < totalLength + 1) {- docs = Arrays.copyOf(docs, totalLength + 1);+ docs = ArrayUtil.growExact(docs, totalLength + 1);}totalLength = largestBuffer.length;for (Buffer buffer : buffers) {diff --git a/lucene/core/src/java/org/apache/lucene/util/IntsRef.java b/lucene/core/src/java/org/apache/lucene/util/IntsRef.javaindex aa7bbcee11c..0c6cfa527c5 100644--- a/lucene/core/src/java/org/apache/lucene/util/IntsRef.java+++ b/lucene/core/src/java/org/apache/lucene/util/IntsRef.java@@ -16,8 +16,6 @@*/package org.apache.lucene.util;-import java.util.Arrays;-/** Represents int[], as a slice (offset + length) into an* existing int[]. The {@link #ints} member should never be null; use@@ -127,7 +125,7 @@ public final class IntsRef implements Comparable<IntsRef>, Cloneable {* and an offset of zero.*/public static IntsRef deepCopyOf(IntsRef other) {- return new IntsRef(Arrays.copyOfRange(other.ints, other.offset, other.offset + other.length), 0, other.length);+ return new IntsRef(ArrayUtil.copyOfSubArray(other.ints, other.offset, other.offset + other.length), 0, other.length);}/**diff --git a/lucene/core/src/java/org/apache/lucene/util/LongsRef.java b/lucene/core/src/java/org/apache/lucene/util/LongsRef.javaindex 952d189a2a3..e4ca3ef1206 100644--- a/lucene/core/src/java/org/apache/lucene/util/LongsRef.java+++ b/lucene/core/src/java/org/apache/lucene/util/LongsRef.java@@ -16,8 +16,6 @@*/package org.apache.lucene.util;-import java.util.Arrays;-/** Represents long[], as a slice (offset + length) into an* existing long[]. The {@link #longs} member should never be null; use@@ -126,7 +124,7 @@ public final class LongsRef implements Comparable<LongsRef>, Cloneable {* and an offset of zero.*/public static LongsRef deepCopyOf(LongsRef other) {- return new LongsRef(Arrays.copyOfRange(other.longs, other.offset, other.offset + other.length), 0, other.length);+ return new LongsRef(ArrayUtil.copyOfSubArray(other.longs, other.offset, other.offset + other.length), 0, other.length);}/**diff --git a/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java b/lucene/core/src/java/org/apache/lucene/util/PagedBytes.javaindex ad26f857e41..e07046c40ef 100644--- a/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java+++ b/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java@@ -18,7 +18,6 @@ package org.apache.lucene.util;import java.io.IOException;-import java.util.Arrays;import org.apache.lucene.store.DataInput;import org.apache.lucene.store.DataOutput;@@ -62,7 +61,7 @@ public final class PagedBytes implements Accountable {private final long bytesUsedPerBlock;private Reader(PagedBytes pagedBytes) {- blocks = Arrays.copyOf(pagedBytes.blocks, pagedBytes.numBlocks);+ blocks = ArrayUtil.copyOfSubArray(pagedBytes.blocks, 0, pagedBytes.numBlocks);blockBits = pagedBytes.blockBits;blockMask = pagedBytes.blockMask;blockSize = pagedBytes.blockSize;@@ -154,9 +153,7 @@ public final class PagedBytes implements Accountable {}private void addBlock(byte[] block) {- if (blocks.length == numBlocks) {- blocks = Arrays.copyOf(blocks, ArrayUtil.oversize(numBlocks, RamUsageEstimator.NUM_BYTES_OBJECT_REF));- }+ blocks = ArrayUtil.grow(blocks, numBlocks + 1);blocks[numBlocks++] = block;}diff --git a/lucene/core/src/java/org/apache/lucene/util/RoaringDocIdSet.java b/lucene/core/src/java/org/apache/lucene/util/RoaringDocIdSet.javaindex 9709c300058..5f704bbe04f 100644--- a/lucene/core/src/java/org/apache/lucene/util/RoaringDocIdSet.java+++ b/lucene/core/src/java/org/apache/lucene/util/RoaringDocIdSet.java@@ -18,7 +18,6 @@ package org.apache.lucene.util;import java.io.IOException;-import java.util.Arrays;import org.apache.lucene.search.DocIdSet;import org.apache.lucene.search.DocIdSetIterator;@@ -73,7 +72,7 @@ public class RoaringDocIdSet extends DocIdSet {// Use sparse encodingassert denseBuffer == null;if (currentBlockCardinality > 0) {- sets[currentBlock] = new ShortArrayDocIdSet(Arrays.copyOf(buffer, currentBlockCardinality));+ sets[currentBlock] = new ShortArrayDocIdSet(ArrayUtil.copyOfSubArray(buffer, 0, currentBlockCardinality));}} else {assert denseBuffer != null;diff --git a/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java b/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.javaindex 0324291fc71..4fcbbef3e98 100644--- a/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java+++ b/lucene/core/src/java/org/apache/lucene/util/SparseFixedBitSet.java@@ -18,7 +18,6 @@ package org.apache.lucene.util;import java.io.IOException;-import java.util.Arrays;import org.apache.lucene.search.DocIdSetIterator;@@ -372,7 +371,7 @@ public class SparseFixedBitSet extends BitSet implements Bits, Accountable {// fast path: if we currently have nothing in the block, just copy the data// this especially happens all the time if you call OR on an empty setindices[i4096] = index;- this.bits[i4096] = Arrays.copyOf(bits, nonZeroLongCount);+ this.bits[i4096] = ArrayUtil.copyOfSubArray(bits, 0, nonZeroLongCount);this.nonZeroLongCount += nonZeroLongCount;return;}diff --git a/lucene/core/src/java/org/apache/lucene/util/StringHelper.java b/lucene/core/src/java/org/apache/lucene/util/StringHelper.javaindex 4c6d4fac3a7..bdab07bf0f4 100644--- a/lucene/core/src/java/org/apache/lucene/util/StringHelper.java+++ b/lucene/core/src/java/org/apache/lucene/util/StringHelper.java@@ -307,7 +307,7 @@ public abstract class StringHelper {if (bits.length > ID_LENGTH) {assert bits.length == ID_LENGTH + 1;assert bits[0] == 0;- return Arrays.copyOfRange(bits, 1, bits.length);+ return ArrayUtil.copyOfSubArray(bits, 1, bits.length);} else {byte[] result = new byte[ID_LENGTH];System.arraycopy(bits, 0, result, result.length - bits.length, bits.length);diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/DaciukMihovAutomatonBuilder.java b/lucene/core/src/java/org/apache/lucene/util/automaton/DaciukMihovAutomatonBuilder.javaindex 704a6c41243..60ec8659346 100644--- a/lucene/core/src/java/org/apache/lucene/util/automaton/DaciukMihovAutomatonBuilder.java+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/DaciukMihovAutomatonBuilder.java@@ -137,8 +137,8 @@ public final class DaciukMihovAutomatonBuilder {assert Arrays.binarySearch(labels, label) < 0 : "State already has transition labeled: "+ label;- labels = Arrays.copyOf(labels, labels.length + 1);- states = Arrays.copyOf(states, states.length + 1);+ labels = ArrayUtil.growExact(labels, labels.length + 1);+ states = ArrayUtil.growExact(states, states.length + 1);labels[labels.length - 1] = label;return states[states.length - 1] = new State();diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.javaindex 7f55895842e..fb7e1cee7d8 100644--- a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java+++ b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java@@ -656,7 +656,7 @@ public class BKDWriter implements Closeable {if (leafBlockFPs.size() > 0) {// Save the first (minimum) value in each leaf block except the first, to build the split value index in the end:- leafBlockStartValues.add(Arrays.copyOf(leafValues, packedBytesLength));+ leafBlockStartValues.add(ArrayUtil.copyOfSubArray(leafValues, 0, packedBytesLength));}leafBlockFPs.add(out.getFilePointer());checkMaxLeafNodeCount(leafBlockFPs.size());@@ -687,8 +687,8 @@ public class BKDWriter implements Closeable {return scratchBytesRef1;}};- assert valuesInOrderAndBounds(leafCount, 0, Arrays.copyOf(leafValues, packedBytesLength),- Arrays.copyOfRange(leafValues, (leafCount - 1) * packedBytesLength, leafCount * packedBytesLength),+ assert valuesInOrderAndBounds(leafCount, 0, ArrayUtil.copyOfSubArray(leafValues, 0, packedBytesLength),+ ArrayUtil.copyOfSubArray(leafValues, (leafCount - 1) * packedBytesLength, leafCount * packedBytesLength),packedValues, leafDocs, 0);writeLeafBlockPackedValues(scratchOut, commonPrefixLengths, leafCount, 0, packedValues);out.writeBytes(scratchOut.getBytes(), 0, scratchOut.getPosition());@@ -1591,8 +1591,8 @@ public class BKDWriter implements Closeable {reader.getValue(mid, scratchBytesRef1);System.arraycopy(scratchBytesRef1.bytes, scratchBytesRef1.offset + splitDim * bytesPerDim, splitPackedValues, address + 1, bytesPerDim);- byte[] minSplitPackedValue = Arrays.copyOf(minPackedValue, packedBytesLength);- byte[] maxSplitPackedValue = Arrays.copyOf(maxPackedValue, packedBytesLength);+ byte[] minSplitPackedValue = ArrayUtil.copyOfSubArray(minPackedValue, 0, packedBytesLength);+ byte[] maxSplitPackedValue = ArrayUtil.copyOfSubArray(maxPackedValue, 0, packedBytesLength);System.arraycopy(scratchBytesRef1.bytes, scratchBytesRef1.offset + splitDim * bytesPerDim,minSplitPackedValue, splitDim * bytesPerDim, bytesPerDim);System.arraycopy(scratchBytesRef1.bytes, scratchBytesRef1.offset + splitDim * bytesPerDim,diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java b/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.javaindex e102651ceb2..eb1d48b9f12 100644--- a/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java+++ b/lucene/core/src/java/org/apache/lucene/util/bkd/HeapPointWriter.java@@ -18,7 +18,6 @@ package org.apache.lucene.util.bkd;import java.io.Closeable;import java.util.ArrayList;-import java.util.Arrays;import java.util.List;import org.apache.lucene.util.ArrayUtil;@@ -116,12 +115,12 @@ public final class HeapPointWriter implements PointWriter {if (docIDs.length == nextWrite) {int nextSize = Math.min(maxSize, ArrayUtil.oversize(nextWrite+1, Integer.BYTES));assert nextSize > nextWrite: "nextSize=" + nextSize + " vs nextWrite=" + nextWrite;- docIDs = Arrays.copyOf(docIDs, nextSize);+ docIDs = ArrayUtil.growExact(docIDs, nextSize);if (singleValuePerDoc == false) {if (ordsLong != null) {- ordsLong = Arrays.copyOf(ordsLong, nextSize);+ ordsLong = ArrayUtil.growExact(ordsLong, nextSize);} else {- ords = Arrays.copyOf(ords, nextSize);+ ords = ArrayUtil.growExact(ords, nextSize);}}}diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/DeltaPackedLongValues.java b/lucene/core/src/java/org/apache/lucene/util/packed/DeltaPackedLongValues.javaindex 80534da8902..6aabb079281 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/DeltaPackedLongValues.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/DeltaPackedLongValues.java@@ -17,8 +17,7 @@package org.apache.lucene.util.packed;-import java.util.Arrays;-+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.RamUsageEstimator;import org.apache.lucene.util.packed.PackedInts.Reader;@@ -70,8 +69,8 @@ class DeltaPackedLongValues extends PackedLongValues {public DeltaPackedLongValues build() {finish();pending = null;- final PackedInts.Reader[] values = Arrays.copyOf(this.values, valuesOff);- final long[] mins = Arrays.copyOf(this.mins, valuesOff);+ final PackedInts.Reader[] values = ArrayUtil.copyOfSubArray(this.values, 0, valuesOff);+ final long[] mins = ArrayUtil.copyOfSubArray(this.mins, 0, valuesOff);final long ramBytesUsed = DeltaPackedLongValues.BASE_RAM_BYTES_USED+ RamUsageEstimator.sizeOf(values) + RamUsageEstimator.sizeOf(mins);return new DeltaPackedLongValues(pageShift, pageMask, values, mins, size, ramBytesUsed);@@ -94,7 +93,7 @@ class DeltaPackedLongValues extends PackedLongValues {void grow(int newBlockCount) {super.grow(newBlockCount);ramBytesUsed -= RamUsageEstimator.sizeOf(mins);- mins = Arrays.copyOf(mins, newBlockCount);+ mins = ArrayUtil.growExact(mins, newBlockCount);ramBytesUsed += RamUsageEstimator.sizeOf(mins);}diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicLongValues.java b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicLongValues.javaindex 09b3ecd5cb2..89ad5ab3ca2 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicLongValues.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/MonotonicLongValues.java@@ -17,10 +17,9 @@package org.apache.lucene.util.packed;-import java.util.Arrays;-import static org.apache.lucene.util.packed.MonotonicBlockPackedReader.expected;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.RamUsageEstimator;import org.apache.lucene.util.packed.PackedInts.Reader;@@ -72,9 +71,9 @@ class MonotonicLongValues extends DeltaPackedLongValues {public MonotonicLongValues build() {finish();pending = null;- final PackedInts.Reader[] values = Arrays.copyOf(this.values, valuesOff);- final long[] mins = Arrays.copyOf(this.mins, valuesOff);- final float[] averages = Arrays.copyOf(this.averages, valuesOff);+ final PackedInts.Reader[] values = ArrayUtil.copyOfSubArray(this.values, 0, valuesOff);+ final long[] mins = ArrayUtil.copyOfSubArray(this.mins, 0, valuesOff);+ final float[] averages = ArrayUtil.copyOfSubArray(this.averages, 0, valuesOff);final long ramBytesUsed = MonotonicLongValues.BASE_RAM_BYTES_USED+ RamUsageEstimator.sizeOf(values) + RamUsageEstimator.sizeOf(mins)+ RamUsageEstimator.sizeOf(averages);@@ -95,7 +94,7 @@ class MonotonicLongValues extends DeltaPackedLongValues {void grow(int newBlockCount) {super.grow(newBlockCount);ramBytesUsed -= RamUsageEstimator.sizeOf(averages);- averages = Arrays.copyOf(averages, newBlockCount);+ averages = ArrayUtil.growExact(averages, newBlockCount);ramBytesUsed += RamUsageEstimator.sizeOf(averages);}diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.java b/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.javaindex 0daf0623f90..19788b76eed 100644--- a/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.java+++ b/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.java@@ -19,8 +19,6 @@ package org.apache.lucene.util.packed;import static org.apache.lucene.util.packed.PackedInts.checkBlockSize;-import java.util.Arrays;-import org.apache.lucene.util.Accountable;import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.LongValues;@@ -194,7 +192,7 @@ public class PackedLongValues extends LongValues implements Accountable {public PackedLongValues build() {finish();pending = null;- final PackedInts.Reader[] values = Arrays.copyOf(this.values, valuesOff);+ final PackedInts.Reader[] values = ArrayUtil.copyOfSubArray(this.values, 0, valuesOff);final long ramBytesUsed = PackedLongValues.BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(values);return new PackedLongValues(pageShift, pageMask, values, size, ramBytesUsed);}@@ -273,7 +271,7 @@ public class PackedLongValues extends LongValues implements Accountable {void grow(int newBlockCount) {ramBytesUsed -= RamUsageEstimator.shallowSizeOf(values);- values = Arrays.copyOf(values, newBlockCount);+ values = ArrayUtil.growExact(values, newBlockCount);ramBytesUsed += RamUsageEstimator.shallowSizeOf(values);}diff --git a/lucene/core/src/test/org/apache/lucene/analysis/TestCharacterUtils.java b/lucene/core/src/test/org/apache/lucene/analysis/TestCharacterUtils.javaindex 53b3f568e88..438e5e3cdc5 100644--- a/lucene/core/src/test/org/apache/lucene/analysis/TestCharacterUtils.java+++ b/lucene/core/src/test/org/apache/lucene/analysis/TestCharacterUtils.java@@ -20,9 +20,9 @@ package org.apache.lucene.analysis;import java.io.IOException;import java.io.Reader;import java.io.StringReader;-import java.util.Arrays;import org.apache.lucene.analysis.CharacterUtils.CharacterBuffer;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;import org.junit.Test;@@ -42,7 +42,7 @@ public class TestCharacterUtils extends LuceneTestCase {final int codePointCount = CharacterUtils.toCodePoints(orig, o1, orig.length - o1, buf, o2);final int charCount = CharacterUtils.toChars(buf, o2, codePointCount, restored, o3);assertEquals(orig.length - o1, charCount);- assertArrayEquals(Arrays.copyOfRange(orig, o1, o1 + charCount), Arrays.copyOfRange(restored, o3, o3 + charCount));+ assertArrayEquals(ArrayUtil.copyOfSubArray(orig, o1, o1 + charCount), ArrayUtil.copyOfSubArray(restored, o3, o3 + charCount));}@Testdiff --git a/lucene/core/src/test/org/apache/lucene/codecs/compressing/AbstractTestCompressionMode.java b/lucene/core/src/test/org/apache/lucene/codecs/compressing/AbstractTestCompressionMode.javaindex 045b19ad802..62d06d8f0f6 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/compressing/AbstractTestCompressionMode.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/compressing/AbstractTestCompressionMode.java@@ -22,6 +22,7 @@ import java.util.Arrays;import org.apache.lucene.store.ByteArrayDataInput;import org.apache.lucene.store.ByteArrayDataOutput;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.BytesRef;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;@@ -60,7 +61,7 @@ public abstract class AbstractTestCompressionMode extends LuceneTestCase {ByteArrayDataOutput out = new ByteArrayDataOutput(compressed);compressor.compress(decompressed, off, len, out);final int compressedLen = out.getPosition();- return Arrays.copyOf(compressed, compressedLen);+ return ArrayUtil.copyOfSubArray(compressed, 0, compressedLen);}byte[] decompress(byte[] compressed, int originalLength) throws IOException {@@ -71,14 +72,14 @@ public abstract class AbstractTestCompressionMode extends LuceneTestCase {static byte[] decompress(Decompressor decompressor, byte[] compressed, int originalLength) throws IOException {final BytesRef bytes = new BytesRef();decompressor.decompress(new ByteArrayDataInput(compressed), originalLength, 0, originalLength, bytes);- return Arrays.copyOfRange(bytes.bytes, bytes.offset, bytes.offset + bytes.length);+ return BytesRef.deepCopyOf(bytes).bytes;}byte[] decompress(byte[] compressed, int originalLength, int offset, int length) throws IOException {Decompressor decompressor = mode.newDecompressor();final BytesRef bytes = new BytesRef();decompressor.decompress(new ByteArrayDataInput(compressed), originalLength, offset, length, bytes);- return Arrays.copyOfRange(bytes.bytes, bytes.offset, bytes.offset + bytes.length);+ return BytesRef.deepCopyOf(bytes).bytes;}public void testDecompress() throws IOException {@@ -89,7 +90,7 @@ public abstract class AbstractTestCompressionMode extends LuceneTestCase {final int len = random().nextBoolean() ? decompressed.length - off : TestUtil.nextInt(random(), 0, decompressed.length - off);final byte[] compressed = compress(decompressed, off, len);final byte[] restored = decompress(compressed, len);- assertArrayEquals(Arrays.copyOfRange(decompressed, off, off+len), restored);+ assertArrayEquals(ArrayUtil.copyOfSubArray(decompressed, off, off+len), restored);}}@@ -106,7 +107,7 @@ public abstract class AbstractTestCompressionMode extends LuceneTestCase {length = random().nextInt(decompressed.length - offset);}final byte[] restored = decompress(compressed, decompressed.length, offset, length);- assertArrayEquals(Arrays.copyOfRange(decompressed, offset, offset + length), restored);+ assertArrayEquals(ArrayUtil.copyOfSubArray(decompressed, offset, offset + length), restored);}}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene50/TestForUtil.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene50/TestForUtil.javaindex 3fe003e1dce..e13645fbfad 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene50/TestForUtil.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene50/TestForUtil.java@@ -22,13 +22,13 @@ import static org.apache.lucene.codecs.lucene50.ForUtil.MAX_DATA_SIZE;import static org.apache.lucene.codecs.lucene50.ForUtil.MAX_ENCODED_SIZE;import java.io.IOException;-import java.util.Arrays;import org.apache.lucene.store.Directory;import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.RAMDirectory;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.packed.PackedInts;@@ -39,7 +39,7 @@ public class TestForUtil extends LuceneTestCase {public void testEncodeDecode() throws IOException {final int iterations = RandomNumbers.randomIntBetween(random(), 1, 1000);final float acceptableOverheadRatio = random().nextFloat();- final int[] values = new int[(iterations - 1) * BLOCK_SIZE + ForUtil.MAX_DATA_SIZE];+ final int[] values = new int[iterations * BLOCK_SIZE];for (int i = 0; i < iterations; ++i) {final int bpv = random().nextInt(32);if (bpv == 0) {@@ -64,9 +64,9 @@ public class TestForUtil extends LuceneTestCase {final ForUtil forUtil = new ForUtil(acceptableOverheadRatio, out);for (int i = 0; i < iterations; ++i) {- forUtil.writeBlock(- Arrays.copyOfRange(values, i * BLOCK_SIZE, values.length),- new byte[MAX_ENCODED_SIZE], out);+ // Although values after BLOCK_SIZE are garbage, we need to allocate extra bytes to avoid AIOOBE.+ int[] block = ArrayUtil.grow(ArrayUtil.copyOfSubArray(values, i*BLOCK_SIZE, (i+1)*BLOCK_SIZE));+ forUtil.writeBlock(ArrayUtil.grow(block, MAX_DATA_SIZE), new byte[MAX_ENCODED_SIZE], out);}endPointer = out.getFilePointer();out.close();@@ -83,8 +83,8 @@ public class TestForUtil extends LuceneTestCase {}final int[] restored = new int[MAX_DATA_SIZE];forUtil.readBlock(in, new byte[MAX_ENCODED_SIZE], restored);- assertArrayEquals(Arrays.copyOfRange(values, i * BLOCK_SIZE, (i + 1) * BLOCK_SIZE),- Arrays.copyOf(restored, BLOCK_SIZE));+ assertArrayEquals(ArrayUtil.copyOfSubArray(values, i*BLOCK_SIZE, (i+1)*BLOCK_SIZE),+ ArrayUtil.copyOfSubArray(restored, 0, BLOCK_SIZE));}assertEquals(endPointer, in.getFilePointer());in.close();diff --git a/lucene/core/src/test/org/apache/lucene/index/TestPerSegmentDeletes.java b/lucene/core/src/test/org/apache/lucene/index/TestPerSegmentDeletes.javaindex 4da5059487d..6e2bd13b0ce 100644--- a/lucene/core/src/test/org/apache/lucene/index/TestPerSegmentDeletes.java+++ b/lucene/core/src/test/org/apache/lucene/index/TestPerSegmentDeletes.java@@ -18,7 +18,6 @@ package org.apache.lucene.index;import java.io.IOException;-import java.util.Arrays;import java.util.Map;import java.util.Random;@@ -238,7 +237,7 @@ public class TestPerSegmentDeletes extends LuceneTestCase {docs = ArrayUtil.grow(docs, numDocs + 1);docs[numDocs + 1] = docID;}- return Arrays.copyOf(docs, numDocs);+ return ArrayUtil.copyOfSubArray(docs, 0, numDocs);}public static class RangeMergePolicy extends MergePolicy {diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java b/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.javaindex a9e2891140a..66b895a3b62 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java@@ -19,7 +19,6 @@ package org.apache.lucene.search;import java.io.IOException;-import java.util.Arrays;import java.util.Collections;import java.util.Random;@@ -38,6 +37,7 @@ import org.apache.lucene.index.Term;import org.apache.lucene.search.similarities.ClassicSimilarity;import org.apache.lucene.store.Directory;import org.apache.lucene.store.IOContext;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;import org.junit.AfterClass;@@ -227,7 +227,7 @@ public class TestBoolean2 extends LuceneTestCase {// adjust the expected doc numbers according to our filler docsif (0 < NUM_FILLER_DOCS) {- expDocNrs = Arrays.copyOf(expDocNrs, expDocNrs.length);+ expDocNrs = ArrayUtil.copyOfSubArray(expDocNrs, 0, expDocNrs.length);for (int i=0; i < expDocNrs.length; i++) {expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]);}diff --git a/lucene/core/src/test/org/apache/lucene/search/TestDoubleValuesSource.java b/lucene/core/src/test/org/apache/lucene/search/TestDoubleValuesSource.javaindex 5a5a3aee420..49b0e110563 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestDoubleValuesSource.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestDoubleValuesSource.java@@ -31,6 +31,7 @@ import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.English;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;@@ -122,7 +123,7 @@ public class TestDoubleValuesSource extends LuceneTestCase {};Collections.shuffle(Arrays.asList(fields), random());int numSorts = TestUtil.nextInt(random(), 1, fields.length);- return new Sort(Arrays.copyOfRange(fields, 0, numSorts));+ return new Sort(ArrayUtil.copyOfSubArray(fields, 0, numSorts));}// Take a Sort, and replace any field sorts with Sortablesdiff --git a/lucene/core/src/test/org/apache/lucene/search/TestLongValuesSource.java b/lucene/core/src/test/org/apache/lucene/search/TestLongValuesSource.javaindex 8b20be5169d..4c77e7e7d25 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestLongValuesSource.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestLongValuesSource.java@@ -27,6 +27,7 @@ import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.store.Directory;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.English;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;@@ -103,7 +104,7 @@ public class TestLongValuesSource extends LuceneTestCase {};Collections.shuffle(Arrays.asList(fields), random());int numSorts = TestUtil.nextInt(random(), 1, fields.length);- return new Sort(Arrays.copyOfRange(fields, 0, numSorts));+ return new Sort(ArrayUtil.copyOfSubArray(fields, 0, numSorts));}// Take a Sort, and replace any field sorts with Sortablesdiff --git a/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.javaindex eb311284bcd..7bd235e0c00 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java@@ -45,6 +45,7 @@ import org.apache.lucene.index.Term;import org.apache.lucene.search.similarities.BM25Similarity;import org.apache.lucene.search.similarities.ClassicSimilarity;import org.apache.lucene.store.Directory;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;import org.junit.AfterClass;@@ -731,7 +732,7 @@ public class TestPhraseQuery extends LuceneTestCase {public void testTopPhrases() throws IOException {Directory dir = newDirectory();IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());- String[] docs = Arrays.copyOf(DOCS, DOCS.length);+ String[] docs = ArrayUtil.copyOfSubArray(DOCS, 0, DOCS.length);Collections.shuffle(Arrays.asList(docs), random());for (String value : DOCS) {Document doc = new Document();diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.javaindex 9f506688c52..7e8a7614ae6 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java@@ -16,12 +16,11 @@*/package org.apache.lucene.search;-import java.util.Arrays;-import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.Term;import org.apache.lucene.index.RandomIndexWriter;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.TestUtil;import org.apache.lucene.util.LuceneTestCase.Slow;@@ -101,7 +100,7 @@ public class TestSimpleExplanationsWithFillerDocs extends TestSimpleExplanations@Overridepublic void qtest(Query q, int[] expDocNrs) throws Exception {- expDocNrs = Arrays.copyOf(expDocNrs, expDocNrs.length);+ expDocNrs = ArrayUtil.copyOfSubArray(expDocNrs, 0, expDocNrs.length);for (int i=0; i < expDocNrs.length; i++) {expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]);}diff --git a/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java b/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.javaindex 2db901baa1a..50532235c0d 100644--- a/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java+++ b/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java@@ -48,7 +48,7 @@ public abstract class BaseSortTestCase extends LuceneTestCase {public void assertSorted(Entry[] original, Entry[] sorted) {assertEquals(original.length, sorted.length);- Entry[] actuallySorted = Arrays.copyOf(original, original.length);+ Entry[] actuallySorted = ArrayUtil.copyOfSubArray(original, 0, original.length);Arrays.sort(actuallySorted);for (int i = 0; i < original.length; ++i) {assertEquals(actuallySorted[i].value, sorted[i].value);@@ -64,7 +64,7 @@ public abstract class BaseSortTestCase extends LuceneTestCase {System.arraycopy(arr, 0, toSort, o, arr.length);final Sorter sorter = newSorter(toSort);sorter.sort(o, o + arr.length);- assertSorted(arr, Arrays.copyOfRange(toSort, o, o + arr.length));+ assertSorted(arr, ArrayUtil.copyOfSubArray(toSort, o, o + arr.length));}enum Strategy {diff --git a/lucene/core/src/test/org/apache/lucene/util/StressRamUsageEstimator.java b/lucene/core/src/test/org/apache/lucene/util/StressRamUsageEstimator.javaindex 7a2712fcbd2..cb743f19561 100644--- a/lucene/core/src/test/org/apache/lucene/util/StressRamUsageEstimator.java+++ b/lucene/core/src/test/org/apache/lucene/util/StressRamUsageEstimator.java@@ -16,9 +16,6 @@*/package org.apache.lucene.util;--import java.util.Arrays;-/*** Estimates how {@link RamUsageEstimator} estimates physical memory consumption* of Java objects.@@ -88,7 +85,7 @@ public class StressRamUsageEstimator extends LuceneTestCase {// Make another batch of objects.Object[] seg = new Object[10000];- all = Arrays.copyOf(all, all.length + 1);+ all = ArrayUtil.growExact(all, all.length + 1);all[all.length - 1] = seg;for (int i = 0; i < seg.length; i++) {seg[i] = new byte[random().nextInt(7)];diff --git a/lucene/core/src/test/org/apache/lucene/util/TestArrayUtil.java b/lucene/core/src/test/org/apache/lucene/util/TestArrayUtil.javaindex 0cda337d4aa..285df74f078 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestArrayUtil.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestArrayUtil.java@@ -21,6 +21,9 @@ import java.util.Collections;import java.util.Comparator;import java.util.Random;+import static org.apache.lucene.util.ArrayUtil.copyOfSubArray;+import static org.apache.lucene.util.ArrayUtil.growExact;+public class TestArrayUtil extends LuceneTestCase {// Ensure ArrayUtil.getNextSize gives linear amortized cost of realloc/copy@@ -294,4 +297,88 @@ public class TestArrayUtil extends LuceneTestCase {}}}++ public void testGrowExact() {+ assertArrayEquals(new short[]{1, 2, 3, 0}, growExact(new short[]{1, 2, 3}, 4));+ assertArrayEquals(new short[]{1, 2, 3, 0, 0}, growExact(new short[]{1, 2, 3}, 5));+ expectThrows(IndexOutOfBoundsException.class, () -> growExact(new short[]{1, 2, 3}, random().nextInt(3)));++ assertArrayEquals(new int[]{1, 2, 3, 0}, growExact(new int[]{1, 2, 3}, 4));+ assertArrayEquals(new int[]{1, 2, 3, 0, 0}, growExact(new int[]{1, 2, 3}, 5));+ expectThrows(IndexOutOfBoundsException.class, () -> growExact(new int[]{1, 2, 3}, random().nextInt(3)));++ assertArrayEquals(new long[]{1, 2, 3, 0}, growExact(new long[]{1, 2, 3}, 4));+ assertArrayEquals(new long[]{1, 2, 3, 0, 0}, growExact(new long[]{1, 2, 3}, 5));+ expectThrows(IndexOutOfBoundsException.class, () -> growExact(new long[]{1, 2, 3}, random().nextInt(3)));++ assertArrayEquals(new float[]{0.1f, 0.2f, 0.3f, 0}, growExact(new float[]{0.1f, 0.2f, 0.3f}, 4), 0.001f);+ assertArrayEquals(new float[]{0.1f, 0.2f, 0.3f, 0, 0}, growExact(new float[]{0.1f, 0.2f, 0.3f}, 5), 0.001f);+ expectThrows(IndexOutOfBoundsException.class, () -> growExact(new float[]{1, 2, 3}, random().nextInt(3)));++ assertArrayEquals(new double[]{0.1, 0.2, 0.3, 0.0}, growExact(new double[]{0.1, 0.2, 0.3}, 4), 0.001);+ assertArrayEquals(new double[]{0.1, 0.2, 0.3, 0.0, 0.0}, growExact(new double[]{0.1, 0.2, 0.3}, 5), 0.001);+ expectThrows(IndexOutOfBoundsException.class, () -> growExact(new double[]{0.1, 0.2, 0.3}, random().nextInt(3)));++ assertArrayEquals(new byte[]{1, 2, 3, 0}, growExact(new byte[]{1, 2, 3}, 4));+ assertArrayEquals(new byte[]{1, 2, 3, 0, 0}, growExact(new byte[]{1, 2, 3}, 5));+ expectThrows(IndexOutOfBoundsException.class, () -> growExact(new byte[]{1, 2, 3}, random().nextInt(3)));++ assertArrayEquals(new char[]{'a', 'b', 'c', '\0'}, growExact(new char[]{'a', 'b', 'c'}, 4));+ assertArrayEquals(new char[]{'a', 'b', 'c', '\0', '\0'}, growExact(new char[]{'a', 'b', 'c'}, 5));+ expectThrows(IndexOutOfBoundsException.class, () -> growExact(new byte[]{'a', 'b', 'c'}, random().nextInt(3)));++ assertArrayEquals(new String[]{"a1", "b2", "c3", null}, growExact(new String[]{"a1", "b2", "c3"}, 4));+ assertArrayEquals(new String[]{"a1", "b2", "c3", null, null}, growExact(new String[]{"a1", "b2", "c3"}, 5));+ expectThrows(IndexOutOfBoundsException.class, () -> growExact(new String[]{"a", "b", "c"}, random().nextInt(3)));+ }++ public void testCopyOfSubArray() {+ short[] shortArray = {1, 2, 3};+ assertArrayEquals(new short[]{1}, copyOfSubArray(shortArray, 0, 1));+ assertArrayEquals(new short[]{1, 2, 3}, copyOfSubArray(shortArray, 0, 3));+ assertEquals(0, copyOfSubArray(shortArray, 0, 0).length);+ expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(shortArray, 0, 4 + random().nextInt(10)));++ int[] intArray = {1, 2, 3};+ assertArrayEquals(new int[]{1, 2}, copyOfSubArray(intArray, 0, 2));+ assertArrayEquals(new int[]{1, 2, 3}, copyOfSubArray(intArray, 0, 3));+ assertEquals(0, copyOfSubArray(intArray, 1, 1).length);+ expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(intArray, 1, 4 + random().nextInt(10)));++ long[] longArray = {1, 2, 3};+ assertArrayEquals(new long[]{2}, copyOfSubArray(longArray, 1, 2));+ assertArrayEquals(new long[]{1, 2, 3}, copyOfSubArray(longArray, 0, 3));+ assertEquals(0, copyOfSubArray(longArray, 2, 2).length);+ expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(longArray, 2, 4 + random().nextInt(10)));++ float[] floatArray = {0.1f, 0.2f, 0.3f};+ assertArrayEquals(new float[]{0.2f, 0.3f}, copyOfSubArray(floatArray, 1, 3), 0.001f);+ assertArrayEquals(new float[]{0.1f, 0.2f, 0.3f}, copyOfSubArray(floatArray, 0, 3), 0.001f);+ assertEquals(0, copyOfSubArray(floatArray, 0, 0).length);+ expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(floatArray, 3, 4 + random().nextInt(10)));++ double[] doubleArray = {0.1, 0.2, 0.3};+ assertArrayEquals(new double[]{0.3}, copyOfSubArray(doubleArray, 2, 3), 0.001);+ assertArrayEquals(new double[]{0.1, 0.2, 0.3}, copyOfSubArray(doubleArray, 0, 3), 0.001);+ assertEquals(0, copyOfSubArray(doubleArray, 1, 1).length);+ expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(doubleArray, 0, 4 + random().nextInt(10)));++ byte[] byteArray = {1, 2, 3};+ assertArrayEquals(new byte[]{1}, copyOfSubArray(byteArray, 0, 1));+ assertArrayEquals(new byte[]{1, 2, 3}, copyOfSubArray(byteArray, 0, 3));+ assertEquals(0, copyOfSubArray(byteArray, 1, 1).length);+ expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(byteArray, 1, 4 + random().nextInt(10)));++ char[] charArray = {'a', 'b', 'c'};+ assertArrayEquals(new char[]{'a', 'b'}, copyOfSubArray(charArray, 0, 2));+ assertArrayEquals(new char[]{'a', 'b', 'c'}, copyOfSubArray(charArray, 0, 3));+ assertEquals(0, copyOfSubArray(charArray, 1, 1).length);+ expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(charArray, 3, 4));++ String[] objectArray = {"a1", "b2", "c3"};+ assertArrayEquals(new String[]{"a1"}, copyOfSubArray(objectArray, 0, 1));+ assertArrayEquals(new String[]{"a1", "b2", "c3"}, copyOfSubArray(objectArray, 0, 3));+ assertEquals(0, copyOfSubArray(objectArray, 1, 1).length);+ expectThrows(IndexOutOfBoundsException.class, () -> copyOfSubArray(objectArray, 2, 5));+ }}diff --git a/lucene/core/src/test/org/apache/lucene/util/TestBytesRef.java b/lucene/core/src/test/org/apache/lucene/util/TestBytesRef.javaindex 3a5bb53c742..2a869adca37 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestBytesRef.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestBytesRef.java@@ -48,4 +48,12 @@ public class TestBytesRef extends LuceneTestCase {// only for 4.xassertEquals("\uFFFF", new BytesRef("\uFFFF").utf8ToString());}++ public void testInvalidDeepCopy() {+ BytesRef from = new BytesRef(new byte[] { 1, 2 });+ from.offset += 1; // now invalid+ expectThrows(IndexOutOfBoundsException.class, () -> {+ BytesRef.deepCopyOf(from);+ });+ }}diff --git a/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java b/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.javaindex 0a4c8844c85..079b3b774b5 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestCharsRef.java@@ -125,4 +125,12 @@ public class TestCharsRef extends LuceneTestCase {c.subSequence(2, 1);});}++ public void testInvalidDeepCopy() {+ CharsRef from = new CharsRef(new char[] { 'a', 'b' }, 0, 2);+ from.offset += 1; // now invalid+ expectThrows(IndexOutOfBoundsException.class, () -> {+ CharsRef.deepCopyOf(from);+ });+ }}diff --git a/lucene/core/src/test/org/apache/lucene/util/TestIntsRef.java b/lucene/core/src/test/org/apache/lucene/util/TestIntsRef.javaindex b9976592fa0..654e77d133d 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestIntsRef.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestIntsRef.java@@ -37,4 +37,12 @@ public class TestIntsRef extends LuceneTestCase {assertFalse(i.equals(i2));}++ public void testInvalidDeepCopy() {+ IntsRef from = new IntsRef(new int[] { 1, 2 }, 0, 2);+ from.offset += 1; // now invalid+ expectThrows(IndexOutOfBoundsException.class, () -> {+ IntsRef.deepCopyOf(from);+ });+ }}diff --git a/lucene/core/src/test/org/apache/lucene/util/TestLSBRadixSorter.java b/lucene/core/src/test/org/apache/lucene/util/TestLSBRadixSorter.javaindex ba8bd021d65..b7696c28aa2 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestLSBRadixSorter.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestLSBRadixSorter.java@@ -37,7 +37,7 @@ public class TestLSBRadixSorter extends LuceneTestCase {}public void test(LSBRadixSorter sorter, int[] arr, int len) {- final int[] expected = Arrays.copyOf(arr, len);+ final int[] expected = ArrayUtil.copyOfSubArray(arr, 0, len);Arrays.sort(expected);int numBits = 0;@@ -50,7 +50,7 @@ public class TestLSBRadixSorter extends LuceneTestCase {}sorter.sort(numBits, arr, len);- final int[] actual = Arrays.copyOf(arr, len);+ final int[] actual = ArrayUtil.copyOfSubArray(arr, 0, len);assertArrayEquals(expected, actual);}diff --git a/lucene/core/src/test/org/apache/lucene/util/TestLongsRef.java b/lucene/core/src/test/org/apache/lucene/util/TestLongsRef.javanew file mode 100644index 00000000000..ec4575fa48c--- /dev/null+++ b/lucene/core/src/test/org/apache/lucene/util/TestLongsRef.java@@ -0,0 +1,47 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.util;++public class TestLongsRef extends LuceneTestCase {+ public void testEmpty() {+ LongsRef i = new LongsRef();+ assertEquals(LongsRef.EMPTY_LONGS, i.longs);+ assertEquals(0, i.offset);+ assertEquals(0, i.length);+ }++ public void testFromLongs() {+ long longs[] = new long[] { 1, 2, 3, 4 };+ LongsRef i = new LongsRef(longs, 0, 4);+ assertEquals(longs, i.longs);+ assertEquals(0, i.offset);+ assertEquals(4, i.length);++ LongsRef i2 = new LongsRef(longs, 1, 3);+ assertEquals(new LongsRef(new long[] { 2, 3, 4 }, 0, 3), i2);++ assertFalse(i.equals(i2));+ }++ public void testInvalidDeepCopy() {+ LongsRef from = new LongsRef(new long[] { 1, 2 }, 0, 2);+ from.offset += 1; // now invalid+ expectThrows(IndexOutOfBoundsException.class, () -> {+ LongsRef.deepCopyOf(from);+ });+ }+}diff --git a/lucene/core/src/test/org/apache/lucene/util/TestMSBRadixSorter.java b/lucene/core/src/test/org/apache/lucene/util/TestMSBRadixSorter.javaindex 52eb494711a..efd1f0328e9 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestMSBRadixSorter.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestMSBRadixSorter.java@@ -23,7 +23,7 @@ import java.util.Set;public class TestMSBRadixSorter extends LuceneTestCase {private void test(BytesRef[] refs, int len) {- BytesRef[] expected = Arrays.copyOf(refs, len);+ BytesRef[] expected = ArrayUtil.copyOfSubArray(refs, 0, len);Arrays.sort(expected);int maxLength = 0;@@ -63,7 +63,7 @@ public class TestMSBRadixSorter extends LuceneTestCase {refs[j] = tmp;}}.sort(0, len);- BytesRef[] actual = Arrays.copyOf(refs, len);+ BytesRef[] actual = ArrayUtil.copyOfSubArray(refs, 0, len);assertArrayEquals(expected, actual);}diff --git a/lucene/core/src/test/org/apache/lucene/util/TestStringMSBRadixSorter.java b/lucene/core/src/test/org/apache/lucene/util/TestStringMSBRadixSorter.javaindex c83ff676404..c4ee68b2d1a 100644--- a/lucene/core/src/test/org/apache/lucene/util/TestStringMSBRadixSorter.java+++ b/lucene/core/src/test/org/apache/lucene/util/TestStringMSBRadixSorter.java@@ -21,7 +21,7 @@ import java.util.Arrays;public class TestStringMSBRadixSorter extends LuceneTestCase {private void test(BytesRef[] refs, int len) {- BytesRef[] expected = Arrays.copyOf(refs, len);+ BytesRef[] expected = ArrayUtil.copyOfSubArray(refs, 0, len);Arrays.sort(expected);new StringMSBRadixSorter() {@@ -38,7 +38,7 @@ public class TestStringMSBRadixSorter extends LuceneTestCase {refs[j] = tmp;}}.sort(0, len);- BytesRef[] actual = Arrays.copyOf(refs, len);+ BytesRef[] actual = ArrayUtil.copyOfSubArray(refs, 0, len);assertArrayEquals(expected, actual);}diff --git a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.javaindex a675e0b8cd0..69c1b3f7720 100644--- a/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java+++ b/lucene/core/src/test/org/apache/lucene/util/packed/TestPackedInts.java@@ -34,6 +34,7 @@ import org.apache.lucene.store.IOContext;import org.apache.lucene.store.IndexInput;import org.apache.lucene.store.IndexOutput;import org.apache.lucene.store.RAMDirectory;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.LongValues;import org.apache.lucene.util.LongsRef;import org.apache.lucene.util.LuceneTestCase;@@ -903,8 +904,8 @@ public class TestPackedInts extends LuceneTestCase {// 3. re-encodefinal long[] blocks2 = new long[blocksOffset2 + blocksLen];encoder.encode(values, valuesOffset, blocks2, blocksOffset2, longIterations);- assertArrayEquals(msg, Arrays.copyOfRange(blocks, blocksOffset, blocks.length),- Arrays.copyOfRange(blocks2, blocksOffset2, blocks2.length));+ assertArrayEquals(msg, ArrayUtil.copyOfSubArray(blocks, blocksOffset, blocks.length),+ ArrayUtil.copyOfSubArray(blocks2, blocksOffset2, blocks2.length));// test encoding from int[]if (bpv <= 32) {final long[] blocks3 = new long[blocks2.length];diff --git a/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSorts.java b/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSorts.javaindex cb24f6fe2a5..fe5317eb527 100644--- a/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSorts.java+++ b/lucene/expressions/src/test/org/apache/lucene/expressions/TestExpressionSorts.java@@ -40,6 +40,7 @@ import org.apache.lucene.search.SortField;import org.apache.lucene.search.TermQuery;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;+import org.apache.lucene.util.ArrayUtil;import org.apache.lucene.util.English;import org.apache.lucene.util.LuceneTestCase;import org.apache.lucene.util.TestUtil;@@ -109,7 +110,7 @@ public class TestExpressionSorts extends LuceneTestCase {};Collections.shuffle(Arrays.asList(fields), random());int numSorts = TestUtil.nextInt(random(), 1, fields.length);- assertQuery(query, new Sort(Arrays.copyOfRange(fields, 0, numSorts)));+ assertQuery(query, new Sort(ArrayUtil.copyOfSubArray(fields, 0, numSorts)));}}diff --git a/lucene/sandbox/src/java/org/apache/lucene/search/intervals/IntervalQuery.java b/lucene/sandbox/src/java/org/apache/lucene/search/intervals/IntervalQuery.javaindex 306c05973c6..c1125c2ff27 100644--- a/lucene/sandbox/src/java/org/apache/lucene/search/intervals/IntervalQuery.java+++ b/lucene/sandbox/src/java/org/apache/lucene/search/intervals/IntervalQuery.java@@ -18,7 +18,6 @@package org.apache.lucene.search.intervals;import java.io.IOException;-import java.util.Arrays;import java.util.HashSet;import java.util.Objects;import java.util.Set;@@ -36,6 +35,7 @@ import org.apache.lucene.search.Scorer;import org.apache.lucene.search.TermStatistics;import org.apache.lucene.search.Weight;import org.apache.lucene.search.similarities.Similarity;+import org.apache.lucene.util.ArrayUtil;/*** A query that retrieves documents containing intervals returned from an@@ -89,7 +89,7 @@ public final class IntervalQuery extends Query {return null;}CollectionStatistics collectionStats = searcher.collectionStatistics(field);- return searcher.getSimilarity().scorer(boost, collectionStats, Arrays.copyOf(termStats, termUpTo));+ return searcher.getSimilarity().scorer(boost, collectionStats, ArrayUtil.copyOfSubArray(termStats, 0, termUpTo));}@Overridediff --git a/lucene/sandbox/src/test/org/apache/lucene/document/TestHalfFloatPoint.java b/lucene/sandbox/src/test/org/apache/lucene/document/TestHalfFloatPoint.javaindex 0bcb3f8b844..9f7080846b6 100644--- a/lucene/sandbox/src/test/org/apache/lucene/document/TestHalfFloatPoint.java+++ b/lucene/sandbox/src/test/org/apache/lucene/document/TestHalfFloatPoint.java@@ -89,7 +89,7 @@ public class TestHalfFloatPoint extends LuceneTestCase {values[o++] = v;}}- values = Arrays.copyOf(values, o);+ values = ArrayUtil.copyOfSubArray(values, 0, o);int iters = atLeast(1000000);for (int iter = 0; iter < iters; ++iter) {diff --git a/lucene/spatial-extras/src/java/org/apache/lucene/spatial/prefix/tree/QuadPrefixTree.java b/lucene/spatial-extras/src/java/org/apache/lucene/spatial/prefix/tree/QuadPrefixTree.javaindex 3242e7e623f..9bc947fbbda 100644--- a/lucene/spatial-extras/src/java/org/apache/lucene/spatial/prefix/tree/QuadPrefixTree.java+++ b/lucene/spatial-extras/src/java/org/apache/lucene/spatial/prefix/tree/QuadPrefixTree.java@@ -19,7 +19,6 @@ package org.apache.lucene.spatial.prefix.tree;import java.io.PrintStream;import java.text.NumberFormat;import java.util.ArrayList;-import java.util.Arrays;import java.util.Collection;import java.util.List;import java.util.Locale;@@ -245,7 +244,8 @@ public class QuadPrefixTree extends LegacyPrefixTree {protected BytesRef concat(BytesRef source, byte b) {//+2 for new char + potential leaf- final byte[] buffer = Arrays.copyOfRange(source.bytes, source.offset, source.offset + source.length + 2);+ final byte[] buffer = new byte[source.length + 2];+ System.arraycopy(source.bytes, source.offset, buffer, 0, source.length);BytesRef target = new BytesRef(buffer);target.length = source.length;target.bytes[target.length++] = b;diff --git a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/DistanceStrategyTest.java b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/DistanceStrategyTest.javaindex 3e3b2e2abcb..989252e3c6d 100644--- a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/DistanceStrategyTest.java+++ b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/DistanceStrategyTest.java@@ -18,7 +18,6 @@ package org.apache.lucene.spatial;import java.io.IOException;import java.util.ArrayList;-import java.util.Arrays;import java.util.List;import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;@@ -31,6 +30,7 @@ import org.apache.lucene.spatial.prefix.tree.QuadPrefixTree;import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;import org.apache.lucene.spatial.serialized.SerializedDVStrategy;import org.apache.lucene.spatial.vector.PointVectorStrategy;+import org.apache.lucene.util.ArrayUtil;import org.junit.Test;import org.locationtech.spatial4j.context.SpatialContext;import org.locationtech.spatial4j.shape.Point;@@ -107,7 +107,7 @@ public class DistanceStrategyTest extends StrategyTestCase {void checkDistValueSource(Point pt, float... distances) throws IOException {float multiplier = random().nextFloat() * 100f;- float[] dists2 = Arrays.copyOf(distances, distances.length);+ float[] dists2 = ArrayUtil.copyOfSubArray(distances, 0, distances.length);for (int i = 0; i < dists2.length; i++) {dists2[i] *= multiplier;}diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.javaindex 2c5dcd8e540..c25b44d5706 100644--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java@@ -17,7 +17,6 @@package org.apache.lucene.search.suggest.document;import java.util.ArrayList;-import java.util.Arrays;import java.util.Comparator;import java.util.HashSet;import java.util.List;@@ -521,7 +520,7 @@ public class TestContextQuery extends LuceneTestCase {query.addContext(contexts.get(i), i + 1);}TopSuggestDocs suggest = suggestIndexSearcher.suggest(query, 4, false);- assertSuggestions(suggest, Arrays.copyOfRange(expectedResults, 0, 4));+ assertSuggestions(suggest, ArrayUtil.copyOfSubArray(expectedResults, 0, 4));}}}diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BaseStoredFieldsFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BaseStoredFieldsFormatTestCase.javaindex 60e2cca0249..82d8adb08e4 100644--- a/lucene/test-framework/src/java/org/apache/lucene/index/BaseStoredFieldsFormatTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/index/BaseStoredFieldsFormatTestCase.java@@ -559,7 +559,7 @@ public abstract class BaseStoredFieldsFormatTestCase extends BaseIndexFileFormatfor (int j = 0; j < data[docId].length; ++j) {final byte[] arr = data[docId][j];final BytesRef arr2Ref = doc.getBinaryValue("bytes" + j);- final byte[] arr2 = Arrays.copyOfRange(arr2Ref.bytes, arr2Ref.offset, arr2Ref.offset + arr2Ref.length);+ final byte[] arr2 = BytesRef.deepCopyOf(arr2Ref).bytes;assertArrayEquals(arr, arr2);}}diff --git a/lucene/test-framework/src/java/org/apache/lucene/search/BlockScoreQueryWrapper.java b/lucene/test-framework/src/java/org/apache/lucene/search/BlockScoreQueryWrapper.javaindex 3b9a740a448..b15fa280fb6 100644--- a/lucene/test-framework/src/java/org/apache/lucene/search/BlockScoreQueryWrapper.java+++ b/lucene/test-framework/src/java/org/apache/lucene/search/BlockScoreQueryWrapper.java@@ -97,10 +97,8 @@ public final class BlockScoreQueryWrapper extends Query {DocIdSetIterator it = inScorer.iterator();int i = 1;for (int doc = it.nextDoc(); ; doc = it.nextDoc()) {- if (i == tmpDocs.length) {- tmpDocs = ArrayUtil.grow(tmpDocs);- tmpScores = Arrays.copyOf(tmpScores, tmpDocs.length);- }+ tmpDocs = ArrayUtil.grow(tmpDocs, i + 1);+ tmpScores = ArrayUtil.grow(tmpScores, i + 1);tmpDocs[i] = doc;if (doc == DocIdSetIterator.NO_MORE_DOCS) {i++;@@ -109,8 +107,8 @@ public final class BlockScoreQueryWrapper extends Query {tmpScores[i] = inScorer.score();i++;}- final int[] docs = Arrays.copyOf(tmpDocs, i);- final float[] scores = Arrays.copyOf(tmpScores, i);+ final int[] docs = ArrayUtil.copyOfSubArray(tmpDocs, 0, i);+ final float[] scores = ArrayUtil.copyOfSubArray(tmpScores, 0, i);return new Scorer(inWeight) {diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java b/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.javaindex 8ef4febea9c..e3f26e46032 100644--- a/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java+++ b/lucene/test-framework/src/java/org/apache/lucene/util/automaton/AutomatonTestUtil.java@@ -17,7 +17,6 @@package org.apache.lucene.util.automaton;import java.util.ArrayList;-import java.util.Arrays;import java.util.HashMap;import java.util.HashSet;import java.util.LinkedList;@@ -253,8 +252,7 @@ public class AutomatonTestUtil {codePoints[codepointCount++] = getRandomCodePoint(r, t.min, t.max);s = t.dest;}-- return Arrays.copyOf(codePoints, codepointCount);+ return ArrayUtil.copyOfSubArray(codePoints, 0, codepointCount);}}diff --git a/lucene/tools/forbiddenApis/lucene.txt b/lucene/tools/forbiddenApis/lucene.txtindex e02bd40ad01..2a1d88390bf 100644--- a/lucene/tools/forbiddenApis/lucene.txt+++ b/lucene/tools/forbiddenApis/lucene.txt@@ -27,3 +27,27 @@ java.util.zip.ZipFile@defaultMessage Use home-grown methods insteadjava.lang.Math#toRadians(double)java.lang.Math#toDegrees(double)++@defaultMessage Prefer using ArrayUtil as Arrays#copyOfRange fills zeros for bad bounds+java.util.Arrays#copyOfRange(byte[],int,int)+java.util.Arrays#copyOfRange(char[],int,int)+java.util.Arrays#copyOfRange(short[],int,int)+java.util.Arrays#copyOfRange(int[],int,int)+java.util.Arrays#copyOfRange(long[],int,int)+java.util.Arrays#copyOfRange(float[],int,int)+java.util.Arrays#copyOfRange(double[],int,int)+java.util.Arrays#copyOfRange(boolean[],int,int)+java.util.Arrays#copyOfRange(java.lang.Object[],int,int)+java.util.Arrays#copyOfRange(java.lang.Object[],int,int,java.lang.Class)++@defaultMessage Prefer using ArrayUtil as Arrays#copyOf fills zeros for bad bounds+java.util.Arrays#copyOf(byte[],int)+java.util.Arrays#copyOf(char[],int)+java.util.Arrays#copyOf(short[],int)+java.util.Arrays#copyOf(int[],int)+java.util.Arrays#copyOf(long[],int)+java.util.Arrays#copyOf(float[],int)+java.util.Arrays#copyOf(double[],int)+java.util.Arrays#copyOf(boolean[],int)+java.util.Arrays#copyOf(java.lang.Object[],int)+java.util.Arrays#copyOf(java.lang.Object[],int,java.lang.Class)