Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/nio/ch/IOUtil.c
32288 views
/*1* Copyright (c) 2000, 2012, 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"));58initInetAddressIDs(env);59}6061/**************************************************************62* IOUtil.c63*/64JNIEXPORT jboolean JNICALL65Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz,66jbyteArray randArray)67{68return69Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed(env,70clazz,71randArray);72}7374JNIEXPORT jint JNICALL75Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this)76{77return 16;78}798081jint82convertReturnVal(JNIEnv *env, jint n, jboolean reading)83{84if (n > 0) /* Number of bytes written */85return n;86if (n == 0) {87if (reading) {88return IOS_EOF; /* EOF is -1 in javaland */89} else {90return 0;91}92}93JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");94return IOS_THROWN;95}9697jlong98convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading)99{100if (n > 0) /* Number of bytes written */101return n;102if (n == 0) {103if (reading) {104return IOS_EOF; /* EOF is -1 in javaland */105} else {106return 0;107}108}109JNU_ThrowIOExceptionWithLastError(env, "Read/write failed");110return IOS_THROWN;111}112113JNIEXPORT jint JNICALL114Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo)115{116return fdval(env, fdo);117}118119JNIEXPORT void JNICALL120Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val)121{122(*env)->SetIntField(env, fdo, fd_fdID, val);123}124125126#define SET_BLOCKING 0127#define SET_NONBLOCKING 1128129JNIEXPORT void JNICALL130Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,131jobject fdo, jboolean blocking)132{133u_long argp;134int result = 0;135jint fd = fdval(env, fdo);136137if (blocking == JNI_FALSE) {138argp = SET_NONBLOCKING;139} else {140argp = SET_BLOCKING;141/* Blocking fd cannot be registered with EventSelect */142WSAEventSelect(fd, NULL, 0);143}144result = ioctlsocket(fd, FIONBIO, &argp);145if (result == SOCKET_ERROR) {146int error = WSAGetLastError();147handleSocketError(env, (jint)error);148}149}150151/* Note: Drain uses the int fd value. It is currently not called152on windows.153*/154JNIEXPORT jboolean JNICALL155Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd)156{157DWORD read = 0;158int totalRead = 0;159BOOL result = 0;160HANDLE h = (HANDLE)_get_osfhandle(fd);161char buf[128];162163if (h == INVALID_HANDLE_VALUE) {164JNU_ThrowIOExceptionWithLastError(env, "Read failed");165return JNI_FALSE;166}167168for (;;) {169result = ReadFile(h, /* File handle to read */170(LPVOID)&buf, /* address to put data */171128, /* number of bytes to read */172&read, /* number of bytes read */173NULL); /* no overlapped struct */174175if (result == 0) {176int error = GetLastError();177if (error == ERROR_NO_DATA) {178return (totalRead > 0) ? JNI_TRUE : JNI_FALSE;179}180JNU_ThrowIOExceptionWithLastError(env, "Drain");181return JNI_FALSE;182}183if (read > 0) {184totalRead += read;185} else {186break;187}188}189return (totalRead > 0) ? JNI_TRUE : JNI_FALSE;190}191192/* Note: This function returns the int fd value from file descriptor.193It is mostly used for sockets which should use the int fd value.194*/195jint196fdval(JNIEnv *env, jobject fdo)197{198return (*env)->GetIntField(env, fdo, fd_fdID);199}200201jlong202handleval(JNIEnv *env, jobject fdo)203{204return (*env)->GetLongField(env, fdo, handle_fdID);205}206207208