Commit b1f9798b by andrewlewis Committed by Oliver Woodman

Fix peeking the end of the stream then reading it.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133240502
parent ab49425e
...@@ -33,14 +33,14 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -33,14 +33,14 @@ public class DefaultExtractorInputTest extends TestCase {
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; private static final int LARGE_TEST_DATA_LENGTH = 8192;
public void testInitialPosition() throws IOException { public void testInitialPosition() throws Exception {
FakeDataSource testDataSource = buildDataSource(); FakeDataSource testDataSource = buildDataSource();
DefaultExtractorInput input = DefaultExtractorInput input =
new DefaultExtractorInput(testDataSource, 123, C.LENGTH_UNSET); new DefaultExtractorInput(testDataSource, 123, C.LENGTH_UNSET);
assertEquals(123, input.getPosition()); assertEquals(123, input.getPosition());
} }
public void testRead() throws IOException, InterruptedException { public void testRead() throws Exception {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length]; byte[] target = new byte[TEST_DATA.length];
// We expect to perform three reads of three bytes, as setup in buildTestDataSource. // We expect to perform three reads of three bytes, as setup in buildTestDataSource.
...@@ -58,7 +58,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -58,7 +58,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(C.RESULT_END_OF_INPUT, expectedEndOfInput); assertEquals(C.RESULT_END_OF_INPUT, expectedEndOfInput);
} }
public void testReadPeeked() throws IOException, InterruptedException { public void testReadPeeked() throws Exception {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length]; byte[] target = new byte[TEST_DATA.length];
...@@ -71,7 +71,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -71,7 +71,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertTrue(Arrays.equals(TEST_DATA, target)); assertTrue(Arrays.equals(TEST_DATA, target));
} }
public void testReadMoreDataPeeked() throws IOException, InterruptedException { public void testReadMoreDataPeeked() throws Exception {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length]; byte[] target = new byte[TEST_DATA.length];
...@@ -84,7 +84,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -84,7 +84,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertTrue(Arrays.equals(TEST_DATA, target)); assertTrue(Arrays.equals(TEST_DATA, target));
} }
public void testReadFullyOnce() throws IOException, InterruptedException { public void testReadFullyOnce() throws Exception {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length]; byte[] target = new byte[TEST_DATA.length];
input.readFully(target, 0, TEST_DATA.length); input.readFully(target, 0, TEST_DATA.length);
...@@ -103,7 +103,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -103,7 +103,7 @@ public class DefaultExtractorInputTest extends TestCase {
} }
} }
public void testReadFullyTwice() throws IOException, InterruptedException { public void testReadFullyTwice() throws Exception {
// Read TEST_DATA in two parts. // Read TEST_DATA in two parts.
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[5]; byte[] target = new byte[5];
...@@ -116,7 +116,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -116,7 +116,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(5 + 4, input.getPosition()); assertEquals(5 + 4, input.getPosition());
} }
public void testReadFullyTooMuch() throws IOException, InterruptedException { public void testReadFullyTooMuch() throws Exception {
// Read more than TEST_DATA. Should fail with an EOFException. Position should not update. // Read more than TEST_DATA. Should fail with an EOFException. Position should not update.
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
try { try {
...@@ -141,7 +141,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -141,7 +141,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(0, input.getPosition()); assertEquals(0, input.getPosition());
} }
public void testReadFullyWithFailingDataSource() throws IOException, InterruptedException { public void testReadFullyWithFailingDataSource() throws Exception {
FakeDataSource testDataSource = buildFailingDataSource(); FakeDataSource testDataSource = buildFailingDataSource();
DefaultExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET); DefaultExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET);
try { try {
...@@ -155,7 +155,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -155,7 +155,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(0, input.getPosition()); assertEquals(0, input.getPosition());
} }
public void testReadFullyHalfPeeked() throws IOException, InterruptedException { public void testReadFullyHalfPeeked() throws Exception {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length]; byte[] target = new byte[TEST_DATA.length];
...@@ -168,7 +168,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -168,7 +168,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(TEST_DATA.length, input.getPosition()); assertEquals(TEST_DATA.length, input.getPosition());
} }
public void testSkip() throws IOException, InterruptedException { public void testSkip() throws Exception {
FakeDataSource testDataSource = buildDataSource(); FakeDataSource testDataSource = buildDataSource();
DefaultExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET); DefaultExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET);
// We expect to perform three skips of three bytes, as setup in buildTestDataSource. // We expect to perform three skips of three bytes, as setup in buildTestDataSource.
...@@ -180,7 +180,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -180,7 +180,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(C.RESULT_END_OF_INPUT, expectedEndOfInput); assertEquals(C.RESULT_END_OF_INPUT, expectedEndOfInput);
} }
public void testLargeSkip() throws IOException, InterruptedException { public void testLargeSkip() throws Exception {
FakeDataSource testDataSource = buildLargeDataSource(); FakeDataSource testDataSource = buildLargeDataSource();
DefaultExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET); DefaultExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET);
// Check that skipping the entire data source succeeds. // Check that skipping the entire data source succeeds.
...@@ -190,7 +190,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -190,7 +190,7 @@ public class DefaultExtractorInputTest extends TestCase {
} }
} }
public void testSkipFullyOnce() throws IOException, InterruptedException { public void testSkipFullyOnce() throws Exception {
// Skip TEST_DATA. // Skip TEST_DATA.
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
input.skipFully(TEST_DATA.length); input.skipFully(TEST_DATA.length);
...@@ -207,7 +207,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -207,7 +207,7 @@ public class DefaultExtractorInputTest extends TestCase {
} }
} }
public void testSkipFullyTwice() throws IOException, InterruptedException { public void testSkipFullyTwice() throws Exception {
// Skip TEST_DATA in two parts. // Skip TEST_DATA in two parts.
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
input.skipFully(5); input.skipFully(5);
...@@ -216,7 +216,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -216,7 +216,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(5 + 4, input.getPosition()); assertEquals(5 + 4, input.getPosition());
} }
public void testSkipFullyTwicePeeked() throws IOException, InterruptedException { public void testSkipFullyTwicePeeked() throws Exception {
// Skip TEST_DATA. // Skip TEST_DATA.
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
...@@ -230,7 +230,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -230,7 +230,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(TEST_DATA.length, input.getPosition()); assertEquals(TEST_DATA.length, input.getPosition());
} }
public void testSkipFullyTooMuch() throws IOException, InterruptedException { public void testSkipFullyTooMuch() throws Exception {
// Skip more than TEST_DATA. Should fail with an EOFException. Position should not update. // Skip more than TEST_DATA. Should fail with an EOFException. Position should not update.
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
try { try {
...@@ -253,7 +253,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -253,7 +253,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(0, input.getPosition()); assertEquals(0, input.getPosition());
} }
public void testSkipFullyWithFailingDataSource() throws IOException, InterruptedException { public void testSkipFullyWithFailingDataSource() throws Exception {
FakeDataSource testDataSource = buildFailingDataSource(); FakeDataSource testDataSource = buildFailingDataSource();
DefaultExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET); DefaultExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET);
try { try {
...@@ -266,7 +266,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -266,7 +266,7 @@ public class DefaultExtractorInputTest extends TestCase {
assertEquals(0, input.getPosition()); assertEquals(0, input.getPosition());
} }
public void testSkipFullyLarge() throws IOException, InterruptedException { public void testSkipFullyLarge() throws Exception {
// Tests skipping an amount of data that's larger than any internal scratch space. // Tests skipping an amount of data that's larger than any internal scratch space.
int largeSkipSize = 1024 * 1024; int largeSkipSize = 1024 * 1024;
FakeDataSource.Builder builder = new FakeDataSource.Builder(); FakeDataSource.Builder builder = new FakeDataSource.Builder();
...@@ -286,7 +286,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -286,7 +286,7 @@ public class DefaultExtractorInputTest extends TestCase {
} }
} }
public void testPeekFully() throws IOException, InterruptedException { public void testPeekFully() throws Exception {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length]; byte[] target = new byte[TEST_DATA.length];
input.peekFully(target, 0, TEST_DATA.length); input.peekFully(target, 0, TEST_DATA.length);
...@@ -312,7 +312,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -312,7 +312,7 @@ public class DefaultExtractorInputTest extends TestCase {
} }
} }
public void testResetPeekPosition() throws IOException, InterruptedException { public void testResetPeekPosition() throws Exception {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length]; byte[] target = new byte[TEST_DATA.length];
input.peekFully(target, 0, TEST_DATA.length); input.peekFully(target, 0, TEST_DATA.length);
...@@ -336,8 +336,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -336,8 +336,7 @@ public class DefaultExtractorInputTest extends TestCase {
} }
} }
public void testPeekFullyAtEndOfStreamWithAllowEndOfInputSucceeds() public void testPeekFullyAtEndOfStreamWithAllowEndOfInputSucceeds() throws Exception {
throws IOException, InterruptedException {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length]; byte[] target = new byte[TEST_DATA.length];
...@@ -348,8 +347,24 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -348,8 +347,24 @@ public class DefaultExtractorInputTest extends TestCase {
assertFalse(input.peekFully(target, 0, 1, true)); assertFalse(input.peekFully(target, 0, 1, true));
} }
public void testPeekFullyAcrossEndOfInputWithAllowEndOfInputFails() public void testPeekFullyAtEndThenReadEndOfInput() throws Exception {
throws IOException, InterruptedException { DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length];
// Peek up to the end of the input.
assertTrue(input.peekFully(target, 0, TEST_DATA.length, false));
// Peek the end of the input.
assertFalse(input.peekFully(target, 0, 1, true));
// Read up to the end of the input.
assertTrue(input.readFully(target, 0, TEST_DATA.length, false));
// Read the end of the input.
assertFalse(input.readFully(target, 0, 1, true));
}
public void testPeekFullyAcrossEndOfInputWithAllowEndOfInputFails() throws Exception {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length]; byte[] target = new byte[TEST_DATA.length];
...@@ -365,8 +380,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -365,8 +380,7 @@ public class DefaultExtractorInputTest extends TestCase {
} }
} }
public void testResetAndPeekFullyPastEndOfStreamWithAllowEndOfInputFails() public void testResetAndPeekFullyPastEndOfStreamWithAllowEndOfInputFails() throws Exception {
throws IOException, InterruptedException {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();
byte[] target = new byte[TEST_DATA.length]; byte[] target = new byte[TEST_DATA.length];
...@@ -382,7 +396,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -382,7 +396,7 @@ public class DefaultExtractorInputTest extends TestCase {
} }
} }
private static FakeDataSource buildDataSource() throws IOException { private static FakeDataSource buildDataSource() throws Exception {
FakeDataSource.Builder builder = new FakeDataSource.Builder(); FakeDataSource.Builder builder = new FakeDataSource.Builder();
builder.appendReadData(Arrays.copyOfRange(TEST_DATA, 0, 3)); builder.appendReadData(Arrays.copyOfRange(TEST_DATA, 0, 3));
builder.appendReadData(Arrays.copyOfRange(TEST_DATA, 3, 6)); builder.appendReadData(Arrays.copyOfRange(TEST_DATA, 3, 6));
...@@ -392,7 +406,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -392,7 +406,7 @@ public class DefaultExtractorInputTest extends TestCase {
return testDataSource; return testDataSource;
} }
private static FakeDataSource buildFailingDataSource() throws IOException { private static FakeDataSource buildFailingDataSource() throws Exception {
FakeDataSource.Builder builder = new FakeDataSource.Builder(); FakeDataSource.Builder builder = new FakeDataSource.Builder();
builder.appendReadData(Arrays.copyOfRange(TEST_DATA, 0, 6)); builder.appendReadData(Arrays.copyOfRange(TEST_DATA, 0, 6));
builder.appendReadError(new IOException()); builder.appendReadError(new IOException());
...@@ -402,7 +416,7 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -402,7 +416,7 @@ public class DefaultExtractorInputTest extends TestCase {
return testDataSource; return testDataSource;
} }
private static FakeDataSource buildLargeDataSource() throws IOException { private static FakeDataSource buildLargeDataSource() throws Exception {
FakeDataSource.Builder builder = new FakeDataSource.Builder(); FakeDataSource.Builder builder = new FakeDataSource.Builder();
builder.appendReadData(new byte[LARGE_TEST_DATA_LENGTH]); builder.appendReadData(new byte[LARGE_TEST_DATA_LENGTH]);
FakeDataSource testDataSource = builder.build(); FakeDataSource testDataSource = builder.build();
...@@ -410,8 +424,9 @@ public class DefaultExtractorInputTest extends TestCase { ...@@ -410,8 +424,9 @@ public class DefaultExtractorInputTest extends TestCase {
return testDataSource; return testDataSource;
} }
private static DefaultExtractorInput createDefaultExtractorInput() throws IOException { private static DefaultExtractorInput createDefaultExtractorInput() throws Exception {
FakeDataSource testDataSource = buildDataSource(); FakeDataSource testDataSource = buildDataSource();
return new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET); return new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET);
} }
} }
...@@ -125,7 +125,6 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -125,7 +125,6 @@ public final class DefaultExtractorInput implements ExtractorInput {
throws IOException, InterruptedException { throws IOException, InterruptedException {
ensureSpaceForPeek(length); ensureSpaceForPeek(length);
int bytesPeeked = Math.min(peekBufferLength - peekBufferPosition, length); int bytesPeeked = Math.min(peekBufferLength - peekBufferPosition, length);
peekBufferLength += length - bytesPeeked;
while (bytesPeeked < length) { while (bytesPeeked < length) {
bytesPeeked = readFromDataSource(peekBuffer, peekBufferPosition, length, bytesPeeked, bytesPeeked = readFromDataSource(peekBuffer, peekBufferPosition, length, bytesPeeked,
allowEndOfInput); allowEndOfInput);
...@@ -134,6 +133,7 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -134,6 +133,7 @@ public final class DefaultExtractorInput implements ExtractorInput {
} }
} }
peekBufferPosition += length; peekBufferPosition += length;
peekBufferLength = Math.max(peekBufferLength, peekBufferPosition);
return true; return true;
} }
......
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