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

File{In/Out}putStream close always called twice

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs
    • None
    • generic
    • generic

      In FileInputstream & FileOutputstream finalize() calls close() if FileDescriptor is null.
      But when close is called it usually sets FD to -1 (i.e. invalid)
      And hence finalize will always call close() regardless of whether it was called already.

      Perhaps finalise() should also check for FD being invalid as well as checking if null?

      Here's some example code to show ...

      // CloseTest.java

      import java.io.*;

      public class CloseTest extends FileOutputStream {

      String m_name;

      public CloseTest(String name) throws FileNotFoundException {
      super(name);
      m_name = name;
      }

      public void close() throws IOException {
      System.out.println("CloseTest#close() - " + m_name + " called ..");
      super.close();
      }


      public static void doTest(String name) throws Exception {
      CloseTest test = new CloseTest(name);

      test.close();
      for (int i=0; i < 10; i++) {
      byte dummy[] = new byte[20*1024];
      }
      }

      public static void main(String args[]) throws Exception {
      for (int i=0; i < 10; i++) {
      CloseTest.doTest("test" + i);
      }
      }
      }
      //////////////////////////////////////////////////////////////////////////

      This bug shows up in all j2se but not jdk 1.1.x since in that version FD was
      set to null when Stream closed.
      ###@###.### 2003-01-16

            mmcclosksunw Michael Mccloskey (Inactive)
            fmaccumh Fionn Maccumhaill (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: