Issue:FileChannel.force() on channel object from a FileInputStream object.
Issue in testcases in FileChannel submodule in java_nio/channels/ module during JCK testing
The following test cases in api/java_nio/channels/FileChannel/index.html#Methods fail in our propreitary unix based on SVR4, with the following error.
FileChannel0031: Failed. Unexpected exception java.io.IOException: Bad file number
FileChannel0037: Failed. Unexpected exception java.io.IOException: Bad file number
FileChannel0040: Failed. Unexpected exception java.io.IOException: Bad file number
FileChannel0043: Failed. Unexpected exception java.io.IOException: Bad file number
FileChannel0044: Failed. Unexpected exception java.io.IOException: Bad file number
The stack trace printed is as follows.
java.io.IOException: Bad file number
at sun.nio.ch.FileChannelImpl.force0(Native Method)
at sun.nio.ch.FileChannelImpl.force(FileChannelImpl.java:359)
at javasoft.sqe.tests.api.java.nio.channels.FileChannel.MethodsTests$30.run(MethodsTests.java:662)
at javasoft.sqe.jck.lib.SecurityTestRunner.runTestWithTCKSM(SecurityTestRunner.java:278)
at javasoft.sqe.jck.lib.SecurityTestRunner.runTestWithPermissions(SecurityTestRunner.java:235)
at javasoft.sqe.jck.lib.SecurityTestRunner.runTestWithAllPermissions(SecurityTestRunner.java:157)
at javasoft.sqe.jck.lib.AllPermissionSM.testRun(AllPermissionSM.java:86)
at javasoft.sqe.jck.lib.AllPermissionSM.testRun(AllPermissionSM.java:133)
at javasoft.sqe.tests.api.java.nio.channels.FileChannel.MethodsTests.FileChannel0031(MethodsTests.java:682)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at javasoft.sqe.javatest.lib.MultiTest.invokeTestCase(MultiTest.java:399)
at javasoft.sqe.javatest.lib.MultiTest.run(MultiTest.java:195)
at javasoft.sqe.javatest.lib.MultiTest.run(MultiTest.java:127)
at javasoft.sqe.tests.api.java.nio.channels.FileChannel.MethodsTests.main(MethodsTests.java:59)
Analysis on the test cases revealed that force() method is invoked on the channel obtained from a FileInputStream object in the above test cases and this causes an exception to be thrown.
The force() method is implemented internally by making use of fsync() function. The man page of fsync() in Linux clearly says that
fsync fails if one or more of the following are true:
EBADF fildes is not a valid file descriptor open for writing.
The FileInputStream object will always open the file in read only mode and hence the file descriptor will not be a valid descriptor for writing. Even then in Linux the fsync() will succeed on a file descriptor valid only for reading.
In our proprietary unix based on SVR4, the man page specifies the same condition and fsync() fails if the file descriptor is not valid for writing and hence an exception is thrown.
The FileInputStream object can be used only for reading raw bytes from a file. A channel obtained via the getChannel method of a FileInputStream instance will be open for reading. Hence logically also it doesn抰 make sense to do a force() on a FileChannel object obtained from FileInputStream.

