Add a Util.nullSafeArrayAppend and use it in HlsSampleStreamWrapper

I could do this with nullSafeArrayConcat but this saves allocating a throw-away
single-element array.

PiperOrigin-RevId: 274545815
This commit is contained in:
ibaker 2019-10-14 11:33:44 +01:00 committed by Oliver Woodman
parent 498a23e328
commit eb677eb4b9
2 changed files with 19 additions and 6 deletions

View File

@ -336,7 +336,20 @@ public final class Util {
} }
/** /**
* Concatenates two non-null type arrays. * Creates a new array containing {@code original} with {@code newElement} appended.
*
* @param original The input array.
* @param newElement The element to append.
* @return The new array.
*/
public static <T> T[] nullSafeArrayAppend(T[] original, T newElement) {
@NullableType T[] result = Arrays.copyOf(original, original.length + 1);
result[original.length] = newElement;
return castNonNullTypeArray(result);
}
/**
* Creates a new array containing the concatenation of two non-null type arrays.
* *
* @param first The first array. * @param first The first array.
* @param second The second array. * @param second The second array.

View File

@ -905,11 +905,11 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
trackOutput.setUpstreamFormatChangeListener(this); trackOutput.setUpstreamFormatChangeListener(this);
sampleQueueTrackIds = Arrays.copyOf(sampleQueueTrackIds, trackCount + 1); sampleQueueTrackIds = Arrays.copyOf(sampleQueueTrackIds, trackCount + 1);
sampleQueueTrackIds[trackCount] = id; sampleQueueTrackIds[trackCount] = id;
sampleQueues = Arrays.copyOf(sampleQueues, trackCount + 1); sampleQueues = Util.nullSafeArrayAppend(sampleQueues, trackOutput);
sampleQueues[trackCount] = trackOutput; sampleQueueReaders =
sampleQueueReaders = Arrays.copyOf(sampleQueueReaders, trackCount + 1); Util.nullSafeArrayAppend(
sampleQueueReaders[trackCount] = sampleQueueReaders,
new DecryptableSampleQueueReader(sampleQueues[trackCount], drmSessionManager); new DecryptableSampleQueueReader(sampleQueues[trackCount], drmSessionManager));
sampleQueueIsAudioVideoFlags = Arrays.copyOf(sampleQueueIsAudioVideoFlags, trackCount + 1); sampleQueueIsAudioVideoFlags = Arrays.copyOf(sampleQueueIsAudioVideoFlags, trackCount + 1);
sampleQueueIsAudioVideoFlags[trackCount] = sampleQueueIsAudioVideoFlags[trackCount] =
type == C.TRACK_TYPE_AUDIO || type == C.TRACK_TYPE_VIDEO; type == C.TRACK_TYPE_AUDIO || type == C.TRACK_TYPE_VIDEO;