jdk/src/windows/native/sun/nio/ch/IOUtil.c
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 13024 ada1a7c54e84
child 22631 ac85b05a53f4
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 13024
diff changeset
     2
 * Copyright (c) 2000, 2012, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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 <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <io.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "sun_nio_ch_IOUtil.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/* field id for jlong 'handle' in java.io.FileDescriptor used for file fds */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static jfieldID handle_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* field id for jint 'fd' in java.io.FileDescriptor used for socket fds */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static jfieldID fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
(JNIEnv *env, jclass clazz, jbyteArray randArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * static method to store field IDs in initializers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    clazz = (*env)->FindClass(env, "java/io/FileDescriptor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/**************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * IOUtil.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                  jbyteArray randArray)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                                                    clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                                                    randArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
13024
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 5506
diff changeset
    73
JNIEXPORT jint JNICALL
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 5506
diff changeset
    74
Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this)
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 5506
diff changeset
    75
{
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 5506
diff changeset
    76
    return 16;
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 5506
diff changeset
    77
}
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 5506
diff changeset
    78
ada1a7c54e84 7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents: 5506
diff changeset
    79
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
convertReturnVal(JNIEnv *env, jint n, jboolean reading)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    if (n > 0) /* Number of bytes written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (reading) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return IOS_EOF; /* EOF is -1 in javaland */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    if (n > 0) /* Number of bytes written */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (reading) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return IOS_EOF; /* EOF is -1 in javaland */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    return fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    (*env)->SetIntField(env, fdo, fd_fdID, val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
#define SET_BLOCKING 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
#define SET_NONBLOCKING 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                        jobject fdo, jboolean blocking)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    u_long argp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    int result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    if (blocking == JNI_FALSE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        argp = SET_NONBLOCKING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        argp = SET_BLOCKING;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /* Blocking fd cannot be registered with EventSelect */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        WSAEventSelect(fd, NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    result = ioctlsocket(fd, FIONBIO, &argp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    if (result == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        int error = WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        handleSocketError(env, (jint)error);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
/* Note: Drain uses the int fd value. It is currently not called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
   on windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
JNIEXPORT jboolean JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    int totalRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    BOOL result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    HANDLE h = (HANDLE)_get_osfhandle(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    char buf[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    if (h == INVALID_HANDLE_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        JNU_ThrowIOExceptionWithLastError(env, "Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        result = ReadFile(h,          /* File handle to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                          (LPVOID)&buf,    /* address to put data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                          128,        /* number of bytes to read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                          &read,      /* number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                          NULL);      /* no overlapped struct */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (result == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            int error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if (error == ERROR_NO_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                return (totalRead > 0) ? JNI_TRUE : JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            JNU_ThrowIOExceptionWithLastError(env, "Drain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (read > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            totalRead += read;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    return (totalRead > 0) ? JNI_TRUE : JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
/* Note: This function returns the int fd value from file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
   It is mostly used for sockets which should use the int fd value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
jint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
fdval(JNIEnv *env, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    return (*env)->GetIntField(env, fdo, fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
jlong
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
handleval(JNIEnv *env, jobject fdo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    return (*env)->GetLongField(env, fdo, handle_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}