jdk/src/windows/native/sun/nio/ch/IOUtil.c
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2000-2003 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 #include <windows.h>
       
    27 #include <winsock2.h>
       
    28 #include <io.h>
       
    29 #include "jni.h"
       
    30 #include "jni_util.h"
       
    31 #include "jvm.h"
       
    32 #include "jlong.h"
       
    33 
       
    34 #include "nio.h"
       
    35 #include "nio_util.h"
       
    36 #include "sun_nio_ch_IOUtil.h"
       
    37 
       
    38 /* field id for jlong 'handle' in java.io.FileDescriptor used for file fds */
       
    39 static jfieldID handle_fdID;
       
    40 
       
    41 /* field id for jint 'fd' in java.io.FileDescriptor used for socket fds */
       
    42 static jfieldID fd_fdID;
       
    43 
       
    44 /* false for 95/98/ME, true for NT/W2K */
       
    45 static jboolean onNT = JNI_FALSE;
       
    46 
       
    47 JNIEXPORT jboolean JNICALL
       
    48 Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed
       
    49 (JNIEnv *env, jclass clazz, jbyteArray randArray);
       
    50 
       
    51 /**************************************************************
       
    52  * static method to store field IDs in initializers
       
    53  */
       
    54 
       
    55 JNIEXPORT void JNICALL
       
    56 Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)
       
    57 {
       
    58     OSVERSIONINFO ver;
       
    59     ver.dwOSVersionInfoSize = sizeof(ver);
       
    60     GetVersionEx(&ver);
       
    61     if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
       
    62         onNT = JNI_TRUE;
       
    63     }
       
    64 
       
    65     clazz = (*env)->FindClass(env, "java/io/FileDescriptor");
       
    66     fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I");
       
    67     handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J");
       
    68 }
       
    69 
       
    70 /**************************************************************
       
    71  * IOUtil.c
       
    72  */
       
    73 JNIEXPORT jboolean JNICALL
       
    74 Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz,
       
    75                                   jbyteArray randArray)
       
    76 {
       
    77     return
       
    78         Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed(env,
       
    79                                                                     clazz,
       
    80                                                                     randArray);
       
    81 }
       
    82 
       
    83 jint
       
    84 convertReturnVal(JNIEnv *env, jint n, jboolean reading)
       
    85 {
       
    86     if (n > 0) /* Number of bytes written */
       
    87         return n;
       
    88     if (n == 0) {
       
    89         if (reading) {
       
    90             return IOS_EOF; /* EOF is -1 in javaland */
       
    91         } else {
       
    92             return 0;
       
    93         }
       
    94     }
       
    95     JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");
       
    96     return IOS_THROWN;
       
    97 }
       
    98 
       
    99 jlong
       
   100 convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)
       
   101 {
       
   102     if (n > 0) /* Number of bytes written */
       
   103         return n;
       
   104     if (n == 0) {
       
   105         if (reading) {
       
   106             return IOS_EOF; /* EOF is -1 in javaland */
       
   107         } else {
       
   108             return 0;
       
   109         }
       
   110     }
       
   111     JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");
       
   112     return IOS_THROWN;
       
   113 }
       
   114 
       
   115 JNIEXPORT jint JNICALL
       
   116 Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo)
       
   117 {
       
   118     return fdval(env, fdo);
       
   119 }
       
   120 
       
   121 JNIEXPORT void JNICALL
       
   122 Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val)
       
   123 {
       
   124     (*env)->SetIntField(env, fdo, fd_fdID, val);
       
   125 }
       
   126 
       
   127 
       
   128 #define SET_BLOCKING 0
       
   129 #define SET_NONBLOCKING 1
       
   130 
       
   131 JNIEXPORT void JNICALL
       
   132 Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
       
   133                                         jobject fdo, jboolean blocking)
       
   134 {
       
   135     u_long argp;
       
   136     int result = 0;
       
   137     jint fd = fdval(env, fdo);
       
   138 
       
   139     if (blocking == JNI_FALSE) {
       
   140         argp = SET_NONBLOCKING;
       
   141     } else {
       
   142         argp = SET_BLOCKING;
       
   143         /* Blocking fd cannot be registered with EventSelect */
       
   144         WSAEventSelect(fd, NULL, 0);
       
   145     }
       
   146     result = ioctlsocket(fd, FIONBIO, &argp);
       
   147     if (result == SOCKET_ERROR) {
       
   148         int error = WSAGetLastError();
       
   149         handleSocketError(env, (jint)error);
       
   150     }
       
   151 }
       
   152 
       
   153 /* Note: Drain uses the int fd value. It is currently not called
       
   154    on windows.
       
   155 */
       
   156 JNIEXPORT jboolean JNICALL
       
   157 Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)
       
   158 {
       
   159     DWORD read = 0;
       
   160     int totalRead = 0;
       
   161     BOOL result = 0;
       
   162     HANDLE h = (HANDLE)_get_osfhandle(fd);
       
   163     char buf[128];
       
   164 
       
   165     if (h == INVALID_HANDLE_VALUE) {
       
   166         JNU_ThrowIOExceptionWithLastError(env, "Read failed");
       
   167         return JNI_FALSE;
       
   168     }
       
   169 
       
   170     for (;;) {
       
   171         result = ReadFile(h,          /* File handle to read */
       
   172                           (LPVOID)&buf,    /* address to put data */
       
   173                           128,        /* number of bytes to read */
       
   174                           &read,      /* number of bytes read */
       
   175                           NULL);      /* no overlapped struct */
       
   176 
       
   177         if (result == 0) {
       
   178             int error = GetLastError();
       
   179             if (error == ERROR_NO_DATA) {
       
   180                 return (totalRead > 0) ? JNI_TRUE : JNI_FALSE;
       
   181             }
       
   182             JNU_ThrowIOExceptionWithLastError(env, "Drain");
       
   183             return JNI_FALSE;
       
   184         }
       
   185         if (read > 0) {
       
   186             totalRead += read;
       
   187         } else {
       
   188             break;
       
   189         }
       
   190     }
       
   191     return (totalRead > 0) ? JNI_TRUE : JNI_FALSE;
       
   192 }
       
   193 
       
   194 /* Note: This function returns the int fd value from file descriptor.
       
   195    It is mostly used for sockets which should use the int fd value.
       
   196 */
       
   197 jint
       
   198 fdval(JNIEnv *env, jobject fdo)
       
   199 {
       
   200     return (*env)->GetIntField(env, fdo, fd_fdID);
       
   201 }
       
   202 
       
   203 jlong
       
   204 handleval(JNIEnv *env, jobject fdo)
       
   205 {
       
   206     return (*env)->GetLongField(env, fdo, handle_fdID);
       
   207 }
       
   208 
       
   209 jboolean
       
   210 isNT()
       
   211 {
       
   212     return onNT;
       
   213 }