jdk/src/windows/native/sun/nio/ch/WindowsAsynchronousFileChannelImpl.c
changeset 2057 3acf8e5e2ca0
child 4668 53e1c92056fc
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2008-2009 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any 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     DWORD nread = 0;
       
    43 
       
    44     OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov);
       
    45     lpOverlapped->Offset = (DWORD)offset;
       
    46     lpOverlapped->OffsetHigh = (DWORD)((long)(offset >> 32));
       
    47     lpOverlapped->hEvent = NULL;
       
    48 
       
    49     res = ReadFile((HANDLE) jlong_to_ptr(handle),
       
    50                    (LPVOID) jlong_to_ptr(address),
       
    51                    (DWORD)len,
       
    52                    &nread,
       
    53                    lpOverlapped);
       
    54 
       
    55     if (res == 0) {
       
    56         int error = GetLastError();
       
    57         if (error == ERROR_IO_PENDING)
       
    58             return IOS_UNAVAILABLE;
       
    59         if (error == ERROR_HANDLE_EOF)
       
    60             return IOS_EOF;
       
    61         JNU_ThrowIOExceptionWithLastError(env, "ReadFile failed");
       
    62         return IOS_THROWN;
       
    63     }
       
    64 
       
    65     return (jint)nread;
       
    66 }
       
    67 
       
    68 JNIEXPORT jint JNICALL
       
    69 Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_writeFile(JNIEnv* env, jclass this,
       
    70     jlong handle, jlong address, jint len, jlong offset, jlong ov)
       
    71 {
       
    72     BOOL res;
       
    73     DWORD nwritten = 0;
       
    74 
       
    75     OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov);
       
    76     lpOverlapped->Offset = (DWORD)offset;
       
    77     lpOverlapped->OffsetHigh = (DWORD)((long)(offset >> 32));
       
    78     lpOverlapped->hEvent = NULL;
       
    79 
       
    80     res = WriteFile((HANDLE)jlong_to_ptr(handle),
       
    81                    (LPVOID) jlong_to_ptr(address),
       
    82                    (DWORD)len,
       
    83                    &nwritten,
       
    84                    lpOverlapped);
       
    85 
       
    86     if (res == 0) {
       
    87         int error = GetLastError();
       
    88         if (error == ERROR_IO_PENDING) {
       
    89             return IOS_UNAVAILABLE;
       
    90         }
       
    91         JNU_ThrowIOExceptionWithLastError(env, "WriteFile failed");
       
    92         return IOS_THROWN;
       
    93     }
       
    94     return (jint)nwritten;
       
    95 }
       
    96 
       
    97 JNIEXPORT jint JNICALL
       
    98 Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_lockFile(JNIEnv *env, jobject this, jlong handle,
       
    99                                                             jlong pos, jlong size, jboolean shared, jlong ov)
       
   100 {
       
   101     BOOL res;
       
   102     HANDLE h = jlong_to_ptr(handle);
       
   103     DWORD lowPos = (DWORD)pos;
       
   104     long highPos = (long)(pos >> 32);
       
   105     DWORD lowNumBytes = (DWORD)size;
       
   106     DWORD highNumBytes = (DWORD)(size >> 32);
       
   107     DWORD flags = (shared == JNI_TRUE) ? 0 : LOCKFILE_EXCLUSIVE_LOCK;
       
   108     OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov);
       
   109 
       
   110     lpOverlapped->Offset = lowPos;
       
   111     lpOverlapped->OffsetHigh = highPos;
       
   112     lpOverlapped->hEvent = NULL;
       
   113 
       
   114     res = LockFileEx(h, flags, 0, lowNumBytes, highNumBytes, lpOverlapped);
       
   115     if (res == 0) {
       
   116         int error = GetLastError();
       
   117         if (error == ERROR_IO_PENDING) {
       
   118             return IOS_UNAVAILABLE;
       
   119         }
       
   120         JNU_ThrowIOExceptionWithLastError(env, "WriteFile failed");
       
   121         return IOS_THROWN;
       
   122     }
       
   123     return 0;
       
   124 }
       
   125 
       
   126 JNIEXPORT void JNICALL
       
   127 Java_sun_nio_ch_WindowsAsynchronousFileChannelImpl_close0(JNIEnv* env, jclass this,
       
   128     jlong handle)
       
   129 {
       
   130     HANDLE h = (HANDLE)jlong_to_ptr(handle);
       
   131     CloseHandle(h);
       
   132 }