1 /* 2 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <windows.h> 27 28 #include "jni.h" 29 #include "jni_util.h" 30 #include "jlong.h" 31 #include "nio.h" 32 #include "nio_util.h" 33 34 #include "sun_nio_ch_WindowsAsynchronousFileChannelImpl.h" 35 36 37 JNIEXPORT jint JNICALL 38 Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_readFile(JNIEnv* env, jclass this, 39 jlong handle, jlong address, jint len, jlong offset, jlong ov) 40 { 41 BOOL res; 42 43 OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov); 44 lpOverlapped->Offset = (DWORD)offset; 45 lpOverlapped->OffsetHigh = (DWORD)((long)(offset >> 32)); 46 lpOverlapped->hEvent = NULL; 47 48 res = ReadFile((HANDLE) jlong_to_ptr(handle), 49 (LPVOID) jlong_to_ptr(address), 50 (DWORD)len, 51 NULL, 52 lpOverlapped); 53 54 if (res == 0) { 55 int error = GetLastError(); 56 if (error == ERROR_IO_PENDING) 57 return IOS_UNAVAILABLE; 58 if (error == ERROR_HANDLE_EOF) 59 return IOS_EOF; 60 JNU_ThrowIOExceptionWithLastError(env, "ReadFile failed"); 61 return IOS_THROWN; 62 } 63 64 return IOS_UNAVAILABLE; 65 } 66 67 JNIEXPORT jint JNICALL 68 Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_writeFile(JNIEnv* env, jclass this, 69 jlong handle, jlong address, jint len, jlong offset, jlong ov) 70 { 71 BOOL res; 72 73 OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov); 74 lpOverlapped->Offset = (DWORD)offset; 75 lpOverlapped->OffsetHigh = (DWORD)((long)(offset >> 32)); 76 lpOverlapped->hEvent = NULL; 77 78 res = WriteFile((HANDLE)jlong_to_ptr(handle), 79 (LPVOID) jlong_to_ptr(address), 80 (DWORD)len, 81 NULL, 82 lpOverlapped); 83 84 if (res == 0) { 85 int error = GetLastError(); 86 if (error == ERROR_IO_PENDING) 87 return IOS_UNAVAILABLE; 88 JNU_ThrowIOExceptionWithLastError(env, "WriteFile failed"); 89 return IOS_THROWN; 90 } 91 92 return IOS_UNAVAILABLE; 93 } 94 95 JNIEXPORT jint JNICALL 96 Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_lockFile(JNIEnv *env, jobject this, jlong handle, 97 jlong pos, jlong size, jboolean shared, jlong ov) 98 { 99 BOOL res; 100 HANDLE h = jlong_to_ptr(handle); 101 DWORD lowPos = (DWORD)pos; 102 long highPos = (long)(pos >> 32); 103 DWORD lowNumBytes = (DWORD)size; 104 DWORD highNumBytes = (DWORD)(size >> 32); 105 DWORD flags = (shared == JNI_TRUE) ? 0 : LOCKFILE_EXCLUSIVE_LOCK; 106 OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov); 107 108 lpOverlapped->Offset = lowPos; 109 lpOverlapped->OffsetHigh = highPos; 110 lpOverlapped->hEvent = NULL; 111 112 res = LockFileEx(h, flags, 0, lowNumBytes, highNumBytes, lpOverlapped); 113 if (res == 0) { 114 int error = GetLastError(); 115 if (error == ERROR_IO_PENDING) { 116 return IOS_UNAVAILABLE; 117 } 118 JNU_ThrowIOExceptionWithLastError(env, "WriteFile failed"); 119 return IOS_THROWN; 120 } 121 return 0; 122 } 123 124 JNIEXPORT void JNICALL 125 Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_close0(JNIEnv* env, jclass this, 126 jlong handle) 127 { 128 HANDLE h = (HANDLE)jlong_to_ptr(handle); 129 CloseHandle(h); 130 }