commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gary D. Gregory (Commented) (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (IO-302) ArrayIndexOutOfBoundsException in BOMInputStream when reading a file without BOM multiple times
Date Mon, 13 Feb 2012 18:13:00 GMT

    [ https://issues.apache.org/jira/browse/IO-302?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13207028#comment-13207028
] 

Gary D. Gregory commented on IO-302:
------------------------------------

Could post this as a unit test patch file with a license grant to Apache (this can be done
by selecting the check-box when you attach a file). Supplying the test files will help too.
Thank you!
                
> 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

        

Mime
View raw message