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

(fc) FileChannel.transferTo throws IOException when position equals size

    XMLWordPrintable

Details

    • 20
    • b10
    • generic
    • os_x, windows

    Backports

      Description

        ADDITIONAL SYSTEM INFORMATION :
        JRE 21.0.2+13

        A DESCRIPTION OF THE PROBLEM :
        The Javadoc states, "If the given position is greater than or equal to the file's current size then no bytes are transferred." The below exception happens when the position equals the current size.

        IOException: Channel not open for writing - cannot extend file to required size
        at java.base/sun.nio.ch.FileChannelImpl.mapInternal(FileChannelImpl.java:1282)
        at java.base/sun.nio.ch.FileChannelImpl.map(FileChannelImpl.java:1183)
        at java.base/sun.nio.ch.FileChannelImpl.transferToTrustedChannel(FileChannelImpl.java:676)
        at java.base/sun.nio.ch.FileChannelImpl.transferTo(FileChannelImpl.java:782)

        REGRESSION : Last worked in version 17.0.10

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1. Create a file of any length
        2. Attempt to copy bytes from that file to another file using FileChannel transferTo where:
        * position is equal to the size of the original file
        * count is greater than MAPPED_TRANSFER_SIZE of 16384

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        No exception. No bytes are transferred as stated in the Javadoc.
        ACTUAL -
        IOException: Channel not open for writing - cannot extend file to required size

        ---------- BEGIN SOURCE ----------
        @Test
        public void test() throws Exception {
        final Path parent = tempFolder.toPath();
        final File original = parent.resolve("original").toFile();
        try (PrintWriter out = new PrintWriter(original)) {
        out.println("test");
        }
        final File copy = parent.resolve("copy").toFile();
        try (FileInputStream in = new FileInputStream(original);
        FileOutputStream out = new FileOutputStream(copy);
        FileChannel inChannel = in.getChannel();
        FileChannel outChannel = out.getChannel()) {
        inChannel.transferTo(original.length(), 16385, outChannel);
        }
        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Avoid calling FileChannel transferTo with a position equal to the size of the channel. The call is unnecessary but breaks code that is implemented in accordance to the Javadoc.

        FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

                bpb Brian Burkhalter
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                6 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: