Commit 96998895 by ibaker Committed by Oliver Woodman

Assert that a negative extractor sniff never advances the read position

Extractor#sniff() javadoc says:
"If true is returned, the input's reading position may have been
modified. Otherwise, only its peek position may have been modified."
https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/extractor/Extractor.html#sniff-com.google.android.exoplayer2.extractor.ExtractorInput-

PiperOrigin-RevId: 314296922
parent 1f80cf15
...@@ -54,7 +54,8 @@ import java.io.IOException; ...@@ -54,7 +54,8 @@ import java.io.IOException;
public void init( public void init(
DataReader dataReader, Uri uri, long position, long length, ExtractorOutput output) DataReader dataReader, Uri uri, long position, long length, ExtractorOutput output)
throws IOException { throws IOException {
extractorInput = new DefaultExtractorInput(dataReader, position, length); ExtractorInput extractorInput = new DefaultExtractorInput(dataReader, position, length);
this.extractorInput = extractorInput;
if (extractor != null) { if (extractor != null) {
return; return;
} }
...@@ -70,6 +71,7 @@ import java.io.IOException; ...@@ -70,6 +71,7 @@ import java.io.IOException;
} catch (EOFException e) { } catch (EOFException e) {
// Do nothing. // Do nothing.
} finally { } finally {
Assertions.checkState(this.extractor != null || extractorInput.getPosition() == position);
extractorInput.resetPeekPosition(); extractorInput.resetPeekPosition();
} }
} }
......
...@@ -155,9 +155,13 @@ public final class ExtractorAsserts { ...@@ -155,9 +155,13 @@ public final class ExtractorAsserts {
*/ */
public static void assertSniff( public static void assertSniff(
Extractor extractor, FakeExtractorInput input, boolean expectedResult) throws IOException { Extractor extractor, FakeExtractorInput input, boolean expectedResult) throws IOException {
long originalPosition = input.getPosition();
while (true) { while (true) {
try { try {
assertThat(extractor.sniff(input)).isEqualTo(expectedResult); assertThat(extractor.sniff(input)).isEqualTo(expectedResult);
if (!expectedResult) {
assertThat(input.getPosition()).isEqualTo(originalPosition);
}
return; return;
} catch (SimulatedIOException e) { } catch (SimulatedIOException e) {
// Ignore. // Ignore.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment