jdk/src/windows/native/sun/nio/ch/FileDispatcherImpl.c
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7515 43202796198e
child 25806 7a0cf527248e
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7515
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2594
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2594
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2594
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2594
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2594
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jlong.h"
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    31
#include "sun_nio_ch_FileDispatcherImpl.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "nio_util.h"
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
    35
#include "jlong.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**************************************************************
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    39
 * FileDispatcherImpl.c
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    43
Java_sun_nio_ch_FileDispatcherImpl_read0(JNIEnv *env, jclass clazz, jobject fdo,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                                      jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    result = ReadFile(h,          /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                      (LPVOID)address,    /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                      len,        /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                      &read,      /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                      NULL);      /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            return IOS_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (error == ERROR_NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        JNU_ThrowIOExceptionWithLastError(env, "Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    return convertReturnVal(env, (jint)read, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
JNIEXPORT jlong JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    74
Java_sun_nio_ch_FileDispatcherImpl_readv0(JNIEnv *env, jclass clazz, jobject fdo,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                       jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    jlong totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    LPVOID loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    DWORD num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    struct iovec *iovecp = (struct iovec *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    for(i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        loc = (LPVOID)jlong_to_ptr(iovecp[i].iov_base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        num = iovecp[i].iov_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        result = ReadFile(h,                /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                          loc,              /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                          num,              /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                          &read,            /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                          NULL);            /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (read > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            totalRead += read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (read < num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return IOS_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (error == ERROR_NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        JNU_ThrowIOExceptionWithLastError(env, "Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    return convertLongReturnVal(env, totalRead, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   123
Java_sun_nio_ch_FileDispatcherImpl_pread0(JNIEnv *env, jclass clazz, jobject fdo,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                            jlong address, jint len, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    DWORD lowPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    long highPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    DWORD lowOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    long highOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    lowOffset = (DWORD)offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    highOffset = (DWORD)(offset >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    lowOffset = SetFilePointer(h, lowOffset, &highOffset, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    if (lowOffset == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    result = ReadFile(h,                /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                      (LPVOID)address,  /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                      len,              /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                      &read,            /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                      NULL);              /* struct with offset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            return IOS_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (error == ERROR_NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        JNU_ThrowIOExceptionWithLastError(env, "Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    return convertReturnVal(env, (jint)read, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   186
Java_sun_nio_ch_FileDispatcherImpl_write0(JNIEnv *env, jclass clazz, jobject fdo,
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   187
                                          jlong address, jint len, jboolean append)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    if (h != INVALID_HANDLE_VALUE) {
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   194
        OVERLAPPED ov;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   195
        LPOVERLAPPED lpOv;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   196
        if (append == JNI_TRUE) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   197
            ov.Offset = (DWORD)0xFFFFFFFF;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   198
            ov.OffsetHigh = (DWORD)0xFFFFFFFF;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   199
            ov.hEvent = NULL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   200
            lpOv = &ov;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   201
        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   202
            lpOv = NULL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   203
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        result = WriteFile(h,           /* File handle to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                      (LPCVOID)address, /* pointers to the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                      len,              /* number of bytes to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                      &written,         /* receives number of bytes written */
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   208
                      lpOv);            /* overlapped struct */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        JNU_ThrowIOExceptionWithLastError(env, "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    return convertReturnVal(env, (jint)written, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
JNIEXPORT jlong JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   219
Java_sun_nio_ch_FileDispatcherImpl_writev0(JNIEnv *env, jclass clazz, jobject fdo,
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   220
                                           jlong address, jint len, jboolean append)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    jlong totalWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        LPVOID loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        DWORD num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        struct iovec *iovecp = (struct iovec *)jlong_to_ptr(address);
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   232
        OVERLAPPED ov;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   233
        LPOVERLAPPED lpOv;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   234
        if (append == JNI_TRUE) {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   235
            ov.Offset = (DWORD)0xFFFFFFFF;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   236
            ov.OffsetHigh = (DWORD)0xFFFFFFFF;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   237
            ov.hEvent = NULL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   238
            lpOv = &ov;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   239
        } else {
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   240
            lpOv = NULL;
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   241
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        for(i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            loc = (LPVOID)jlong_to_ptr(iovecp[i].iov_base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            num = iovecp[i].iov_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            result = WriteFile(h,       /* File handle to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                               loc,     /* pointers to the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                               num,     /* number of bytes to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                               &written,/* receives number of bytes written */
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   249
                               lpOv);   /* overlapped struct */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (written > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                totalWritten += written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (written < num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        JNU_ThrowIOExceptionWithLastError(env, "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    return convertLongReturnVal(env, totalWritten, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
JNIEXPORT jint JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   267
Java_sun_nio_ch_FileDispatcherImpl_pwrite0(JNIEnv *env, jclass clazz, jobject fdo,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                            jlong address, jint len, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    DWORD lowPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    long highPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    DWORD lowOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    long highOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    lowOffset = (DWORD)offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    highOffset = (DWORD)(offset >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    lowOffset = SetFilePointer(h, lowOffset, &highOffset, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    if (lowOffset == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    result = WriteFile(h,               /* File handle to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                      (LPCVOID)address, /* pointers to the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                      len,              /* number of bytes to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                      &written,         /* receives number of bytes written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                      NULL);            /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        JNU_ThrowIOExceptionWithLastError(env, "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    return convertReturnVal(env, (jint)written, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   318
JNIEXPORT jint JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   319
Java_sun_nio_ch_FileDispatcherImpl_force0(JNIEnv *env, jobject this,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   320
                                          jobject fdo, jboolean md)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   321
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   322
    int result = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   323
    HANDLE h = (HANDLE)(handleval(env, fdo));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   324
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   325
    if (h != INVALID_HANDLE_VALUE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   326
        result = FlushFileBuffers(h);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   327
        if (result == 0) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   328
            int error = GetLastError();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   329
            if (error != ERROR_ACCESS_DENIED) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   330
                JNU_ThrowIOExceptionWithLastError(env, "Force failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   331
                return IOS_THROWN;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   332
            }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   333
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   334
    } else {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   335
        JNU_ThrowIOExceptionWithLastError(env, "Force failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   336
        return IOS_THROWN;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   337
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   338
    return 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   339
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   340
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   341
JNIEXPORT jint JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   342
Java_sun_nio_ch_FileDispatcherImpl_truncate0(JNIEnv *env, jobject this,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   343
                                             jobject fdo, jlong size)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   344
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   345
    DWORD lowPos = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   346
    long highPos = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   347
    BOOL result = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   348
    HANDLE h = (HANDLE)(handleval(env, fdo));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   349
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   350
    lowPos = (DWORD)size;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   351
    highPos = (long)(size >> 32);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   352
    lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   353
    if (lowPos == ((DWORD)-1)) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   354
        if (GetLastError() != ERROR_SUCCESS) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   355
            JNU_ThrowIOExceptionWithLastError(env, "Truncation failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   356
            return IOS_THROWN;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   357
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   358
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   359
    result = SetEndOfFile(h);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   360
    if (result == 0) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   361
        JNU_ThrowIOExceptionWithLastError(env, "Truncation failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   362
        return IOS_THROWN;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   363
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   364
    return 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   365
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   366
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   367
JNIEXPORT jlong JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   368
Java_sun_nio_ch_FileDispatcherImpl_size0(JNIEnv *env, jobject this, jobject fdo)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   369
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   370
    DWORD sizeLow = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   371
    DWORD sizeHigh = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   372
    HANDLE h = (HANDLE)(handleval(env, fdo));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   373
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   374
    sizeLow = GetFileSize(h, &sizeHigh);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   375
    if (sizeLow == ((DWORD)-1)) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   376
        if (GetLastError() != ERROR_SUCCESS) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   377
            JNU_ThrowIOExceptionWithLastError(env, "Size failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   378
            return IOS_THROWN;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   379
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   380
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   381
    return (((jlong)sizeHigh) << 32) | sizeLow;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   382
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   383
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   384
JNIEXPORT jint JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   385
Java_sun_nio_ch_FileDispatcherImpl_lock0(JNIEnv *env, jobject this, jobject fdo,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   386
                                      jboolean block, jlong pos, jlong size,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   387
                                      jboolean shared)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   388
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   389
    HANDLE h = (HANDLE)(handleval(env, fdo));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   390
    DWORD lowPos = (DWORD)pos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   391
    long highPos = (long)(pos >> 32);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   392
    DWORD lowNumBytes = (DWORD)size;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   393
    DWORD highNumBytes = (DWORD)(size >> 32);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   394
    BOOL result;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   395
    DWORD flags = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   396
    OVERLAPPED o;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   397
    o.hEvent = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   398
    o.Offset = lowPos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   399
    o.OffsetHigh = highPos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   400
    if (block == JNI_FALSE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   401
        flags |= LOCKFILE_FAIL_IMMEDIATELY;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   402
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   403
    if (shared == JNI_FALSE) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   404
        flags |= LOCKFILE_EXCLUSIVE_LOCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   405
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   406
    result = LockFileEx(h, flags, 0, lowNumBytes, highNumBytes, &o);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   407
    if (result == 0) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   408
        int error = GetLastError();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   409
        if (error != ERROR_LOCK_VIOLATION) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   410
            JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   411
            return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   412
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   413
        if (flags & LOCKFILE_FAIL_IMMEDIATELY) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   414
            return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   415
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   416
        JNU_ThrowIOExceptionWithLastError(env, "Lock failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   417
        return sun_nio_ch_FileDispatcherImpl_NO_LOCK;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   418
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   419
    return sun_nio_ch_FileDispatcherImpl_LOCKED;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   420
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   421
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   422
JNIEXPORT void JNICALL
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   423
Java_sun_nio_ch_FileDispatcherImpl_release0(JNIEnv *env, jobject this,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   424
                                        jobject fdo, jlong pos, jlong size)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   425
{
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   426
    HANDLE h = (HANDLE)(handleval(env, fdo));
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   427
    DWORD lowPos = (DWORD)pos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   428
    long highPos = (long)(pos >> 32);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   429
    DWORD lowNumBytes = (DWORD)size;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   430
    DWORD highNumBytes = (DWORD)(size >> 32);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   431
    jint result = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   432
    OVERLAPPED o;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   433
    o.hEvent = 0;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   434
    o.Offset = lowPos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   435
    o.OffsetHigh = highPos;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   436
    result = UnlockFileEx(h, 0, lowNumBytes, highNumBytes, &o);
2594
3755ecdb395d 6543863: (fc) FileLock.release can deadlock with FileChannel.close
alanb
parents: 2057
diff changeset
   437
    if (result == 0 && GetLastError() != ERROR_NOT_LOCKED) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   438
        JNU_ThrowIOExceptionWithLastError(env, "Release failed");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   439
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   440
}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   441
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
static void closeFile(JNIEnv *env, jlong fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        int result = CloseHandle(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (result < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            JNU_ThrowIOExceptionWithLastError(env, "Close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
JNIEXPORT void JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   452
Java_sun_nio_ch_FileDispatcherImpl_close0(JNIEnv *env, jclass clazz, jobject fdo)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    jlong fd = handleval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    closeFile(env, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
JNIEXPORT void JNICALL
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   459
Java_sun_nio_ch_FileDispatcherImpl_closeByHandle(JNIEnv *env, jclass clazz,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                             jlong fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    closeFile(env, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
}
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   464
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   465
JNIEXPORT jlong JNICALL
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   466
Java_sun_nio_ch_FileDispatcherImpl_duplicateHandle(JNIEnv *env, jclass this, jlong handle)
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   467
{
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   468
    HANDLE hProcess = GetCurrentProcess();
7515
43202796198e 6709457: (fc) lock/tryLock() throws IOException "Access is denied" when file opened for append [win]
alanb
parents: 7025
diff changeset
   469
    HANDLE hFile = jlong_to_ptr(handle);
7025
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   470
    HANDLE hResult;
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   471
    BOOL res = DuplicateHandle(hProcess, hFile, hProcess, &hResult, 0, FALSE,
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   472
                               DUPLICATE_SAME_ACCESS);
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   473
    if (res == 0)
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   474
       JNU_ThrowIOExceptionWithLastError(env, "DuplicateHandle failed");
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   475
    return ptr_to_jlong(hResult);
6e002f9a2899 6816049: (bf) MappedByteBuffer.force() method does not flush data correctly
alanb
parents: 5506
diff changeset
   476
}