--- old/src/java.base/unix/native/libnet/SocketInputStream.c 2016-09-07 18:50:57.753241551 +0530 +++ new/src/java.base/unix/native/libnet/SocketInputStream.c 2016-09-07 18:50:57.305240796 +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,46 @@ 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_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) { + int result = 0; + 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 +104,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 +137,17 @@ } 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); + } + if ((*env)->ExceptionCheck(env)) { + if (bufP != BUF) { + free(bufP); } + return nread; } - - nread = NET_Read(fd, bufP, len); - if (nread <= 0) { if (nread < 0) { @@ -143,7 +167,6 @@ JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", "Operation interrupted"); break; - default: JNU_ThrowByNameWithMessageAndLastError (env, JNU_JAVANETPKG "SocketException", "Read failed");