TMI-107: Better fix, with test case on stream level. JPEGSegmentImageInputStream now recovers from EOFException while parsing segments.

This commit is contained in:
Harald Kuhr
2015-03-09 12:49:40 +01:00
parent 2b2dd2fb40
commit 032b2d9601
2 changed files with 37 additions and 3 deletions

View File

@@ -153,4 +153,29 @@ public class JPEGSegmentImageInputStreamTest {
assertEquals(1061L, length); // Sanity check: same as file size, except padding and the filtered ICC_PROFILE segment
}
@Test
public void testEOFExceptionInSegmentParsingShouldNotCreateBadState() throws IOException {
ImageInputStream iis = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(getClassLoaderResource("/broken-jpeg/broken-no-sof-ascii-transfer-mode.jpg")));
int fileLength = 2021;
byte[] buffer = new byte[4096];
// NOTE: This is a simulation of how the native parts of com.sun...JPEGImageReader would read the image...
assertEquals(fileLength, iis.read(buffer, 0, buffer.length));
assertEquals(fileLength, iis.getStreamPosition());
iis.seek(0x2012); // bad segment length, should have been 0x0012, not 0x2012
assertEquals(0x2012, iis.getStreamPosition());
// So far, so good (but stream position is now really beyond EOF)...
// This however, will blow up with an EOFException internally (but we'll return -1 to be good)
assertEquals(-1, iis.read(buffer, 0, buffer.length));
assertEquals(0x2012, iis.getStreamPosition());
// Again, should just continue returning -1 for ever
assertEquals(-1, iis.read(buffer, 0, buffer.length));
assertEquals(0x2012, iis.getStreamPosition());
}
}