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

FileDescriptor should respect append flag

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 9
    • 7u7
    • core-libs
    • b38
    • windows_7
    • Verified

    Backports

      Description

        FULL PRODUCT VERSION :
        java version " 1.7.0_07 "
        Java(TM) SE Runtime Environment (build 1.7.0_07-b11)
        Java HotSpot(TM) Client VM (build 23.3-b01, mixed mode, sharing)

        java version " 1.5.0_16 "
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_16-b02)
        Java HotSpot(TM) Client VM (build 1.5.0_16-b02, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [Version 6.1.7601]


        A DESCRIPTION OF THE PROBLEM :
        When you write to a FileWriter object that is instantiated with a FileDescriptor from a FileOutputStream that was created with " append " = true, it is not appending to the end of the file. It is overwriting the previous file content from the beginning of the file. Note that it is not behaving like " append " =false, because it is not wiping out all of the previous content unless the second write is larger than the content of the file.

        In Java 1.3.1 and 1.4.2 it appended. In Java 1.5 and later (1.7) it is overwriting at the beginning of the file.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Code is attached.
        Create a FileOutputStream (fos) with " append " =true
        Get the FileDescriptor (fd) from the fos.
        Create a FileWriter (fw) with the fd.
        Write to the (fw).

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The file should be appended to at the end instead of overwriting at the beginning. This worked in Java 1.3.1 and 1.4.2, but is not working in Java 5, 6, and 7.

        Stdout output should be as follows:
        SUCCESS!
        ACTUAL -
        The file is overwritten with the second write.
        Stdout Output is as follows:
        FAILED! File should increase in size since we are appending. curSize=4; prevSize=4



        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        N/A. Reproducible every time. No exception is thrown.

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        public static void main(String[] args) throws IOException
        {
        /* Setup: Create a file with some data in it. */
        File file1 = File.createTempFile( " test " , null );
        file1.deleteOnExit();
        String testString = " Test " ;
        FileOutputStream fos = new FileOutputStream(file1.getPath(),true);
        fos.write(testString.getBytes());
        fos.flush();
        fos.close();
        long prevSize = file1.length();
        assertTrue( prevSize > 0 );
        /* End Setup */

        FileOutputStream fos2 = new FileOutputStream(file1.getPath(),true);
        FileWriter fw = new FileWriter(fos2.getFD());
        fw.write(testString);
        fw.close();
        long curSize = file1.length();
        //assertTrue( " File should increase in size since we are appending. curSize= " + curSize + " ; prevSize= " + prevSize, curSize > prevSize );
        if ( curSize > prevSize )
        {
        System.out.println( " SUCCESS! " );
        System.exit(0);
        }
        System.out.println( " FAILED! File should increase in size since we are appending. curSize= " + curSize + " ; prevSize= " + prevSize );
        System.exit(1);

        }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        None yet, but I think it will be possible.
        I want to use the FileDescriptor.sync() because the implementation that depended on this was for critical file system writes.

        Attachments

          Issue Links

            Activity

              People

                igerasim Ivan Gerasimov
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: