jdk/src/windows/native/sun/nio/ch/FileDispatcher.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2003 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "sun_nio_ch_FileDispatcher.h"
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"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * FileDispatcher.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
Java_sun_nio_ch_FileDispatcher_read0(JNIEnv *env, jclass clazz, jobject fdo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                                      jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    result = ReadFile(h,          /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                      (LPVOID)address,    /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                      len,        /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                      &read,      /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                      NULL);      /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            return IOS_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (error == ERROR_NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        JNU_ThrowIOExceptionWithLastError(env, "Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    return convertReturnVal(env, (jint)read, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
Java_sun_nio_ch_FileDispatcher_readv0(JNIEnv *env, jclass clazz, jobject fdo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                       jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    jlong totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    LPVOID loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    DWORD num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    struct iovec *iovecp = (struct iovec *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    for(i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        loc = (LPVOID)jlong_to_ptr(iovecp[i].iov_base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        num = iovecp[i].iov_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        result = ReadFile(h,                /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                          loc,              /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                          num,              /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                          &read,            /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                          NULL);            /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (read > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            totalRead += read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (read < num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return IOS_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (error == ERROR_NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        JNU_ThrowIOExceptionWithLastError(env, "Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    return convertLongReturnVal(env, totalRead, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
Java_sun_nio_ch_FileDispatcher_pread0(JNIEnv *env, jclass clazz, jobject fdo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                            jlong address, jint len, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    DWORD lowPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    long highPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    DWORD lowOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    long highOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        JNU_ThrowIOExceptionWithLastError(env, "Invalid handle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    lowOffset = (DWORD)offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    highOffset = (DWORD)(offset >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    lowOffset = SetFilePointer(h, lowOffset, &highOffset, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    if (lowOffset == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    result = ReadFile(h,                /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                      (LPVOID)address,  /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                      len,              /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                      &read,            /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                      NULL);              /* struct with offset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (error == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return IOS_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (error == ERROR_NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        JNU_ThrowIOExceptionWithLastError(env, "Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    return convertReturnVal(env, (jint)read, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
Java_sun_nio_ch_FileDispatcher_write0(JNIEnv *env, jclass clazz, jobject fdo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                       jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        result = WriteFile(h,           /* File handle to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                      (LPCVOID)address, /* pointers to the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                      len,              /* number of bytes to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                      &written,         /* receives number of bytes written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                      NULL);            /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        JNU_ThrowIOExceptionWithLastError(env, "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    return convertReturnVal(env, (jint)written, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
Java_sun_nio_ch_FileDispatcher_writev0(JNIEnv *env, jclass clazz, jobject fdo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                       jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    jlong totalWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        LPVOID loc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        DWORD num = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        struct iovec *iovecp = (struct iovec *)jlong_to_ptr(address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        for(i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            loc = (LPVOID)jlong_to_ptr(iovecp[i].iov_base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            num = iovecp[i].iov_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            result = WriteFile(h,       /* File handle to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                               loc,     /* pointers to the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                               num,     /* number of bytes to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                               &written,/* receives number of bytes written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                               NULL);   /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (written > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                totalWritten += written;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (written < num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        JNU_ThrowIOExceptionWithLastError(env, "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    return convertLongReturnVal(env, totalWritten, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
Java_sun_nio_ch_FileDispatcher_pwrite0(JNIEnv *env, jclass clazz, jobject fdo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                            jlong address, jint len, jlong offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    HANDLE h = (HANDLE)(handleval(env, fdo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    DWORD lowPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    long highPos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    DWORD lowOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    long highOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    lowPos = SetFilePointer(h, 0, &highPos, FILE_CURRENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    lowOffset = (DWORD)offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    highOffset = (DWORD)(offset >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    lowOffset = SetFilePointer(h, lowOffset, &highOffset, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    if (lowOffset == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    result = WriteFile(h,               /* File handle to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                      (LPCVOID)address, /* pointers to the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                      len,              /* number of bytes to write */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                      &written,         /* receives number of bytes written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                      NULL);            /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    if ((h == INVALID_HANDLE_VALUE) || (result == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        JNU_ThrowIOExceptionWithLastError(env, "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    lowPos = SetFilePointer(h, lowPos, &highPos, FILE_BEGIN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    if (lowPos == ((DWORD)-1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        if (GetLastError() != ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            JNU_ThrowIOExceptionWithLastError(env, "Seek failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    return convertReturnVal(env, (jint)written, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
static void closeFile(JNIEnv *env, jlong fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    HANDLE h = (HANDLE)fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    if (h != INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int result = CloseHandle(h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        if (result < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            JNU_ThrowIOExceptionWithLastError(env, "Close failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
Java_sun_nio_ch_FileDispatcher_close0(JNIEnv *env, jclass clazz, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    jlong fd = handleval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    closeFile(env, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
Java_sun_nio_ch_FileDispatcher_closeByHandle(JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                             jlong fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    closeFile(env, fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
}