8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win]
authorigerasim
Thu, 25 Sep 2014 00:19:11 +0400
changeset 26732 9b27273e6131
parent 26731 abc6b733e3ab
child 26733 bb835fd801b6
8058099: (fc) Cleanup in FileChannel/FileDispatcher native implementation [win] Reviewed-by: alanb
jdk/src/java.base/windows/native/libnio/ch/FileChannelImpl.c
jdk/src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c
--- a/jdk/src/java.base/windows/native/libnio/ch/FileChannelImpl.c	Wed Sep 24 09:43:10 2014 -0700
+++ b/jdk/src/java.base/windows/native/libnio/ch/FileChannelImpl.c	Thu Sep 25 00:19:11 2014 +0400
@@ -140,24 +140,25 @@
 Java_sun_nio_ch_FileChannelImpl_position0(JNIEnv *env, jobject this,
                                           jobject fdo, jlong offset)
 {
-    DWORD lowPos = 0;
-    long highPos = 0;
+    BOOL result = 0;
     HANDLE h = (HANDLE)(handleval(env, fdo));
+    LARGE_INTEGER where;
+    DWORD whence;
 
     if (offset < 0) {
-        lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
+        where.QuadPart = 0;
+        whence = FILE_CURRENT;
     } else {
-        lowPos = (DWORD)offset;
-        highPos = (long)(offset >> 32);
-        lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
+        where.QuadPart = offset;
+        whence = FILE_BEGIN;
     }
-    if (lowPos == ((DWORD)-1)) {
-        if (GetLastError() != ERROR_SUCCESS) {
-            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
-            return IOS_THROWN;
-        }
+
+    result = SetFilePointerEx(h, where, &where, whence);
+    if (result == 0) {
+        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
+        return IOS_THROWN;
     }
-    return (((jlong)highPos) << 32) | lowPos;
+    return (jlong)where.QuadPart;
 }
 
 JNIEXPORT void JNICALL
--- a/jdk/src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c	Wed Sep 24 09:43:10 2014 -0700
+++ b/jdk/src/java.base/windows/native/libnio/ch/FileDispatcherImpl.c	Thu Sep 25 00:19:11 2014 +0400
@@ -126,39 +126,30 @@
     DWORD read = 0;
     BOOL result = 0;
     HANDLE h = (HANDLE)(handleval(env, fdo));
-    DWORD lowPos = 0;
-    long highPos = 0;
-    DWORD lowOffset = 0;
-    long highOffset = 0;
+    LARGE_INTEGER currPos;
+    OVERLAPPED ov;
 
     if (h == INVALID_HANDLE_VALUE) {
         JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
         return IOS_THROWN;
     }
 
-    lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
-    if (lowPos == ((DWORD)-1)) {
-        if (GetLastError() != ERROR_SUCCESS) {
-            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
-            return IOS_THROWN;
-        }
+    currPos.QuadPart = 0;
+    result = SetFilePointerEx(h, currPos, &currPos, FILE_CURRENT);
+    if (result == 0) {
+        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
+        return IOS_THROWN;
     }
 
-    lowOffset = (DWORD)offset;
-    highOffset = (DWORD)(offset >> 32);
-    lowOffset = SetFilePointer(h, lowOffset, &highOffset, FILE_BEGIN);
-    if (lowOffset == ((DWORD)-1)) {
-        if (GetLastError() != ERROR_SUCCESS) {
-            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
-            return IOS_THROWN;
-        }
-    }
+    ZeroMemory(&ov, sizeof(ov));
+    ov.Offset = (DWORD)offset;
+    ov.OffsetHigh = (DWORD)(offset >> 32);
 
     result = ReadFile(h,                /* File handle to read */
                       (LPVOID)address,  /* address to put data */
                       len,              /* number of bytes to read */
                       &read,            /* number of bytes read */
-                      NULL);              /* struct with offset */
+                      &ov);             /* position to read from */
 
     if (result == 0) {
         int error = GetLastError();
@@ -168,17 +159,18 @@
         if (error == ERROR_NO_DATA) {
             return IOS_UNAVAILABLE;
         }
-        JNU_ThrowIOExceptionWithLastError(env, "Read failed");
+        if (error != ERROR_HANDLE_EOF) {
+            JNU_ThrowIOExceptionWithLastError(env, "Read failed");
+            return IOS_THROWN;
+        }
+    }
+
+    result = SetFilePointerEx(h, currPos, NULL, FILE_BEGIN);
+    if (result == 0) {
+        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
         return IOS_THROWN;
     }
 
-    lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
-    if (lowPos == ((DWORD)-1)) {
-        if (GetLastError() != ERROR_SUCCESS) {
-            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
-            return IOS_THROWN;
-        }
-    }
     return convertReturnVal(env, (jint)read, JNI_TRUE);
 }
 
@@ -194,18 +186,18 @@
         OVERLAPPED ov;
         LPOVERLAPPED lpOv;
         if (append == JNI_TRUE) {
+            ZeroMemory(&ov, sizeof(ov));
             ov.Offset = (DWORD)0xFFFFFFFF;
             ov.OffsetHigh = (DWORD)0xFFFFFFFF;
-            ov.hEvent = NULL;
             lpOv = &ov;
         } else {
             lpOv = NULL;
         }
-        result = WriteFile(h,           /* File handle to write */
-                      (LPCVOID)address, /* pointers to the buffers */
-                      len,              /* number of bytes to write */
-                      &written,         /* receives number of bytes written */
-                      lpOv);            /* overlapped struct */
+        result = WriteFile(h,                /* File handle to write */
+                           (LPCVOID)address, /* pointer to the buffer */
+                           len,              /* number of bytes to write */
+                           &written,         /* receives number of bytes written */
+                           lpOv);            /* overlapped struct */
     }
 
     if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
@@ -232,9 +224,9 @@
         OVERLAPPED ov;
         LPOVERLAPPED lpOv;
         if (append == JNI_TRUE) {
+            ZeroMemory(&ov, sizeof(ov));
             ov.Offset = (DWORD)0xFFFFFFFF;
             ov.OffsetHigh = (DWORD)0xFFFFFFFF;
-            ov.hEvent = NULL;
             lpOv = &ov;
         } else {
             lpOv = NULL;
@@ -270,46 +262,35 @@
     BOOL result = 0;
     DWORD written = 0;
     HANDLE h = (HANDLE)(handleval(env, fdo));
-    DWORD lowPos = 0;
-    long highPos = 0;
-    DWORD lowOffset = 0;
-    long highOffset = 0;
+    LARGE_INTEGER currPos;
+    OVERLAPPED ov;
 
-    lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
-    if (lowPos == ((DWORD)-1)) {
-        if (GetLastError() != ERROR_SUCCESS) {
-            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
-            return IOS_THROWN;
-        }
+    currPos.QuadPart = 0;
+    result = SetFilePointerEx(h, currPos, &currPos, FILE_CURRENT);
+    if (result == 0) {
+        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
+        return IOS_THROWN;
     }
 
-    lowOffset = (DWORD)offset;
-    highOffset = (DWORD)(offset >> 32);
-    lowOffset = SetFilePointer(h, lowOffset, &highOffset, FILE_BEGIN);
-    if (lowOffset == ((DWORD)-1)) {
-        if (GetLastError() != ERROR_SUCCESS) {
-            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
-            return IOS_THROWN;
-        }
-    }
+    ZeroMemory(&ov, sizeof(ov));
+    ov.Offset = (DWORD)offset;
+    ov.OffsetHigh = (DWORD)(offset >> 32);
 
-    result = WriteFile(h,               /* File handle to write */
-                      (LPCVOID)address, /* pointers to the buffers */
-                      len,              /* number of bytes to write */
-                      &written,         /* receives number of bytes written */
-                      NULL);            /* no overlapped struct */
+    result = WriteFile(h,                /* File handle to write */
+                       (LPCVOID)address, /* pointer to the buffer */
+                       len,              /* number of bytes to write */
+                       &written,         /* receives number of bytes written */
+                       &ov);             /* position to write at */
 
     if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
         JNU_ThrowIOExceptionWithLastError(env, "Write failed");
         return IOS_THROWN;
     }
 
-    lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
-    if (lowPos == ((DWORD)-1)) {
-        if (GetLastError() != ERROR_SUCCESS) {
-            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
-            return IOS_THROWN;
-        }
+    result = SetFilePointerEx(h, currPos, NULL, FILE_BEGIN);
+    if (result == 0) {
+        JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
+        return IOS_THROWN;
     }
 
     return convertReturnVal(env, (jint)written, JNI_FALSE);
@@ -342,20 +323,17 @@
 Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
                                              jobject fdo, jlong size)
 {
-    DWORD lowPos = 0;
-    long highPos = 0;
     BOOL result = 0;
     HANDLE h = (HANDLE)(handleval(env, fdo));
+    LARGE_INTEGER offset;
 
-    lowPos = (DWORD)size;
-    highPos = (long)(size >> 32);
-    lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
-    if (lowPos == ((DWORD)-1)) {
-        if (GetLastError() != ERROR_SUCCESS) {
-            JNU_ThrowIOExceptionWithLastError(env, "Truncation failed");
-            return IOS_THROWN;
-        }
+    offset.QuadPart = size;
+    result = SetFilePointerEx(h, offset, NULL, FILE_BEGIN);
+    if (result == 0) {
+        JNU_ThrowIOExceptionWithLastError(env, "Truncation failed");
+        return IOS_THROWN;
     }
+
     result = SetEndOfFile(h);
     if (result == 0) {
         JNU_ThrowIOExceptionWithLastError(env, "Truncation failed");
@@ -367,18 +345,16 @@
 JNIEXPORT jlong JNICALL
 Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
 {
-    DWORD sizeLow = 0;
-    DWORD sizeHigh = 0;
+    BOOL result = 0;
     HANDLE h = (HANDLE)(handleval(env, fdo));
+    LARGE_INTEGER size;
 
-    sizeLow = GetFileSize(h, &sizeHigh);
-    if (sizeLow == ((DWORD)-1)) {
-        if (GetLastError() != ERROR_SUCCESS) {
-            JNU_ThrowIOExceptionWithLastError(env, "Size failed");
-            return IOS_THROWN;
-        }
+    result = GetFileSizeEx(h, &size);
+    if (result == 0) {
+        JNU_ThrowIOExceptionWithLastError(env, "Size failed");
+        return IOS_THROWN;
     }
-    return (((jlong)sizeHigh) << 32) | sizeLow;
+    return (jlong)size.QuadPart;
 }
 
 JNIEXPORT jint JNICALL
@@ -407,7 +383,7 @@
     if (result == 0) {
         int error = GetLastError();
         if (error == ERROR_IO_PENDING) {
-            LPDWORD dwBytes;
+            DWORD dwBytes;
             result = GetOverlappedResult(h, &o, &dwBytes, TRUE);
             if (result != 0) {
                 return sun_nio_ch_FileDispatcherImpl_LOCKED;
@@ -442,8 +418,19 @@
     o.Offset = lowPos;
     o.OffsetHigh = highPos;
     result = UnlockFileEx(h, 0, lowNumBytes, highNumBytes, &o);
-    if (result == 0 && GetLastError() != ERROR_NOT_LOCKED) {
-        JNU_ThrowIOExceptionWithLastError(env, "Release failed");
+    if (result == 0) {
+        int error = GetLastError();
+        if (error == ERROR_IO_PENDING) {
+            DWORD dwBytes;
+            result = GetOverlappedResult(h, &o, &dwBytes, TRUE);
+            if (result == 0) {
+                return;
+            }
+            error = GetLastError();
+        }
+        if (error != ERROR_NOT_LOCKED) {
+            JNU_ThrowIOExceptionWithLastError(env, "Release failed");
+        }
     }
 }
 
@@ -464,8 +451,7 @@
 }
 
 JNIEXPORT void JNICALL
-Java_sun_nio_ch_FileDispatcherImpl_closeByHandle(JNIEnv *env, jclass clazz,
-                                             jlong fd)
+Java_sun_nio_ch_FileDispatcherImpl_closeByHandle(JNIEnv *env, jclass clazz, jlong fd)
 {
     closeFile(env, fd);
 }