Clip the skip length in DefaultExtractorInput.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=117141509
This commit is contained in:
olly 2016-03-14 09:41:10 -07:00 committed by Oliver Woodman
parent a6ba84ade6
commit 2380857bf2
2 changed files with 21 additions and 1 deletions

View File

@ -34,6 +34,7 @@ public class DefaultExtractorInputTest extends TestCase {
private static final String TEST_URI = "http://www.google.com"; private static final String TEST_URI = "http://www.google.com";
private static final byte[] TEST_DATA = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8}; private static final byte[] TEST_DATA = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8};
private static final int LARGE_TEST_DATA_LENGTH = 8192;
public void testInitialPosition() throws IOException { public void testInitialPosition() throws IOException {
FakeDataSource testDataSource = buildDataSource(); FakeDataSource testDataSource = buildDataSource();
@ -182,6 +183,16 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(-1, expectedEndOfInput); assertEquals(-1, expectedEndOfInput);
} }
public void testLargeSkip() throws IOException, InterruptedException {
FakeDataSource testDataSource = buildLargeDataSource();
DefaultExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNBOUNDED);
// Check that skipping the entire data source succeeds.
int bytesToSkip = LARGE_TEST_DATA_LENGTH;
while (bytesToSkip > 0) {
bytesToSkip -= input.skip(bytesToSkip);
}
}
public void testSkipFullyOnce() throws IOException, InterruptedException { public void testSkipFullyOnce() throws IOException, InterruptedException {
// Skip TEST_DATA. // Skip TEST_DATA.
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
@ -391,6 +402,14 @@ public class DefaultExtractorInputTest extends TestCase {
return testDataSource; return testDataSource;
} }
private static FakeDataSource buildLargeDataSource() throws IOException {
FakeDataSource.Builder builder = new FakeDataSource.Builder();
builder.appendReadData(new byte[LARGE_TEST_DATA_LENGTH]);
FakeDataSource testDataSource = builder.build();
testDataSource.open(new DataSpec(Uri.parse(TEST_URI)));
return testDataSource;
}
private static DefaultExtractorInput createDefaultExtractorInput() throws IOException { private static DefaultExtractorInput createDefaultExtractorInput() throws IOException {
FakeDataSource testDataSource = buildDataSource(); FakeDataSource testDataSource = buildDataSource();
return new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNBOUNDED); return new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNBOUNDED);

View File

@ -80,7 +80,8 @@ public final class DefaultExtractorInput implements ExtractorInput {
public int skip(int length) throws IOException, InterruptedException { public int skip(int length) throws IOException, InterruptedException {
int bytesSkipped = skipFromPeekBuffer(length); int bytesSkipped = skipFromPeekBuffer(length);
if (bytesSkipped == 0) { if (bytesSkipped == 0) {
bytesSkipped = readFromDataSource(SCRATCH_SPACE, 0, length, 0, true); bytesSkipped =
readFromDataSource(SCRATCH_SPACE, 0, Math.min(length, SCRATCH_SPACE.length), 0, true);
} }
commitBytesRead(bytesSkipped); commitBytesRead(bytesSkipped);
return bytesSkipped; return bytesSkipped;