Home | History | Annotate | Download | only in native
      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 <nativehelper/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 
     47 static jfieldID IO_fd_fdID;
     48 
     49 /*
     50  * Class:     java_net_SocketOutputStream
     51  * Method:    socketWrite0
     52  * Signature: (Ljava/io/FileDescriptor;[BII)V
     53  */
     54 JNIEXPORT void JNICALL
     55 SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
     56                                               jobject fdObj,
     57                                               jbyteArray data,
     58                                               jint off, jint len) {
     59     char *bufP;
     60     char BUF[MAX_BUFFER_LEN];
     61     int buflen;
     62     int fd;
     63 
     64     if (IS_NULL(fdObj)) {
     65         JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
     66         return;
     67     } else {
     68         fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
     69         /* Bug 4086704 - If the Socket associated with this file descriptor
     70          * was closed (sysCloseFD), the the file descriptor is set to -1.
     71          */
     72         if (fd == -1) {
     73             JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
     74             return;
     75         }
     76 
     77     }
     78 
     79     if (len <= MAX_BUFFER_LEN) {
     80         bufP = BUF;
     81         buflen = MAX_BUFFER_LEN;
     82     } else {
     83         buflen = min(MAX_HEAP_BUFFER_LEN, len);
     84         bufP = (char *)malloc((size_t)buflen);
     85 
     86         /* if heap exhausted resort to stack buffer */
     87         if (bufP == NULL) {
     88             bufP = BUF;
     89             buflen = MAX_BUFFER_LEN;
     90         }
     91     }
     92 
     93     while(len > 0) {
     94         int loff = 0;
     95         int chunkLen = min(buflen, len);
     96         int llen = chunkLen;
     97         (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);
     98 
     99         while(llen > 0) {
    100             int n = NET_Send(fd, bufP + loff, llen, 0);
    101             if (n > 0) {
    102                 llen -= n;
    103                 loff += n;
    104                 continue;
    105             }
    106             if (n == JVM_IO_INTR) {
    107                 JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
    108             } else {
    109                 if (errno == ECONNRESET) {
    110                     JNU_ThrowByName(env, "sun/net/ConnectionResetException",
    111                         "Connection reset");
    112                 } else {
    113                     NET_ThrowByNameWithLastError(env, "java/net/SocketException",
    114                         "Write failed");
    115                 }
    116             }
    117             if (bufP != BUF) {
    118                 free(bufP);
    119             }
    120             return;
    121         }
    122         len -= chunkLen;
    123         off += chunkLen;
    124     }
    125 
    126     if (bufP != BUF) {
    127         free(bufP);
    128     }
    129 }
    130 
    131 static JNINativeMethod gMethods[] = {
    132   NATIVE_METHOD(SocketOutputStream, socketWrite0, "(Ljava/io/FileDescriptor;[BII)V"),
    133 };
    134 
    135 void register_java_net_SocketOutputStream(JNIEnv* env) {
    136   jniRegisterNativeMethods(env, "java/net/SocketOutputStream", gMethods, NELEM(gMethods));
    137 
    138   // Init field ids
    139   IO_fd_fdID = NET_GetFileDescriptorID(env);
    140 }
    141