1/* 2 * Copyright (c) 1997, 2016, 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#include <errno.h> 26#include <stdlib.h> 27#include <string.h> 28 29#include "net_util.h" 30 31#include "java_net_SocketOutputStream.h" 32 33#define min(a, b) ((a) < (b) ? (a) : (b)) 34 35/* 36 * SocketOutputStream 37 */ 38 39static jfieldID IO_fd_fdID; 40 41/* 42 * Class: java_net_SocketOutputStream 43 * Method: init 44 * Signature: ()V 45 */ 46JNIEXPORT void JNICALL 47Java_java_net_SocketOutputStream_init(JNIEnv *env, jclass cls) { 48 IO_fd_fdID = NET_GetFileDescriptorID(env); 49} 50 51/* 52 * Class: java_net_SocketOutputStream 53 * Method: socketWrite0 54 * Signature: (Ljava/io/FileDescriptor;[BII)V 55 */ 56JNIEXPORT void JNICALL 57Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this, 58 jobject fdObj, 59 jbyteArray data, 60 jint off, jint len) { 61 char *bufP; 62 char BUF[MAX_BUFFER_LEN]; 63 int buflen; 64 int fd; 65 66 if (IS_NULL(fdObj)) { 67 JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); 68 return; 69 } else { 70 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); 71 /* Bug 4086704 - If the Socket associated with this file descriptor 72 * was closed (sysCloseFD), the file descriptor is set to -1. 73 */ 74 if (fd == -1) { 75 JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); 76 return; 77 } 78 79 } 80 81 if (len <= MAX_BUFFER_LEN) { 82 bufP = BUF; 83 buflen = MAX_BUFFER_LEN; 84 } else { 85 buflen = min(MAX_HEAP_BUFFER_LEN, len); 86 bufP = (char *)malloc((size_t)buflen); 87 88 /* if heap exhausted resort to stack buffer */ 89 if (bufP == NULL) { 90 bufP = BUF; 91 buflen = MAX_BUFFER_LEN; 92 } 93 } 94 95 while(len > 0) { 96 int loff = 0; 97 int chunkLen = min(buflen, len); 98 int llen = chunkLen; 99 (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP); 100 101 if ((*env)->ExceptionCheck(env)) { 102 break; 103 } else { 104 while(llen > 0) { 105 int n = NET_Send(fd, bufP + loff, llen, 0); 106 if (n > 0) { 107 llen -= n; 108 loff += n; 109 continue; 110 } 111 if (errno == ECONNRESET) { 112 JNU_ThrowByName(env, "sun/net/ConnectionResetException", 113 "Connection reset"); 114 } else { 115 JNU_ThrowByNameWithMessageAndLastError 116 (env, "java/net/SocketException", "Write failed"); 117 } 118 if (bufP != BUF) { 119 free(bufP); 120 } 121 return; 122 } 123 len -= chunkLen; 124 off += chunkLen; 125 } 126 } 127 128 if (bufP != BUF) { 129 free(bufP); 130 } 131} 132