Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8054029

(fc) FileChannel.size() returns 0 for block devices on Linux

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • 7
    • core-libs
    • b32
    • x86
    • linux
    • Verified

        FULL PRODUCT VERSION :
        java version "1.7.0_03"
        Java(TM) SE Runtime Environment (build 1.7.0_03-b04)
        Java HotSpot(TM) 64-Bit Server VM (build 22.1-b02, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Linux XXX 3.0.0-21-generic #35-Ubuntu SMP Fri May 25 17:57:41 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

        A DESCRIPTION OF THE PROBLEM :
        FileChannel.size() returns 0 for block devices on Linux. As a consequence, it is impossible to memory map a block device, because FileChannel.map() uses size() to determine whether the file is big enough, and in the case of a block device, since size() always returns 0, it always concludes that it isn't big enough, tries to enlarge it, and fails.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        On Linux, compile the sample code and execute it using a command similar to this:

        sudo java -cp . BlockDeviceBug /dev/sda

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        It should show something like this:

        File: /dev/sda
        Size: 500107862016
        Actual size: 500107862016
        ACTUAL -
        It actually says:

        File: /dev/sda
        Size: 0
        Actual size: 500107862016

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        public class BlockDeviceBug {
            public static void main(String[] args) throws IOException {
                try (RandomAccessFile file = new RandomAccessFile(args[0], "r")) {
                    FileChannel channel = file.getChannel();
                    System.out.println("File: " + args[0]);
                    System.out.println("Size according to FileChannel: " + channel.size());
                    System.out.println("Actual size: " + file.length());
                }
            }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        No workaround found.

              igerasim Ivan Gerasimov
              coffeys Sean Coffey
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported: