Created
March 26, 2011 01:09
-
-
Save DHager/887928 to your computer and use it in GitHub Desktop.
FileChannel bug in Sun's code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.technofovea.packbsp; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.nio.channels.FileChannel; | |
/** | |
* This quick test shows a bug in the SUN/Oracle FileChannel class. | |
* | |
* Instead of throwing an IOException as per the documentation, a java.lang.Error | |
* is thrown. | |
* | |
* @author Darien Hager | |
*/ | |
public class ErrorCase { | |
public static void main(String[] ars) throws Exception { | |
try { | |
//File file = new File("c:/faceprov.log"); | |
File file = File.createTempFile("errtest", ".tmp"); | |
file.deleteOnExit(); | |
FileInputStream in = new FileInputStream(file); | |
in.close(); //Oops, closing the channel too early early... | |
FileChannel fc = in.getChannel(); | |
// OK, here comes the part that throws an exception... | |
fc.tryLock(0, Long.MAX_VALUE, true); | |
} catch (IOException ioex) { | |
System.out.println("This is where we expected to catch an IOException from our bug."); | |
ioex.printStackTrace(); | |
} catch(Exception ex){ | |
System.out.println("OK, maybe we didn't expect some other kind of exception..."); | |
ex.printStackTrace(); | |
} catch(Error e){ | |
System.out.println("WTF! A completely unexpected class was thrown: "+e.getClass()); | |
System.out.println("This should *BE* an IOException, not something *CONTAINING* an IOException."); | |
e.printStackTrace(); | |
} | |
/* Expected output | |
WTF! A completely unexpected class was thrown: class java.lang.Error | |
This should *BE* an IOException, not something *CONTAINING* an IOException. | |
java.lang.Error: java.io.IOException: The handle is invalid | |
at sun.nio.ch.FileKey.create(FileKey.java:27) | |
at sun.nio.ch.FileChannelImpl$SharedFileLockTable.<init>(FileChannelImpl.java:1041) | |
at sun.nio.ch.FileChannelImpl.fileLockTable(FileChannelImpl.java:816) | |
at sun.nio.ch.FileChannelImpl.tryLock(FileChannelImpl.java:877) | |
at com.technofovea.packbsp.ErrorCase.main(ErrorCase.java:40) | |
Caused by: java.io.IOException: The handle is invalid | |
at sun.nio.ch.FileKey.init(Native Method) | |
at sun.nio.ch.FileKey.create(FileKey.java:25) | |
... 4 more | |
*/ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment