diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java index 213745f93d..13f61dec56 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java @@ -405,6 +405,21 @@ public final class Util { return concatenation; } + /** + * Copies the contents of {@code list} into {@code array}. + * + *

{@code list.size()} must be the same as {@code array.length} to ensure the contents can be + * copied into {@code array} without leaving any nulls at the end. + * + * @param list The list to copy items from. + * @param array The array to copy items to. + */ + @SuppressWarnings("nullness:toArray.nullable.elements.not.newarray") + public static void nullSafeListToArray(List list, T[] array) { + Assertions.checkState(list.size() == array.length); + list.toArray(array); + } + /** * Creates a {@link Handler} on the current {@link Looper} thread. * diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveMediaPeriod.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveMediaPeriod.java index d3eec0b85b..4a3b9e923e 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveMediaPeriod.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeAdaptiveMediaPeriod.java @@ -31,6 +31,7 @@ import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy; import com.google.android.exoplayer2.upstream.TransferListener; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.MimeTypes; +import com.google.android.exoplayer2.util.Util; import java.util.ArrayList; import java.util.List; import org.checkerframework.checker.nullness.compatqual.NullableType; @@ -48,7 +49,7 @@ public class FakeAdaptiveMediaPeriod extends FakeMediaPeriod @Nullable private final TransferListener transferListener; private final long durationUs; - @MonotonicNonNull private Callback callback; + private @MonotonicNonNull Callback callback; private ChunkSampleStream[] sampleStreams; private SequenceableLoader sequenceableLoader; @@ -99,7 +100,7 @@ public class FakeAdaptiveMediaPeriod extends FakeMediaPeriod } } sampleStreams = newSampleStreamArray(validStreams.size()); - validStreams.toArray(sampleStreams); + Util.nullSafeListToArray(validStreams, sampleStreams); this.sequenceableLoader = new CompositeSequenceableLoader(sampleStreams); return returnPositionUs; } diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSource.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSource.java index a9ca00ac64..5f858bea99 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSource.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeDataSource.java @@ -217,8 +217,7 @@ public class FakeDataSource extends BaseDataSource { * this method. */ public final DataSpec[] getAndClearOpenedDataSpecs() { - DataSpec[] dataSpecs = new DataSpec[openedDataSpecs.size()]; - openedDataSpecs.toArray(dataSpecs); + DataSpec[] dataSpecs = openedDataSpecs.toArray(new DataSpec[0]); openedDataSpecs.clear(); return dataSpecs; }