Path: blob/master/src/java.base/windows/native/libnet/SocketInputStream.c
41119 views
/*1* Copyright (c) 1997, 2016, 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*/24#include <malloc.h>2526#include "net_util.h"2728#include "java_net_SocketInputStream.h"2930/*************************************************************************31* SocketInputStream32*/33static jfieldID IO_fd_fdID;3435/*36* Class: java_net_SocketInputStream37* Method: init38* Signature: ()V39*/40JNIEXPORT void JNICALL41Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) {42IO_fd_fdID = NET_GetFileDescriptorID(env);43}4445/*46* Class: java_net_SocketInputStream47* Method: socketRead48* Signature: (Ljava/io/FileDescriptor;[BIII)I49*/50JNIEXPORT jint JNICALL51Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,52jobject fdObj, jbyteArray data,53jint off, jint len, jint timeout)54{55char BUF[MAX_BUFFER_LEN];56char *bufP;57jint fd, newfd, nread;5859if (IS_NULL(fdObj)) {60JNU_ThrowByName(env, "java/net/SocketException",61"Socket closed");62return -1;63}64fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);65if (fd == -1) {66JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");67return -1;68}6970/*71* If the caller buffer is large than our stack buffer then we allocate72* from the heap (up to a limit). If memory is exhausted we always use73* the stack buffer.74*/75if (len <= MAX_BUFFER_LEN) {76bufP = BUF;77} else {78if (len > MAX_HEAP_BUFFER_LEN) {79len = MAX_HEAP_BUFFER_LEN;80}81bufP = (char *)malloc((size_t)len);82if (bufP == NULL) {83/* allocation failed so use stack buffer */84bufP = BUF;85len = MAX_BUFFER_LEN;86}87}888990if (timeout) {91if (timeout <= 5000 || !isRcvTimeoutSupported) {92int ret = NET_Timeout (fd, timeout);9394if (ret <= 0) {95if (ret == 0) {96JNU_ThrowByName(env, "java/net/SocketTimeoutException",97"Read timed out");98} else if (ret == -1) {99JNU_ThrowByName(env, "java/net/SocketException", "socket closed");100}101if (bufP != BUF) {102free(bufP);103}104return -1;105}106107/*check if the socket has been closed while we were in timeout*/108newfd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);109if (newfd == -1) {110JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");111if (bufP != BUF) {112free(bufP);113}114return -1;115}116}117}118119nread = recv(fd, bufP, len, 0);120if (nread > 0) {121(*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);122} else {123if (nread < 0) {124int err = WSAGetLastError();125// Check if the socket has been closed since we last checked.126// This could be a reason for recv failing.127if ((*env)->GetIntField(env, fdObj, IO_fd_fdID) == -1) {128JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");129} else {130switch (err) {131case WSAEINTR:132JNU_ThrowByName(env, "java/net/SocketException",133"socket closed");134break;135136case WSAECONNRESET:137case WSAESHUTDOWN:138/*139* Connection has been reset - Windows sometimes reports140* the reset as a shutdown error.141*/142JNU_ThrowByName(env, "sun/net/ConnectionResetException",143"");144break;145146case WSAETIMEDOUT :147JNU_ThrowByName(env, "java/net/SocketTimeoutException",148"Read timed out");149break;150151default:152NET_ThrowCurrent(env, "recv failed");153}154}155}156}157if (bufP != BUF) {158free(bufP);159}160return nread;161}162163164