SocketOutputStream.c revision 240109a2df3fb0699cdaf919b490d34668814a4e
1/* 2 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle 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 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 "jni_util.h" 33#include "jvm.h" 34#include "net_util.h" 35 36#include "JNIHelp.h" 37 38#define NATIVE_METHOD(className, functionName, signature) \ 39{ #functionName, signature, (void*)(className ## _ ## functionName) } 40 41#define min(a, b) ((a) < (b) ? (a) : (b)) 42 43/* 44 * SocketOutputStream 45 */ 46 47static jfieldID IO_fd_fdID; 48 49/* 50 * Class: java_net_SocketOutputStream 51 * Method: init 52 * Signature: ()V 53 */ 54JNIEXPORT void JNICALL 55SocketOutputStream_init(JNIEnv *env, jclass cls) { 56 IO_fd_fdID = NET_GetFileDescriptorID(env); 57} 58 59/* 60 * Class: java_net_SocketOutputStream 61 * Method: socketWrite0 62 * Signature: (Ljava/io/FileDescriptor;[BII)V 63 */ 64JNIEXPORT void JNICALL 65SocketOutputStream_socketWrite0(JNIEnv *env, jobject this, 66 jobject fdObj, 67 jbyteArray data, 68 jint off, jint len) { 69 char *bufP; 70 char BUF[MAX_BUFFER_LEN]; 71 int buflen; 72 int fd; 73 74 if (IS_NULL(fdObj)) { 75 JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); 76 return; 77 } else { 78 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); 79 /* Bug 4086704 - If the Socket associated with this file descriptor 80 * was closed (sysCloseFD), the the file descriptor is set to -1. 81 */ 82 if (fd == -1) { 83 JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); 84 return; 85 } 86 87 } 88 89 if (len <= MAX_BUFFER_LEN) { 90 bufP = BUF; 91 buflen = MAX_BUFFER_LEN; 92 } else { 93 buflen = min(MAX_HEAP_BUFFER_LEN, len); 94 bufP = (char *)malloc((size_t)buflen); 95 96 /* if heap exhausted resort to stack buffer */ 97 if (bufP == NULL) { 98 bufP = BUF; 99 buflen = MAX_BUFFER_LEN; 100 } 101 } 102 103 while(len > 0) { 104 int loff = 0; 105 int chunkLen = min(buflen, len); 106 int llen = chunkLen; 107 (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP); 108 109 while(llen > 0) { 110 int n = NET_Send(fd, bufP + loff, llen, 0); 111 if (n > 0) { 112 llen -= n; 113 loff += n; 114 continue; 115 } 116 if (n == JVM_IO_INTR) { 117 JNU_ThrowByName(env, "java/io/InterruptedIOException", 0); 118 } else { 119 if (errno == ECONNRESET) { 120 JNU_ThrowByName(env, "sun/net/ConnectionResetException", 121 "Connection reset"); 122 } else { 123 NET_ThrowByNameWithLastError(env, "java/net/SocketException", 124 "Write failed"); 125 } 126 } 127 if (bufP != BUF) { 128 free(bufP); 129 } 130 return; 131 } 132 len -= chunkLen; 133 off += chunkLen; 134 } 135 136 if (bufP != BUF) { 137 free(bufP); 138 } 139} 140 141static JNINativeMethod gMethods[] = { 142 NATIVE_METHOD(SocketOutputStream, socketWrite0, "(Ljava/io/FileDescriptor;[BII)V"), 143 NATIVE_METHOD(SocketOutputStream, init, "()V"), 144}; 145 146void register_java_net_SocketOutputStream(JNIEnv* env) { 147 jniRegisterNativeMethods(env, "java/net/SocketOutputStream", gMethods, NELEM(gMethods)); 148} 149