Index: /trunk/openjdk/jdk/src/share/native/java/io/FileInputStream.c
===================================================================
--- /trunk/openjdk/jdk/src/share/native/java/io/FileInputStream.c	(revision 372)
+++ /trunk/openjdk/jdk/src/share/native/java/io/FileInputStream.c	(revision 373)
@@ -78,5 +78,5 @@
     jlong end = jlong_zero;
     FD fd = GET_FD(this, fis_fd);
-    if (fd == -1) {
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException (env, "Stream Closed");
         return 0;
@@ -94,5 +94,5 @@
     jlong ret;
     FD fd = GET_FD(this, fis_fd);
-    if (fd == -1) {
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException (env, "Stream Closed");
         return 0;
Index: /trunk/openjdk/jdk/src/share/native/java/io/RandomAccessFile.c
===================================================================
--- /trunk/openjdk/jdk/src/share/native/java/io/RandomAccessFile.c	(revision 372)
+++ /trunk/openjdk/jdk/src/share/native/java/io/RandomAccessFile.c	(revision 373)
@@ -91,6 +91,6 @@
     jlong ret;
 
-    fd = GET_FD(this, raf_fd);
-    if (fd == -1) {
+    ASSIGN_FD(fd, this, raf_fd);
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException(env, "Stream Closed");
         return -1;
@@ -108,6 +108,6 @@
     jlong end = jlong_zero;
 
-    fd = GET_FD(this, raf_fd);
-    if (fd == -1) {
+    ASSIGN_FD(fd, this, raf_fd);
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException(env, "Stream Closed");
         return -1;
@@ -129,6 +129,6 @@
     FD fd;
 
-    fd = GET_FD(this, raf_fd);
-    if (fd == -1) {
+    ASSIGN_FD(fd, this, raf_fd);
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException(env, "Stream Closed");
         return;
@@ -148,6 +148,6 @@
     jlong cur;
 
-    fd = GET_FD(this, raf_fd);
-    if (fd == -1) {
+    ASSIGN_FD(fd, this, raf_fd);
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException(env, "Stream Closed");
         return;
Index: /trunk/openjdk/jdk/src/share/native/java/io/io_util.c
===================================================================
--- /trunk/openjdk/jdk/src/share/native/java/io/io_util.c	(revision 372)
+++ /trunk/openjdk/jdk/src/share/native/java/io/io_util.c	(revision 373)
@@ -41,5 +41,5 @@
     char ret;
     FD fd = GET_FD(this, fid);
-    if (fd == -1) {
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException(env, "Stream Closed");
         return -1;
@@ -104,6 +104,6 @@
     }
 
-    fd = GET_FD(this, fid);
-    if (fd == -1) {
+    ASSIGN_FD(fd, this, fid);
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException(env, "Stream Closed");
         nread = -1;
@@ -133,5 +133,5 @@
     jint n;
     FD fd = GET_FD(this, fid);
-    if (fd == -1) {
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException(env, "Stream Closed");
         return;
@@ -181,6 +181,6 @@
         off = 0;
         while (len > 0) {
-            fd = GET_FD(this, fid);
-            if (fd == -1) {
+            ASSIGN_FD(fd, this, fid);
+            if (!VALID_FD(fd)) {
                 JNU_ThrowIOException(env, "Stream Closed");
                 break;
Index: /trunk/openjdk/jdk/src/solaris/native/java/io/io_util_md.h
===================================================================
--- /trunk/openjdk/jdk/src/solaris/native/java/io/io_util_md.h	(revision 372)
+++ /trunk/openjdk/jdk/src/solaris/native/java/io/io_util_md.h	(revision 373)
@@ -48,4 +48,12 @@
         -1 : (*env)->GetIntField(env, (*env)->GetObjectField(env, (this), (fid)), IO_fd_fdID)
 
+#define ASSIGN_FD(fd, this, fid) \
+    do { \
+        fd = (*env)->GetObjectField(env, (this), (fid)) == NULL ? \
+            -1 : (*env)->GetIntField(env, (*env)->GetObjectField(env, (this), (fid)), IO_fd_fdID); \
+    } while(0)
+
+#define VALID_FD(fd) (fd != -1)
+
 /*
  * Macros to set/get fd when inside java.io.FileDescriptor
@@ -66,5 +74,5 @@
  * On Solaris, the handle field is unused
  */
-#define SET_HANDLE(fd) return (jlong)-1
+#define RETURN_HANDLE(fd) return (jlong)-1
 
 /*
Index: /trunk/openjdk/jdk/src/windows/native/java/io/FileDescriptor_md.c
===================================================================
--- /trunk/openjdk/jdk/src/windows/native/java/io/FileDescriptor_md.c	(revision 372)
+++ /trunk/openjdk/jdk/src/windows/native/java/io/FileDescriptor_md.c	(revision 373)
@@ -55,5 +55,5 @@
 JNIEXPORT jlong JNICALL
 Java_java_io_FileDescriptor_set(JNIEnv *env, jclass fdClass, jint fd) {
-    SET_HANDLE(fd);
+    RETURN_HANDLE(fd);
 }
 
Index: /trunk/openjdk/jdk/src/windows/native/java/io/FileOutputStream_md.c
===================================================================
--- /trunk/openjdk/jdk/src/windows/native/java/io/FileOutputStream_md.c	(revision 372)
+++ /trunk/openjdk/jdk/src/windows/native/java/io/FileOutputStream_md.c	(revision 373)
@@ -71,5 +71,5 @@
     jboolean append = (*env)->GetBooleanField(env, this, fos_append);
     FD fd = GET_FD(this, fos_fd);
-    if (fd == -1) {
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException(env, "Stream Closed");
         return;
@@ -88,5 +88,5 @@
     jboolean append = (*env)->GetBooleanField(env, this, fos_append);
     FD fd = GET_FD(this, fos_fd);
-    if (fd == -1) {
+    if (!VALID_FD(fd)) {
         JNU_ThrowIOException(env, "Stream Closed");
         return;
Index: /trunk/openjdk/jdk/src/windows/native/java/io/io_util_md.c
===================================================================
--- /trunk/openjdk/jdk/src/windows/native/java/io/io_util_md.c	(revision 372)
+++ /trunk/openjdk/jdk/src/windows/native/java/io/io_util_md.c	(revision 373)
@@ -533,5 +533,5 @@
 {
     FD fd = GET_FD(this, fid);
-    HANDLE h = (HANDLE)fd;
+    HANDLE h = (HANDLE)fd.handle;
 
     if (h == INVALID_HANDLE_VALUE) {
@@ -548,5 +548,5 @@
 
     if (CloseHandle(h) == 0) { /* Returns zero on failure */
-        SET_FD(this, fd, fid); // restore fd
+        SET_FD(this, fd.handle, fid); // restore fd
         JNU_ThrowIOExceptionWithLastError(env, "close failed");
     }
Index: /trunk/openjdk/jdk/src/windows/native/java/io/io_util_md.h
===================================================================
--- /trunk/openjdk/jdk/src/windows/native/java/io/io_util_md.h	(revision 372)
+++ /trunk/openjdk/jdk/src/windows/native/java/io/io_util_md.h	(revision 373)
@@ -54,5 +54,11 @@
  * Macros to use the right data type for file descriptors
  */
-#define FD jlong
+typedef struct __FD
+{
+    jint fd;
+    jlong handle;
+} _FD;
+
+#define FD _FD
 
 /*
@@ -61,4 +67,5 @@
  * will simply return -1 to avoid crashing VM.
  */
+
 #define SET_FD(this, fd, fid) \
     if ((*env)->GetObjectField(env, (this), (fid)) != NULL) \
@@ -66,21 +73,33 @@
 
 #define GET_FD(this, fid) \
-    ((*env)->GetObjectField(env, (this), (fid)) == NULL) ? \
-      -1 : (*env)->GetLongField(env, (*env)->GetObjectField(env, (this), (fid)), IO_handle_fdID)
+    { ((*env)->GetObjectField(env, (this), (fid)) == NULL ? \
+      -1 : (*env)->GetIntField(env, (*env)->GetObjectField(env, (this), (fid)), IO_fd_fdID)), \
+      ((*env)->GetObjectField(env, (this), (fid)) == NULL ? \
+        -1 : (*env)->GetLongField(env, (*env)->GetObjectField(env, (this), (fid)), IO_handle_fdID)) }
+
+#define ASSIGN_FD(fd, this, fid) \
+    do { \
+        fd.fd = ((*env)->GetObjectField(env, (this), (fid)) == NULL ? \
+            -1 : (*env)->GetIntField(env, (*env)->GetObjectField(env, (this), (fid)), IO_fd_fdID)); \
+        fd.handle = ((*env)->GetObjectField(env, (this), (fid)) == NULL ? \
+            -1 : (*env)->GetLongField(env, (*env)->GetObjectField(env, (this), (fid)), IO_handle_fdID)); \
+    } while(0)
+
+#define VALID_FD(fd) (fd.handle != -1 || fd.fd != -1)
 
 /*
  * Macros to set/get fd when inside java.io.FileDescriptor
  */
-#define THIS_FD(obj) (*env)->GetLongField(env, obj, IO_handle_fdID)
+#define THIS_FD(obj) { (*env)->GetLongField(env, obj, IO_fd_fdID), (*env)->GetLongField(env, obj, IO_handle_fdID) }
 
 /*
  * Route the routines away from HPI layer
  */
-#define IO_Write handleWrite
-#define IO_Sync handleSync
-#define IO_Read handleRead
-#define IO_Lseek handleLseek
-#define IO_Available handleAvailable
-#define IO_SetLength handleSetLength
+#define IO_Write(fd,buf,len) (fd.handle != -1 ? handleWrite(fd.handle, buf, len) : JVM_Write(fd.fd, buf, len))
+#define IO_Sync(fd) (fd.handle != -1 ? handleSync(fd.handle) : JVM_Sync(fd.fd))
+#define IO_Read(fd,buf,len) (fd.handle != -1 ? handleRead(fd.handle, buf, len) : JVM_Read(fd.fd, buf, len))
+#define IO_Lseek(fd,offset,whence) (fd.handle != -1 ? handleLseek(fd.handle, offset, whence) : JVM_Lseek(fd.fd, offset, whence))
+#define IO_Available(fd,pbytes) (fd.handle != -1 ? handleAvailable(fd.handle, pbytes) : JVM_Available(fd.fd, pbytes))
+#define IO_SetLength(fd, length) (fd.handle != -1 ? handleSetLength(fd.handle, length) : JVM_SetLength(fd.fd, length))
 
 /*
@@ -88,5 +107,5 @@
  * standard handles stdIn, stdOut, stdErr
  */
-#define SET_HANDLE(fd) \
+#define RETURN_HANDLE(fd) \
 if (fd == 0) { \
     return (jlong)GetStdHandle(STD_INPUT_HANDLE); \
