J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2002 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 "jni_util.h" |
| 33 | #include "jvm.h" |
| 34 | #include "net_util.h" |
| 35 | |
| 36 | #include "java_net_SocketOutputStream.h" |
| 37 | |
| 38 | #define min(a, b) ((a) < (b) ? (a) : (b)) |
| 39 | |
| 40 | /* |
| 41 | * SocketOutputStream |
| 42 | */ |
| 43 | |
| 44 | static jfieldID IO_fd_fdID; |
| 45 | |
| 46 | /* |
| 47 | * Class: java_net_SocketOutputStream |
| 48 | * Method: init |
| 49 | * Signature: ()V |
| 50 | */ |
| 51 | JNIEXPORT void JNICALL |
| 52 | Java_java_net_SocketOutputStream_init(JNIEnv *env, jclass cls) { |
| 53 | IO_fd_fdID = NET_GetFileDescriptorID(env); |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Class: java_net_SocketOutputStream |
| 58 | * Method: socketWrite0 |
| 59 | * Signature: (Ljava/io/FileDescriptor;[BII)V |
| 60 | */ |
| 61 | JNIEXPORT void JNICALL |
| 62 | Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this, |
| 63 | jobject fdObj, |
| 64 | jbyteArray data, |
| 65 | jint off, jint len) { |
| 66 | char *bufP; |
| 67 | char BUF[MAX_BUFFER_LEN]; |
| 68 | int buflen; |
| 69 | int fd; |
| 70 | jint n = 0; |
| 71 | |
| 72 | if (IS_NULL(fdObj)) { |
| 73 | JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
| 74 | return; |
| 75 | } else { |
| 76 | fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
| 77 | /* Bug 4086704 - If the Socket associated with this file descriptor |
| 78 | * was closed (sysCloseFD), the the file descriptor is set to -1. |
| 79 | */ |
| 80 | if (fd == -1) { |
| 81 | JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
| 87 | if (len <= MAX_BUFFER_LEN) { |
| 88 | bufP = BUF; |
| 89 | buflen = MAX_BUFFER_LEN; |
| 90 | } else { |
| 91 | buflen = min(MAX_HEAP_BUFFER_LEN, len); |
| 92 | bufP = (char *)malloc((size_t)buflen); |
| 93 | |
| 94 | /* if heap exhausted resort to stack buffer */ |
| 95 | if (bufP == NULL) { |
| 96 | bufP = BUF; |
| 97 | buflen = MAX_BUFFER_LEN; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | while(len > 0) { |
| 102 | int loff = 0; |
| 103 | int chunkLen = min(buflen, len); |
| 104 | int llen = chunkLen; |
| 105 | (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP); |
| 106 | |
| 107 | while(llen > 0) { |
| 108 | int n = NET_Send(fd, bufP + loff, llen, 0); |
| 109 | if (n > 0) { |
| 110 | llen -= n; |
| 111 | loff += n; |
| 112 | continue; |
| 113 | } |
| 114 | if (n == JVM_IO_INTR) { |
| 115 | JNU_ThrowByName(env, "java/io/InterruptedIOException", 0); |
| 116 | } else { |
| 117 | if (errno == ECONNRESET) { |
| 118 | JNU_ThrowByName(env, "sun/net/ConnectionResetException", |
| 119 | "Connection reset"); |
| 120 | } else { |
| 121 | NET_ThrowByNameWithLastError(env, "java/net/SocketException", |
| 122 | "Write failed"); |
| 123 | } |
| 124 | } |
| 125 | if (bufP != BUF) { |
| 126 | free(bufP); |
| 127 | } |
| 128 | return; |
| 129 | } |
| 130 | len -= chunkLen; |
| 131 | off += chunkLen; |
| 132 | } |
| 133 | |
| 134 | if (bufP != BUF) { |
| 135 | free(bufP); |
| 136 | } |
| 137 | } |