Path: blob/master/src/java.base/windows/native/libnio/ch/IOUtil.c
41134 views
/*1* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <windows.h>26#include <winsock2.h>27#include <io.h>28#include "jni.h"29#include "jni_util.h"30#include "jvm.h"31#include "jlong.h"3233#include "nio.h"34#include "nio_util.h"35#include "net_util.h"36#include "sun_nio_ch_IOUtil.h"3738/* field id for jlong 'handle' in java.io.FileDescriptor used for file fds */39static jfieldID handle_fdID;4041/* field id for jint 'fd' in java.io.FileDescriptor used for socket fds */42static jfieldID fd_fdID;4344JNIEXPORT jboolean JNICALL45Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed46(JNIEnv *env, jclass clazz, jbyteArray randArray);4748/**************************************************************49* static method to store field IDs in initializers50*/5152JNIEXPORT void JNICALL53Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz)54{55CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));56CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));57CHECK_NULL(handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J"));58}5960/**************************************************************61* IOUtil.c62*/63JNIEXPORT jboolean JNICALL64Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz,65jbyteArray randArray)66{67return68Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed(env,69clazz,70randArray);71}7273JNIEXPORT jint JNICALL74Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this)75{76return 16;77}787980jint81convertReturnVal(JNIEnv *env, jint n, jboolean reading)82{83if (n > 0) /* Number of bytes written */84return n;85if (n == 0) {86if (reading) {87return IOS_EOF; /* EOF is -1 in javaland */88} else {89return 0;90}91}92JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");93return IOS_THROWN;94}9596jlong97convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)98{99if (n > 0) /* Number of bytes written */100return n;101if (n == 0) {102if (reading) {103return IOS_EOF; /* EOF is -1 in javaland */104} else {105return 0;106}107}108JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");109return IOS_THROWN;110}111112JNIEXPORT jint JNICALL113Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo)114{115return fdval(env, fdo);116}117118JNIEXPORT void JNICALL119Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val)120{121setfdval(env, fdo, val);122}123124125#define SET_BLOCKING 0126#define SET_NONBLOCKING 1127128JNIEXPORT void JNICALL129Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,130jobject fdo, jboolean blocking)131{132u_long argp;133int result = 0;134jint fd = fdval(env, fdo);135136if (blocking == JNI_FALSE) {137argp = SET_NONBLOCKING;138} else {139argp = SET_BLOCKING;140/* Blocking fd cannot be registered with EventSelect */141WSAEventSelect(fd, NULL, 0);142}143result = ioctlsocket(fd, FIONBIO, &argp);144if (result == SOCKET_ERROR) {145int error = WSAGetLastError();146handleSocketError(env, (jint)error);147}148}149150JNIEXPORT jboolean JNICALL151Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)152{153char buf[16];154jboolean readBytes = JNI_FALSE;155for (;;) {156int n = recv((SOCKET) fd, buf, sizeof(buf), 0);157if (n == SOCKET_ERROR) {158if (WSAGetLastError() != WSAEWOULDBLOCK) {159JNU_ThrowIOExceptionWithLastError(env, "recv failed");160}161return readBytes;162}163if (n <= 0)164return readBytes;165if (n < (int)sizeof(buf))166return JNI_TRUE;167readBytes = JNI_TRUE;168}169}170171JNIEXPORT jint JNICALL172Java_sun_nio_ch_IOUtil_write1(JNIEnv *env, jclass cl, jint fd, jbyte b)173{174int n = send((SOCKET) fd, &b, 1, 0);175if (n == SOCKET_ERROR && WSAGetLastError() != WSAEWOULDBLOCK) {176JNU_ThrowIOExceptionWithLastError(env, "send failed");177return IOS_THROWN;178}179return (n == 1) ? 1 : 0;180}181182/* Note: This function returns the int fd value from file descriptor.183It is mostly used for sockets which should use the int fd value.184*/185jint186fdval(JNIEnv *env, jobject fdo)187{188return (*env)->GetIntField(env, fdo, fd_fdID);189}190191void192setfdval(JNIEnv *env, jobject fdo, jint val)193{194(*env)->SetIntField(env, fdo, fd_fdID, val);195}196197jlong198handleval(JNIEnv *env, jobject fdo)199{200return (*env)->GetLongField(env, fdo, handle_fdID);201}202203204