11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <stdlib.h> 27 #include <errno.h> 28 #include <string.h> 29 #include <sys/types.h> 30 #include <sys/socket.h> 31 32 #include "jvm.h" 33 #include "jni_util.h" 34 #include "net_util.h" 35 36 #include "java_net_SocketInputStream.h" 37 38 39 /************************************************************************ 40 * SocketInputStream 41 */ 42 43 static jfieldID IO_fd_fdID; 44 45 /* 46 * Class: java_net_SocketInputStream 47 * Method: init 48 * Signature: ()V 49 */ 50 JNIEXPORT void JNICALL 51 Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) { 52 IO_fd_fdID = NET_GetFileDescriptorID(env); 53 } 54 55 /* 56 * Class: java_net_SocketInputStream 57 * Method: socketRead0 58 * Signature: (Ljava/io/FileDescriptor;[BIII)I 59 */ 60 JNIEXPORT jint JNICALL 61 Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this, 62 jobject fdObj, jbyteArray data, 63 jint off, jint len, jint timeout) 64 { 65 char BUF[MAX_BUFFER_LEN]; 66 char *bufP; 67 jint fd, nread; 68 69 if (IS_NULL(fdObj)) { 70 /* shouldn't this be a NullPointerException? -br */ 71 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", 72 "Socket closed"); 73 return -1; 74 } else { 75 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); 76 /* Bug 4086704 - If the Socket associated with this file descriptor 77 * was closed (sysCloseFD), then the file descriptor is set to -1. 78 */ 79 if (fd == -1) { 80 JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); 81 return -1; 82 } 83 } 84 85 /* 86 * If the read is greater than our stack allocated buffer then 87 * we allocate from the heap (up to a limit) 88 */ 89 if (len > MAX_BUFFER_LEN) { 90 if (len > MAX_HEAP_BUFFER_LEN) { 91 len = MAX_HEAP_BUFFER_LEN; 92 } 93 bufP = (char *)malloc((size_t)len); 94 if (bufP == NULL) { 95 bufP = BUF; 96 len = MAX_BUFFER_LEN; 97 } 98 } else { 99 bufP = BUF; 100 } 101 102 if (timeout) { 103 nread = NET_Timeout(fd, timeout); 104 if (nread <= 0) { 105 if (nread == 0) { 106 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", 107 "Read timed out"); 108 } else if (nread == -1) { 109 if (errno == EBADF) { 110 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); 111 } else if (errno == ENOMEM) { 112 JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); 113 } else { 114 JNU_ThrowByNameWithMessageAndLastError 115 (env, JNU_JAVANETPKG "SocketException", "select/poll failed"); 116 } 117 } 118 if (bufP != BUF) { 119 free(bufP); 120 } 121 return -1; 122 } 123 } 124 125 nread = NET_Read(fd, bufP, len); 126 127 if (nread <= 0) { 128 if (nread < 0) { 129 130 switch (errno) { 131 case ECONNRESET: 132 case EPIPE: 133 JNU_ThrowByName(env, "sun/net/ConnectionResetException", 134 "Connection reset"); 135 break; 136 137 case EBADF: 138 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", 139 "Socket closed"); 140 break; 141 142 case EINTR: 143 JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", 144 "Operation interrupted"); 145 break; 146 147 default: 148 JNU_ThrowByNameWithMessageAndLastError 149 (env, JNU_JAVANETPKG "SocketException", "Read failed"); 150 } 151 } 152 } else { 153 (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP); 154 } 155 156 if (bufP != BUF) { 157 free(bufP); 158 } 159 return nread; 160 } | 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <stdlib.h> 27 #include <errno.h> 28 #include <string.h> 29 #include <sys/types.h> 30 #include <sys/socket.h> 31 #include <sys/time.h> 32 #include "jvm.h" 33 #include "jni_util.h" 34 #include "net_util.h" 35 36 #include "java_net_SocketInputStream.h" 37 38 /************************************************************************ 39 * SocketInputStream 40 */ 41 42 static jfieldID IO_fd_fdID; 43 44 /* 45 * Class: java_net_SocketInputStream 46 * Method: init 47 * Signature: ()V 48 */ 49 JNIEXPORT void JNICALL 50 Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) { 51 IO_fd_fdID = NET_GetFileDescriptorID(env); 52 } 53 54 /* 55 * Class: java_net_SocketInputStream 56 * Method: socketRead0 57 * Signature: (Ljava/io/FileDescriptor;[BIII)I 58 */ 59 JNIEXPORT jint JNICALL 60 Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this, 61 jobject fdObj, jbyteArray data, 62 jint off, jint len, jint timeout) 63 { 64 char BUF[MAX_BUFFER_LEN]; 65 char *bufP; 66 jint fd, nread; 67 long prevtime = 0, newtime; 68 struct timeval t; 69 long _timeout = timeout; 70 71 if (_timeout > 0) { 72 gettimeofday(&t, NULL); 73 prevtime = t.tv_sec * 1000 + t.tv_usec / 1000; 74 } 75 76 if (IS_NULL(fdObj)) { 77 /* shouldn't this be a NullPointerException? -br */ 78 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", 79 "Socket closed"); 80 return -1; 81 } else { 82 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); 83 /* Bug 4086704 - If the Socket associated with this file descriptor 84 * was closed (sysCloseFD), then the file descriptor is set to -1. 85 */ 86 if (fd == -1) { 87 JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); 88 return -1; 89 } 90 } 91 92 /* 93 * If the read is greater than our stack allocated buffer then 94 * we allocate from the heap (up to a limit) 95 */ 96 if (len > MAX_BUFFER_LEN) { 97 if (len > MAX_HEAP_BUFFER_LEN) { 98 len = MAX_HEAP_BUFFER_LEN; 99 } 100 bufP = (char *)malloc((size_t)len); 101 if (bufP == NULL) { 102 bufP = BUF; 103 len = MAX_BUFFER_LEN; 104 } 105 } else { 106 bufP = BUF; 107 } 108 109 LABEL: if (_timeout) { 110 nread = NET_Timeout(fd, _timeout); 111 if (nread <= 0) { 112 if (nread == 0) { 113 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", 114 "Read timed out"); 115 } else if (nread == -1) { 116 if (errno == EBADF) { 117 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); 118 } else if (errno == ENOMEM) { 119 JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed"); 120 } else { 121 JNU_ThrowByNameWithMessageAndLastError 122 (env, JNU_JAVANETPKG "SocketException", "select/poll failed"); 123 } 124 } 125 if (bufP != BUF) { 126 free(bufP); 127 } 128 return -1; 129 } 130 nread = NET_NonBlockingRead(fd, bufP, len); 131 if (nread == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) { 132 gettimeofday(&t, NULL); 133 newtime = t.tv_sec * 1000 + t.tv_usec / 1000; 134 _timeout -= newtime - prevtime; 135 if(_timeout > 0){ 136 prevtime = newtime; 137 goto LABEL; 138 } 139 } 140 } else { // no timeout set, it has to be blocking call. 141 nread = NET_Read(fd, bufP, len); 142 } 143 if (nread <= 0) { 144 if (nread < 0) { 145 146 switch (errno) { 147 case ECONNRESET: 148 case EPIPE: 149 JNU_ThrowByName(env, "sun/net/ConnectionResetException", 150 "Connection reset"); 151 break; 152 153 case EBADF: 154 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", 155 "Socket closed"); 156 break; 157 158 case EINTR: 159 JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException", 160 "Operation interrupted"); 161 break; 162 default: 163 JNU_ThrowByNameWithMessageAndLastError 164 (env, JNU_JAVANETPKG "SocketException", "Read failed"); 165 } 166 } 167 } else { 168 (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP); 169 } 170 171 if (bufP != BUF) { 172 free(bufP); 173 } 174 return nread; 175 } |