--- old/src/java.base/aix/native/libnet/aix_close.c 2016-09-06 14:36:39.983179734 +0530 +++ new/src/java.base/aix/native/libnet/aix_close.c 2016-09-06 14:36:39.859180107 +0530 @@ -410,6 +410,10 @@ BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) ); } +int NET_NonBlockingRead(int s, void* buf, size_t len) { + BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT)); +} + int NET_ReadV(int s, const struct iovec * vector, int count) { BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) ); } --- old/src/java.base/linux/native/libnet/linux_close.c 2016-09-06 14:36:40.359178605 +0530 +++ new/src/java.base/linux/native/libnet/linux_close.c 2016-09-06 14:36:40.239178966 +0530 @@ -367,6 +367,10 @@ BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) ); } +int NET_NonBlockingRead(int s, void* buf, size_t len) { + BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT) ); +} + int NET_ReadV(int s, const struct iovec * vector, int count) { BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) ); } --- old/src/java.base/macosx/native/libnet/bsd_close.c 2016-09-06 14:36:40.727177500 +0530 +++ new/src/java.base/macosx/native/libnet/bsd_close.c 2016-09-06 14:36:40.583177932 +0530 @@ -371,6 +371,10 @@ BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) ); } +int NET_NonBlockingRead(int s, void* buf, size_t len) { + BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT)); +} + int NET_ReadV(int s, const struct iovec * vector, int count) { BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) ); } --- old/src/java.base/solaris/native/libnet/solaris_close.c 2016-09-06 14:36:41.111176347 +0530 +++ new/src/java.base/solaris/native/libnet/solaris_close.c 2016-09-06 14:36:40.987176719 +0530 @@ -35,7 +35,7 @@ if (1) { \ do { \ _result = _cmd; \ - } while((_result == -1) && (errno == EINTR)); \ + } while((_result == -1) && (errno == EINTR)); \ return _result; \ } \ } while(0) @@ -44,6 +44,10 @@ RESTARTABLE_RETURN_INT(recv(s, buf, len, 0)); } +int NET_NonBlockingRead(int s, void* buf, size_t len) { + RESTARTABLE_RETURN_INT(recv(s, buf, len, MSG_DONTWAIT)); +} + int NET_RecvFrom(int s, void *buf, int len, unsigned int flags, struct sockaddr *from, socklen_t *fromlen) { RESTARTABLE_RETURN_INT(recvfrom(s, buf, len, flags, from, fromlen)); --- old/src/java.base/unix/native/libnet/SocketInputStream.c 2016-09-06 14:36:41.491175205 +0530 +++ new/src/java.base/unix/native/libnet/SocketInputStream.c 2016-09-06 14:36:41.367175578 +0530 @@ -28,14 +28,13 @@ #include #include #include - +#include #include "jvm.h" #include "jni_util.h" #include "net_util.h" #include "java_net_SocketInputStream.h" - /************************************************************************ * SocketInputStream */ @@ -52,6 +51,68 @@ IO_fd_fdID = NET_GetFileDescriptorID(env); } +static long getCurrentTime() { + struct timeval time; + gettimeofday(&time, NULL); + return (time.tv_sec * 1000 + time.tv_usec / 1000); +} + +static int NET_TimeoutWithCurrentTime(int s, long timeout, long current_time) { + int result; + long prevtime = current_time, newtime; + struct pollfd pfd; + pfd.fd = s; + pfd.events = POLLIN; + for (;;) { + result = poll(&pfd, 1, timeout); + if (result < 0 && errno == EINTR) { + if (timeout > 0) { + newtime = getCurrentTime(); + timeout -= newtime - prevtime; + if (timeout <= 0) + return 0; + prevtime = newtime; + } + } else { + return result; + } + } +} + +static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) { + int result; + long prevtime = getCurrentTime(), newtime; + while (timeout > 0) { + result = NET_TimeoutWithCurrentTime(fd, timeout, prevtime); + if (result <= 0) { + if (result == 0) { + JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", "Read timed out"); + } else if (result == -1) { + if (errno == EBADF) { + JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); + } else if (errno == ENOMEM) { + JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); + } else { + JNU_ThrowByNameWithMessageAndLastError + (env, JNU_JAVANETPKG "SocketException", "select/poll failed"); + } + } + return -1; + } + result = NET_NonBlockingRead(fd, bufP, len); + if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) { + newtime = getCurrentTime(); + timeout -= newtime - prevtime; + if (timeout > 0) { + prevtime = newtime; + } + } else { + break; + } + } + return result; +} + /* * Class: java_net_SocketInputStream * Method: socketRead0 @@ -65,7 +126,7 @@ char BUF[MAX_BUFFER_LEN]; char *bufP; jint fd, nread; - + if (IS_NULL(fdObj)) { /* shouldn't this be a NullPointerException? -br */ JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", @@ -98,32 +159,11 @@ } else { bufP = BUF; } - if (timeout) { - nread = NET_Timeout(fd, timeout); - if (nread <= 0) { - if (nread == 0) { - JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", - "Read timed out"); - } else if (nread == -1) { - if (errno == EBADF) { - JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); - } else if (errno == ENOMEM) { - JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); - } else { - JNU_ThrowByNameWithMessageAndLastError - (env, JNU_JAVANETPKG "SocketException", "select/poll failed"); - } - } - if (bufP != BUF) { - free(bufP); - } - return -1; - } + nread = NET_ReadWithTimeout(env, fd, bufP, len, timeout); + } else { + nread = NET_Read(fd, bufP, len); } - - nread = NET_Read(fd, bufP, len); - if (nread <= 0) { if (nread < 0) { @@ -143,7 +183,6 @@ JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", "Operation interrupted"); break; - default: JNU_ThrowByNameWithMessageAndLastError (env, JNU_JAVANETPKG "SocketException", "Read failed"); --- old/src/java.base/unix/native/libnet/net_util_md.h 2016-09-06 14:36:41.863174087 +0530 +++ new/src/java.base/unix/native/libnet/net_util_md.h 2016-09-06 14:36:41.731174484 +0530 @@ -36,6 +36,7 @@ int NET_Timeout(int s, long timeout); int NET_Read(int s, void* buf, size_t len); +int NET_NonBlockingRead(int s, void* buf, size_t len); int NET_RecvFrom(int s, void *buf, int len, unsigned int flags, struct sockaddr *from, socklen_t *fromlen); int NET_ReadV(int s, const struct iovec * vector, int count);