ArrayIndexOutOfBoundsException in BOMInputStream when reading a file without
BOM multiple times
-----------------------------------------------------------------------------------------------
Key: IO-302
URL: https://issues.apache.org/jira/browse/IO-302
Project: Commons IO
Issue Type: Bug
Components: Streams/Writers
Affects Versions: 2.1
Environment: Win7 64bit, Java 6 32bit
Reporter: Jan Steuerwald
Resetting the BOMInputStream doesn't reset the _fbLength_ member variable. This
causes _fbLength_ to grow bigger than the _firstBytes_ array (when the file
doesn't contain a BOM), which leads to an ArrayIndexOutOfBoundsException in the
_readFirstBytes_ method.
The following test code reveals the problem:
{code:title=Test.java}
import java.io.InputStream;
import org.apache.commons.io.input.BOMInputStream;
public class Test {
public static void main(String[] args) {
try {
// read the file with BOM twice - works
readFile("testfileWithBOM.xml");
// read the file without BOM twice - crashes
readFile("testfileWithoutBOM.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
private static void readFile(String testFile) throws Exception {
InputStream inputStream =
Test.class.getClassLoader().getResourceAsStream(testFile);
BOMInputStream bomStream = new BOMInputStream(inputStream);
bomStream.mark(1000000);
// read for the first time => ok
int bytes = 0;
byte[] bytesFromStream = new byte[100];
do {
bytes = bomStream.read(bytesFromStream);
} while (bytes > 0);
// reset and read a second time => crashes when file has no BOM
bomStream.reset();
do {
bytes = bomStream.read(bytesFromStream);
} while (bytes > 0);
}
}
{code}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira