JDK-5056395 : nio does not seem to work with Linux kernel 2.6.4 (and probably above)
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 1.4.2,1.4.2_04,1.4.2_07,1.4.2_21
  • Priority: P1
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux,linux_suse_sles_8
  • CPU: x86
  • Submitted: 2004-06-02
  • Updated: 2004-11-05
  • Resolved: 2004-06-10
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
Description
Apparently nio does not really like to work here:-(  I tested this on SuSE 9.1pro (with stock kernel) but we had problem reports from a Mandrake 10 user (with 2.6.* kernel too). 

Thanks,
-- Marco


marco@linux:/tmp> cat FileCopyTest.java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileCopyTest {

    public static void main(String[] args) {
        File a = new File("/tmp/a");
        File b = new File("/tmp/b");

        copyFile(a, b);
    } // end of main()

    public static void copyFile(File source, File dest){
        try {
            FileChannel in = new FileInputStream(source).getChannel();
            if(!dest.getParentFile().exists()) dest.getParentFile().mkdirs();
            FileChannel out = new FileOutputStream(dest).getChannel();
            in.transferTo(0, in.size(), out);
            in.close();
            out.close();
        }
        catch ( IOException e ) {
            e.printStackTrace();
        }
    }
}
marco@linux:/tmp> cp -p ~/snapshot1.png /tmp/a
marco@linux:/tmp> java FileCopyTest
java.io.IOException: Invalid argument
        at sun.nio.ch.FileChannelImpl.transferTo0(Native Method)
        at sun.nio.ch.FileChannelImpl.transferToDirectly(FileChannelImpl.java:392)
        at sun.nio.ch.FileChannelImpl.transferTo(FileChannelImpl.java:487)
        at FileCopyTest.copyFile(FileCopyTest.java:21)
        at FileCopyTest.main(FileCopyTest.java:13)
marco@linux:/tmp> type java
java is hashed (/usr/lib/java/bin/java)
marco@linux:/tmp> java -version
java version "1.4.2_03"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_03-b02)
Java HotSpot(TM) Client VM (build 1.4.2_03-b02, mixed mode)
marco@linux:/tmp> ~/Creator/java/bin/java -version
java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)
marco@linux:/tmp> ~/Creator/java/bin/java FileCopyTest
java.io.IOException: Invalid argument
        at sun.nio.ch.FileChannelImpl.transferTo0(Native Method)
        at sun.nio.ch.FileChannelImpl.transferToDirectly(FileChannelImpl.java:392)
        at sun.nio.ch.FileChannelImpl.transferTo(FileChannelImpl.java:487)
        at FileCopyTest.copyFile(FileCopyTest.java:21)
        at FileCopyTest.main(FileCopyTest.java:13)
marco@linux:/tmp> uname -a
Linux linux 2.6.4-54.5-bigsmp #1 SMP Fri May 7 21:54:56 UTC 2004 i686 i686 i386 GNU/Linux
marco@linux:/tmp> cat /etc/SuSE-release
SuSE Linux 9.1 (i586)
VERSION = 9.1

###@###.### 2004-06-02

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: tiger-rc FIXED IN: tiger-rc INTEGRATED IN: tiger-b56 tiger-rc
19-09-2004

EVALUATION The same mechanism used to detect if transfers on pipes is supported has been extended to try and detect if file to file transfer is supported by sendfile. When sendfile does not support it then the file is mapped and copied over by more conventional means. ###@###.### 2004-06-07
07-06-2004

WORK AROUND You could try to find all the places which use nio and replace them with the original io mechanisms. But I don't think that's really a workaround. ###@###.### 2004-06-02
02-06-2004