From a18e2812755e848ae906d01c3c0ad36631c254f2 Mon Sep 17 00:00:00 2001 From: christosts Date: Tue, 12 Oct 2021 17:31:28 +0100 Subject: [PATCH] SubtitleExtractor: optimize calls to ExtractorInput.read() When ExtractorInput.getLength() returns a defined length, the SubtitleExtractor will create a buffer of the same length, call ExtractorInput.read() until it has read the input bytes, plus one more time where ExtractorInput.read() returns RESULT_END_OF_INPUT. The last call to ExtractorInput.read() however will make the SubtitleExtractor to increase its buffer (including a copy) unnecessarily. This change makes the SubtitleExtractor avoid calling ExtractorInput.read() if the expected number of bytes have already been read, so that the internal buffer does not grow. PiperOrigin-RevId: 402583610 --- .../com/google/android/exoplayer2/text/SubtitleExtractor.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/extractor/src/main/java/com/google/android/exoplayer2/text/SubtitleExtractor.java b/library/extractor/src/main/java/com/google/android/exoplayer2/text/SubtitleExtractor.java index 0336f23de1..be3a582a3d 100644 --- a/library/extractor/src/main/java/com/google/android/exoplayer2/text/SubtitleExtractor.java +++ b/library/extractor/src/main/java/com/google/android/exoplayer2/text/SubtitleExtractor.java @@ -199,7 +199,9 @@ public class SubtitleExtractor implements Extractor { if (readResult != C.RESULT_END_OF_INPUT) { bytesRead += readResult; } - return readResult == C.RESULT_END_OF_INPUT; + long inputLength = input.getLength(); + return (inputLength != C.LENGTH_UNSET && bytesRead == inputLength) + || readResult == C.RESULT_END_OF_INPUT; } /** Decodes the subtitle data and stores the samples in the memory of the extractor. */