Path: blob/master/src/java.base/unix/native/libnet/SocketInputStream.c
41119 views
/*1* Copyright (c) 1997, 2017, 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 <errno.h>25#include <stdlib.h>26#include <string.h>2728#include "jvm.h"29#include "net_util.h"3031#include "java_net_SocketInputStream.h"3233/*34* SocketInputStream35*/3637static jfieldID IO_fd_fdID;3839/*40* Class: java_net_SocketInputStream41* Method: init42* Signature: ()V43*/44JNIEXPORT void JNICALL45Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) {46IO_fd_fdID = NET_GetFileDescriptorID(env);47}4849static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) {50int result = 0;51jlong prevNanoTime = JVM_NanoTime(env, 0);52jlong nanoTimeout = (jlong) timeout * NET_NSEC_PER_MSEC;53while (nanoTimeout >= NET_NSEC_PER_MSEC) {54result = NET_Timeout(env, fd, nanoTimeout / NET_NSEC_PER_MSEC, prevNanoTime);55if (result <= 0) {56if (result == 0) {57JNU_ThrowByName(env, "java/net/SocketTimeoutException", "Read timed out");58} else if (result == -1) {59if (errno == EBADF) {60JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");61} else if (errno == ENOMEM) {62JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");63} else {64JNU_ThrowByNameWithMessageAndLastError65(env, "java/net/SocketException", "select/poll failed");66}67}68return -1;69}70result = NET_NonBlockingRead(fd, bufP, len);71if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) {72jlong newtNanoTime = JVM_NanoTime(env, 0);73nanoTimeout -= newtNanoTime - prevNanoTime;74if (nanoTimeout >= NET_NSEC_PER_MSEC) {75prevNanoTime = newtNanoTime;76}77} else {78break;79}80}81return result;82}8384/*85* Class: java_net_SocketInputStream86* Method: socketRead087* Signature: (Ljava/io/FileDescriptor;[BIII)I88*/89JNIEXPORT jint JNICALL90Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,91jobject fdObj, jbyteArray data,92jint off, jint len, jint timeout)93{94char BUF[MAX_BUFFER_LEN];95char *bufP;96jint fd, nread;9798if (IS_NULL(fdObj)) {99JNU_ThrowByName(env, "java/net/SocketException",100"Socket closed");101return -1;102}103fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);104if (fd == -1) {105JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");106return -1;107}108109/*110* If the read is greater than our stack allocated buffer then111* we allocate from the heap (up to a limit)112*/113if (len > MAX_BUFFER_LEN) {114if (len > MAX_HEAP_BUFFER_LEN) {115len = MAX_HEAP_BUFFER_LEN;116}117bufP = (char *)malloc((size_t)len);118if (bufP == NULL) {119bufP = BUF;120len = MAX_BUFFER_LEN;121}122} else {123bufP = BUF;124}125if (timeout) {126nread = NET_ReadWithTimeout(env, fd, bufP, len, timeout);127if ((*env)->ExceptionCheck(env)) {128if (bufP != BUF) {129free(bufP);130}131return nread;132}133} else {134nread = NET_Read(fd, bufP, len);135}136137if (nread <= 0) {138if (nread < 0) {139140switch (errno) {141case ECONNRESET:142case EPIPE:143JNU_ThrowByName(env, "sun/net/ConnectionResetException",144"Connection reset");145break;146147case EBADF:148JNU_ThrowByName(env, "java/net/SocketException",149"Socket closed");150break;151152case EINTR:153JNU_ThrowByName(env, "java/io/InterruptedIOException",154"Operation interrupted");155break;156default:157JNU_ThrowByNameWithMessageAndLastError158(env, "java/net/SocketException", "Read failed");159}160}161} else {162(*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);163}164165if (bufP != BUF) {166free(bufP);167}168return nread;169}170171172