src/share/classes/java/io/FileInputStream.java

Print this page

        

@@ -122,11 +122,11 @@
         }
         if (name == null) {
             throw new NullPointerException();
         }
         fd = new FileDescriptor();
-        fd.incrementAndGetUseCount();
+        fd.attach(this);
         open(name);
     }
 
     /**
      * Creates a <code>FileInputStream</code> by using the file descriptor

@@ -162,14 +162,13 @@
         }
         fd = fdObj;
 
         /*
          * FileDescriptor is being shared by streams.
-         * Ensure that it's GC'ed only when all the streams/channels are done
-         * using it.
+         * Register this stream with FileDescriptor tracker.
          */
-        fd.incrementAndGetUseCount();
+        fd.attach(this);
     }
 
     /**
      * Opens the specified file for reading.
      * @param name the name of the file

@@ -292,31 +291,18 @@
                 return;
             }
             closed = true;
         }
         if (channel != null) {
-            /*
-             * Decrement the FD use count associated with the channel
-             * The use count is incremented whenever a new channel
-             * is obtained from this stream.
-             */
-           fd.decrementAndGetUseCount();
            channel.close();
         }
 
-        /*
-         * Decrement the FD use count associated with this stream
-         */
-        int useCount = fd.decrementAndGetUseCount();
-
-        /*
-         * If FileDescriptor is still in use by another stream, we
-         * will not close it.
-         */
-        if (useCount <= 0) {
+        fd.closeAll(new Closeable() {
+            public void close() throws IOException {
             close0();
         }
+        });
     }
 
     /**
      * Returns the <code>FileDescriptor</code>
      * object  that represents the connection to

@@ -326,11 +312,13 @@
      * @return     the file descriptor object associated with this stream.
      * @exception  IOException  if an I/O error occurs.
      * @see        java.io.FileDescriptor
      */
     public final FileDescriptor getFD() throws IOException {
-        if (fd != null) return fd;
+        if (fd != null) {
+            return fd;
+        }
         throw new IOException();
     }
 
     /**
      * Returns the unique {@link java.nio.channels.FileChannel FileChannel}

@@ -350,17 +338,10 @@
      */
     public FileChannel getChannel() {
         synchronized (this) {
             if (channel == null) {
                 channel = FileChannelImpl.open(fd, true, false, this);
-
-                /*
-                 * Increment fd's use count. Invoking the channel's close()
-                 * method will result in decrementing the use count set for
-                 * the channel.
-                 */
-                fd.incrementAndGetUseCount();
             }
             return channel;
         }
     }
 

@@ -379,9 +360,14 @@
      * @exception  IOException  if an I/O error occurs.
      * @see        java.io.FileInputStream#close()
      */
     protected void finalize() throws IOException {
         if ((fd != null) &&  (fd != FileDescriptor.in)) {
+            /* if fd is shared, the references in FileDescriptor
+             * will ensure that finalizer is only called when
+             * safe to do so. All references using the fd have
+             * become unreachable. We can call close()
+             */
                 close();
         }
     }
 }