changeset 47235:9ef10c6e67b8

8187631: Refactor FileDescriptor close implementation Reviewed-by: bpb, alanb
author rriggs
date Thu, 21 Sep 2017 11:41:12 -0400
parents 5ee94c2e087e
children bed98952fb09
files make/mapfiles/libjava/mapfile-vers src/java.base/share/classes/java/io/FileInputStream.java src/java.base/share/classes/java/io/FileOutputStream.java src/java.base/share/classes/java/io/RandomAccessFile.java src/java.base/share/classes/jdk/internal/misc/JavaIOFileDescriptorAccess.java src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java src/java.base/unix/classes/java/io/FileDescriptor.java src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java src/java.base/unix/native/libjava/FileDescriptor_md.c src/java.base/unix/native/libjava/FileInputStream_md.c src/java.base/unix/native/libjava/RandomAccessFile_md.c src/java.base/unix/native/libjava/io_util_md.c src/java.base/unix/native/libjava/io_util_md.h src/java.base/windows/classes/java/io/FileDescriptor.java src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java src/java.base/windows/native/libjava/FileDescriptor_md.c src/java.base/windows/native/libjava/FileInputStream_md.c src/java.base/windows/native/libjava/RandomAccessFile_md.c src/java.base/windows/native/libjava/io_util_md.c src/java.base/windows/native/libjava/io_util_md.h
diffstat 20 files changed, 189 insertions(+), 310 deletions(-) [+]
line wrap: on
line diff
--- a/make/mapfiles/libjava/mapfile-vers	Wed Sep 20 15:22:21 2017 -0700
+++ b/make/mapfiles/libjava/mapfile-vers	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 #
-# Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
+# Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 #
 # This code is free software; you can redistribute it and/or modify it
@@ -74,17 +74,16 @@
 		JNU_ThrowStringIndexOutOfBoundsException;
 		JNU_ToString;
 
+		Java_java_io_FileDescriptor_close;
 		Java_java_io_FileDescriptor_initIDs;
 		Java_java_io_FileDescriptor_sync;
 		Java_java_io_FileDescriptor_getAppend;
 		Java_java_io_FileInputStream_available0;
-		Java_java_io_FileInputStream_close0;
 		Java_java_io_FileInputStream_initIDs;
 		Java_java_io_FileInputStream_open0;
 		Java_java_io_FileInputStream_read0;
 		Java_java_io_FileInputStream_readBytes;
 		Java_java_io_FileInputStream_skip0;
-		Java_java_io_FileOutputStream_close0;
 		Java_java_io_FileOutputStream_initIDs;
 		Java_java_io_FileOutputStream_open0;
 		Java_java_io_FileOutputStream_write;
@@ -95,7 +94,6 @@
 		Java_java_io_ObjectOutputStream_floatsToBytes;
 		Java_java_io_ObjectStreamClass_hasStaticInitializer;
 		Java_java_io_ObjectStreamClass_initNative;
-		Java_java_io_RandomAccessFile_close0;
 		Java_java_io_RandomAccessFile_getFilePointer;
 		Java_java_io_RandomAccessFile_initIDs;
 		Java_java_io_RandomAccessFile_length;
--- a/src/java.base/share/classes/java/io/FileInputStream.java	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/share/classes/java/io/FileInputStream.java	Thu Sep 21 11:41:12 2017 -0400
@@ -341,7 +341,7 @@
 
         fd.closeAll(new Closeable() {
             public void close() throws IOException {
-               close0();
+               fd.close();
            }
         });
     }
@@ -403,7 +403,6 @@
 
     private static native void initIDs();
 
-    private native void close0() throws IOException;
 
     static {
         initIDs();
--- a/src/java.base/share/classes/java/io/FileOutputStream.java	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/share/classes/java/io/FileOutputStream.java	Thu Sep 21 11:41:12 2017 -0400
@@ -365,7 +365,7 @@
 
         fd.closeAll(new Closeable() {
             public void close() throws IOException {
-               close0();
+               fd.close();
            }
         });
     }
@@ -458,8 +458,6 @@
         }
     }
 
-    private native void close0() throws IOException;
-
     private static native void initIDs();
 
     static {
--- a/src/java.base/share/classes/java/io/RandomAccessFile.java	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/share/classes/java/io/RandomAccessFile.java	Thu Sep 21 11:41:12 2017 -0400
@@ -648,7 +648,7 @@
 
         fd.closeAll(new Closeable() {
             public void close() throws IOException {
-               close0();
+               fd.close();
            }
         });
     }
@@ -1177,8 +1177,6 @@
 
     private static native void initIDs();
 
-    private native void close0() throws IOException;
-
     static {
         initIDs();
         SharedSecrets.setJavaIORandomAccessFileAccess(new JavaIORandomAccessFileAccess()
--- a/src/java.base/share/classes/jdk/internal/misc/JavaIOFileDescriptorAccess.java	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/share/classes/jdk/internal/misc/JavaIOFileDescriptorAccess.java	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,12 +31,13 @@
  */
 
 public interface JavaIOFileDescriptorAccess {
-    public void set(FileDescriptor obj, int fd);
-    public int get(FileDescriptor fd);
-    public void setAppend(FileDescriptor obj, boolean append);
-    public boolean getAppend(FileDescriptor obj);
+    public void set(FileDescriptor fdo, int fd);
+    public int get(FileDescriptor fdo);
+    public void setAppend(FileDescriptor fdo, boolean append);
+    public boolean getAppend(FileDescriptor fdo);
+    public void close(FileDescriptor fdo);
 
     // Only valid on Windows
-    public void setHandle(FileDescriptor obj, long handle);
-    public long getHandle(FileDescriptor obj);
+    public void setHandle(FileDescriptor fdo, long handle);
+    public long getHandle(FileDescriptor fdo);
 }
--- a/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -144,7 +144,7 @@
             //
             ((java.io.Closeable)parent).close();
         } else {
-            nd.close(fd);
+            fdAccess.close(fd);
         }
 
     }
--- a/src/java.base/unix/classes/java/io/FileDescriptor.java	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/unix/classes/java/io/FileDescriptor.java	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -33,16 +33,13 @@
 /**
  * Instances of the file descriptor class serve as an opaque handle
  * to the underlying machine-specific structure representing an open
- * file, an open socket, or another source or sink of bytes. The
- * main practical use for a file descriptor is to create a
- * <code>FileInputStream</code> or <code>FileOutputStream</code> to
- * contain it.
+ * file, an open socket, or another source or sink of bytes.
+ * The main practical use for a file descriptor is to create a
+ * {@link FileInputStream} or {@link FileOutputStream} to contain it.
  * <p>
  * Applications should not create their own file descriptors.
  *
  * @author  Pavani Diwanji
- * @see     java.io.FileInputStream
- * @see     java.io.FileOutputStream
  * @since   1.0
  */
 public final class FileDescriptor {
@@ -58,6 +55,45 @@
      */
     private boolean append;
 
+    static {
+        initIDs();
+    }
+
+    // Set up JavaIOFileDescriptorAccess in SharedSecrets
+    static {
+        SharedSecrets.setJavaIOFileDescriptorAccess(
+                new JavaIOFileDescriptorAccess() {
+                    public void set(FileDescriptor fdo, int fd) {
+                        fdo.fd = fd;
+                    }
+
+                    public int get(FileDescriptor fdo) {
+                        return fdo.fd;
+                    }
+
+                    public void setAppend(FileDescriptor fdo, boolean append) {
+                        fdo.append = append;
+                    }
+
+                    public boolean getAppend(FileDescriptor fdo) {
+                        return fdo.append;
+                    }
+
+                    public void close(FileDescriptor fdo) {
+                        fdo.close();
+                    }
+
+                    public void setHandle(FileDescriptor fdo, long handle) {
+                        throw new UnsupportedOperationException();
+                    }
+
+                    public long getHandle(FileDescriptor fdo) {
+                        throw new UnsupportedOperationException();
+                    }
+                }
+        );
+    }
+
     /**
      * Constructs an (invalid) FileDescriptor
      * object.
@@ -74,7 +110,7 @@
     /**
      * A handle to the standard input stream. Usually, this file
      * descriptor is not used directly, but rather via the input stream
-     * known as <code>System.in</code>.
+     * known as {@code System.in}.
      *
      * @see     java.lang.System#in
      */
@@ -83,7 +119,7 @@
     /**
      * A handle to the standard output stream. Usually, this file
      * descriptor is not used directly, but rather via the output stream
-     * known as <code>System.out</code>.
+     * known as {@code System.out}.
      * @see     java.lang.System#out
      */
     public static final FileDescriptor out = new FileDescriptor(1);
@@ -91,7 +127,7 @@
     /**
      * A handle to the standard error stream. Usually, this file
      * descriptor is not used directly, but rather via the output stream
-     * known as <code>System.err</code>.
+     * known as {@code System.err}.
      *
      * @see     java.lang.System#err
      */
@@ -100,9 +136,9 @@
     /**
      * Tests if this file descriptor object is valid.
      *
-     * @return  <code>true</code> if the file descriptor object represents a
+     * @return  {@code true} if the file descriptor object represents a
      *          valid, open file, socket, or other active I/O connection;
-     *          <code>false</code> otherwise.
+     *          {@code false} otherwise.
      */
     public boolean valid() {
         return fd != -1;
@@ -141,46 +177,18 @@
     /* This routine initializes JNI field offsets for the class */
     private static native void initIDs();
 
-    static {
-        initIDs();
-    }
-
-    // Set up JavaIOFileDescriptorAccess in SharedSecrets
-    static {
-        SharedSecrets.setJavaIOFileDescriptorAccess(
-            new JavaIOFileDescriptorAccess() {
-                public void set(FileDescriptor obj, int fd) {
-                    obj.fd = fd;
-                }
-
-                public int get(FileDescriptor obj) {
-                    return obj.fd;
-                }
-
-                public void setAppend(FileDescriptor obj, boolean append) {
-                    obj.append = append;
-                }
-
-                public boolean getAppend(FileDescriptor obj) {
-                    return obj.append;
-                }
-
-                public void setHandle(FileDescriptor obj, long handle) {
-                    throw new UnsupportedOperationException();
-                }
-
-                public long getHandle(FileDescriptor obj) {
-                    throw new UnsupportedOperationException();
-                }
-            }
-        );
-    }
-
     /**
      * Returns true, if the file was opened for appending.
      */
     private static native boolean getAppend(int fd);
 
+    /**
+     * Close the raw file descriptor or handle, if it has not already been closed
+     * and set the fd and handle to -1.
+     * Package private to allow it to be used in java.io.
+     */
+    native void close();
+
     /*
      * Package private methods to track referents.
      * If multiple streams point to the same FileDescriptor, we cycle
--- a/src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/unix/classes/sun/nio/ch/FileDispatcherImpl.java	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,9 @@
 import java.io.FileDescriptor;
 import java.io.IOException;
 
+import jdk.internal.misc.JavaIOFileDescriptorAccess;
+import jdk.internal.misc.SharedSecrets;
+
 class FileDispatcherImpl extends FileDispatcher {
 
     static {
@@ -35,6 +38,9 @@
         init();
     }
 
+    private static final JavaIOFileDescriptorAccess fdAccess =
+            SharedSecrets.getJavaIOFileDescriptorAccess();
+
     FileDispatcherImpl() {
     }
 
@@ -95,7 +101,7 @@
     }
 
     void close(FileDescriptor fd) throws IOException {
-        close0(fd);
+        fdAccess.close(fd);
     }
 
     void preClose(FileDescriptor fd) throws IOException {
@@ -153,6 +159,8 @@
     static native void release0(FileDescriptor fd, long pos, long size)
         throws IOException;
 
+    // Shared with SocketDispatcher and DatagramDispatcher but
+    // NOT used by FileDispatcherImpl
     static native void close0(FileDescriptor fd) throws IOException;
 
     static native void preClose0(FileDescriptor fd) throws IOException;
--- a/src/java.base/unix/native/libjava/FileDescriptor_md.c	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/unix/native/libjava/FileDescriptor_md.c	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -70,3 +70,9 @@
     int flags = fcntl(fd, F_GETFL);
     return ((flags & O_APPEND) == 0) ? JNI_FALSE : JNI_TRUE;
 }
+
+// instance method close0 for FileDescriptor
+JNIEXPORT void JNICALL
+Java_java_io_FileDescriptor_close(JNIEnv *env, jobject this) {
+    fileDescriptorClose(env, this);
+}
--- a/src/java.base/unix/native/libjava/FileInputStream_md.c	Wed Sep 20 15:22:21 2017 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-/*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include "jni.h"
-#include "jni_util.h"
-#include "jvm.h"
-#include "io_util.h"
-#include "io_util_md.h"
-
-#include "java_io_FileInputStream.h"
-
-extern jfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */
-
-/*********************************************************************
- * Platform specific implementation of input stream native methods
- */
-
-JNIEXPORT void JNICALL
-Java_java_io_FileInputStream_close0(JNIEnv *env, jobject this) {
-    fileClose(env, this, fis_fd);
-}
--- a/src/java.base/unix/native/libjava/RandomAccessFile_md.c	Wed Sep 20 15:22:21 2017 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-/*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include "jni.h"
-#include "jni_util.h"
-#include "jvm.h"
-#include "io_util.h"
-#include "io_util_md.h"
-
-#include "java_io_RandomAccessFile.h"
-
-extern jfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */
-
-/*********************************************************************
- * Platform specific implementation of input stream native methods
- */
-
-JNIEXPORT void JNICALL
-Java_java_io_RandomAccessFile_close0(JNIEnv *env, jobject this) {
-    fileClose(env, this, raf_fd);
-}
--- a/src/java.base/unix/native/libjava/io_util_md.c	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/unix/native/libjava/io_util_md.c	Thu Sep 21 11:41:12 2017 -0400
@@ -124,19 +124,31 @@
 void
 fileClose(JNIEnv *env, jobject this, jfieldID fid)
 {
-    FD fd = GET_FD(this, fid);
-    if (fd == -1) {
+    jobject fileDescriptor = (*env)->GetObjectField(env, (this), (fid));
+    if (fileDescriptor == NULL) {
         return;
     }
+    fileDescriptorClose(env, fileDescriptor);
+}
 
+// Function to close the fd held by this FileDescriptor and set fd to -1.
+void
+fileDescriptorClose(JNIEnv *env, jobject this)
+{
+    FD fd = (*env)->GetIntField(env, this, IO_fd_fdID);
+    if ((*env)->ExceptionOccurred(env)) {
+        return;
+    }
     /* Set the fd to -1 before closing it so that the timing window
      * of other threads using the wrong fd (closed but recycled fd,
      * that gets re-opened with some other filename) is reduced.
      * Practically the chance of its occurance is low, however, we are
      * taking extra precaution over here.
      */
-    SET_FD(this, -1, fid);
-
+    (*env)->SetIntField(env, this, IO_fd_fdID, -1);
+    if ((*env)->ExceptionOccurred(env)) {
+        return;
+    }
     /*
      * Don't close file descriptors 0, 1, or 2. If we close these stream
      * then a subsequent file open or socket will use them. Instead we
@@ -145,7 +157,7 @@
     if (fd >= STDIN_FILENO && fd <= STDERR_FILENO) {
         int devnull = open("/dev/null", O_WRONLY);
         if (devnull < 0) {
-            SET_FD(this, fd, fid); // restore fd
+            (*env)->SetIntField(env, this, IO_fd_fdID, fd);
             JNU_ThrowIOExceptionWithLastError(env, "open /dev/null failed");
         } else {
             dup2(devnull, fd);
--- a/src/java.base/unix/native/libjava/io_util_md.h	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/unix/native/libjava/io_util_md.h	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -103,6 +103,7 @@
  * IO helper function(s)
  */
 void fileClose(JNIEnv *env, jobject this, jfieldID fid);
+void fileDescriptorClose(JNIEnv *env, jobject this);
 
 #ifdef MACOSX
 jstring newStringPlatform(JNIEnv *env, const char* str);
--- a/src/java.base/windows/classes/java/io/FileDescriptor.java	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/windows/classes/java/io/FileDescriptor.java	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -32,12 +32,12 @@
 
 /**
  * Instances of the file descriptor class serve as an opaque handle
- * to the underlying machine-specific structure representing an
- * open file, an open socket, or another source or sink of bytes.
+ * to the underlying machine-specific structure representing an open
+ * file, an open socket, or another source or sink of bytes.
  * The main practical use for a file descriptor is to create a
  * {@link FileInputStream} or {@link FileOutputStream} to contain it.
- *
- * <p>Applications should not create their own file descriptors.
+ * <p>
+ * Applications should not create their own file descriptors.
  *
  * @author  Pavani Diwanji
  * @since   1.0
@@ -57,15 +57,6 @@
      */
     private boolean append;
 
-    /**
-     * Constructs an (invalid) FileDescriptor
-     * object.
-     */
-    public /**/ FileDescriptor() {
-        fd = -1;
-        handle = -1;
-    }
-
     static {
         initIDs();
     }
@@ -73,32 +64,45 @@
     // Set up JavaIOFileDescriptorAccess in SharedSecrets
     static {
         SharedSecrets.setJavaIOFileDescriptorAccess(
-            new JavaIOFileDescriptorAccess() {
-                public void set(FileDescriptor obj, int fd) {
-                    obj.fd = fd;
+                new JavaIOFileDescriptorAccess() {
+                    public void set(FileDescriptor fdo, int fd) {
+                        fdo.fd = fd;
+                    }
+
+                    public int get(FileDescriptor fdo) {
+                        return fdo.fd;
+                    }
+
+                    public void setAppend(FileDescriptor fdo, boolean append) {
+                        fdo.append = append;
+                    }
+
+                    public boolean getAppend(FileDescriptor fdo) {
+                        return fdo.append;
+                    }
+
+                    public void close(FileDescriptor fdo) {
+                        fdo.close();
+                    }
+
+                    public void setHandle(FileDescriptor fdo, long handle) {
+                        fdo.handle = handle;
+                    }
+
+                    public long getHandle(FileDescriptor fdo) {
+                        return fdo.handle;
+                    }
                 }
+        );
+    }
 
-                public int get(FileDescriptor obj) {
-                    return obj.fd;
-                }
-
-                public void setAppend(FileDescriptor obj, boolean append) {
-                    obj.append = append;
-                }
-
-                public boolean getAppend(FileDescriptor obj) {
-                    return obj.append;
-                }
-
-                public void setHandle(FileDescriptor obj, long handle) {
-                    obj.handle = handle;
-                }
-
-                public long getHandle(FileDescriptor obj) {
-                    return obj.handle;
-                }
-            }
-        );
+    /**
+     * Constructs an (invalid) FileDescriptor
+     * object.
+     */
+    public FileDescriptor() {
+        fd = -1;
+        handle = -1;
     }
 
     /**
@@ -135,7 +139,7 @@
      *          {@code false} otherwise.
      */
     public boolean valid() {
-        return ((handle != -1) || (fd != -1));
+        return (handle != -1) || (fd != -1);
     }
 
     /**
@@ -179,6 +183,13 @@
         return desc;
     }
 
+    /**
+     * Close the raw file descriptor or handle, if it has not already been closed
+     * and set the fd and handle to -1.
+     * Package private to allow it to be used in java.io.
+     */
+    native void close();
+
     /*
      * Package private methods to track referents.
      * If multiple streams point to the same FileDescriptor, we cycle
--- a/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/windows/classes/sun/nio/ch/FileDispatcherImpl.java	Thu Sep 21 11:41:12 2017 -0400
@@ -104,7 +104,7 @@
     }
 
     void close(FileDescriptor fd) throws IOException {
-        close0(fd);
+        fdAccess.close(fd);
     }
 
     FileDescriptor duplicateForMapping(FileDescriptor fd) throws IOException {
--- a/src/java.base/windows/native/libjava/FileDescriptor_md.c	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/windows/native/libjava/FileDescriptor_md.c	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -72,3 +72,9 @@
         JNU_ThrowByName(env, "java/io/SyncFailedException", "sync failed");
     }
 }
+
+// instance method close0 for FileDescriptor
+JNIEXPORT void JNICALL
+Java_java_io_FileDescriptor_close(JNIEnv *env, jobject this) {
+    fileDescriptorClose(env, this);
+}
--- a/src/java.base/windows/native/libjava/FileInputStream_md.c	Wed Sep 20 15:22:21 2017 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,44 +0,0 @@
-/*
- * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-#include "jni.h"
-#include "jni_util.h"
-#include "jvm.h"
-
-#include "io_util.h"
-#include "io_util_md.h"
-
-#include "java_io_FileInputStream.h"
-
-extern jfieldID fis_fd; /* id for jobject 'fd' in java.io.FileInputStream */
-
-/*********************************************************************
- * Platform specific implementation of input stream native methods
- */
-
-JNIEXPORT void JNICALL
-Java_java_io_FileInputStream_close0(JNIEnv *env, jobject this) {
-    handleClose(env, this, fis_fd);
-}
--- a/src/java.base/windows/native/libjava/RandomAccessFile_md.c	Wed Sep 20 15:22:21 2017 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,44 +0,0 @@
-/*
- * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
- *
- * This code is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License version 2 only, as
- * published by the Free Software Foundation.  Oracle designates this
- * particular file as subject to the "Classpath" exception as provided
- * by Oracle in the LICENSE file that accompanied this code.
- *
- * This code is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
- * version 2 for more details (a copy is included in the LICENSE file that
- * accompanied this code).
- *
- * You should have received a copy of the GNU General Public License version
- * 2 along with this work; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
- * or visit www.oracle.com if you need additional information or have any
- * questions.
- */
-
-#include "jni.h"
-#include "jni_util.h"
-#include "jvm.h"
-
-#include "io_util.h"
-#include "io_util_md.h"
-
-#include "java_io_RandomAccessFile.h"
-
-extern jfieldID raf_fd; /* id for jobject 'fd' in java.io.RandomAccessFile */
-
-/*********************************************************************
- * Platform specific implementation of input stream native methods
- */
-
-JNIEXPORT void JNICALL
-Java_java_io_RandomAccessFile_close0(JNIEnv *env, jobject this) {
-    handleClose(env, this, raf_fd);
-}
--- a/src/java.base/windows/native/libjava/io_util_md.c	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/windows/native/libjava/io_util_md.c	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -535,14 +535,28 @@
     return writeInternal(fd, buf, len, JNI_TRUE);
 }
 
-jint
+void
 handleClose(JNIEnv *env, jobject this, jfieldID fid)
 {
-    FD fd = GET_FD(this, fid);
+    jobject fileDescriptor = (*env)->GetObjectField(env, (this), (fid));
+    if (fileDescriptor == NULL) {
+        return;
+    }
+    fileDescriptorClose(env, fileDescriptor);
+}
+
+// Function to close the fd held by this FileDescriptor and set fd to -1.
+void
+fileDescriptorClose(JNIEnv *env, jobject this)
+{
+    FD fd = (*env)->GetLongField(env, this, IO_handle_fdID);
+    if ((*env)->ExceptionOccurred(env)) {
+        return;
+    }
     HANDLE h = (HANDLE)fd;
 
     if (h == INVALID_HANDLE_VALUE) {
-        return 0;
+        return;
     }
 
     /* Set the fd to -1 before closing it so that the timing window
@@ -551,12 +565,14 @@
      * Practically the chance of its occurance is low, however, we are
      * taking extra precaution over here.
      */
-    SET_FD(this, -1, fid);
+    (*env)->SetLongField(env, this, IO_handle_fdID, -1);
+    if ((*env)->ExceptionOccurred(env)) {
+        return;
+    }
 
     if (CloseHandle(h) == 0) { /* Returns zero on failure */
         JNU_ThrowIOExceptionWithLastError(env, "close failed");
     }
-    return 0;
 }
 
 jlong
--- a/src/java.base/windows/native/libjava/io_util_md.h	Wed Sep 20 15:22:21 2017 -0700
+++ b/src/java.base/windows/native/libjava/io_util_md.h	Thu Sep 21 11:41:12 2017 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -48,7 +48,8 @@
 JNIEXPORT jint handleRead(FD fd, void *buf, jint len);
 jint handleWrite(FD fd, const void *buf, jint len);
 jint handleAppend(FD fd, const void *buf, jint len);
-jint handleClose(JNIEnv *env, jobject this, jfieldID fid);
+void handleClose(JNIEnv *env, jobject this, jfieldID fid);
+void fileDescriptorClose(JNIEnv *env, jobject this);
 jlong handleLseek(FD fd, jlong offset, jint whence);
 
 /*