jdk/src/solaris/native/java/net/SocketOutputStream.c
author chegar
Mon, 28 Jul 2008 13:02:11 +0100
changeset 910 1f53246fb014
parent 2 90ce3da70b43
child 1247 b4c26443dee5
permissions -rw-r--r--
6729881: Compiler warning in networking native code Summary: Cleanup compiler warnings Reviewed-by: alanb, jccollet, michaelm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2002 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <errno.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <sys/socket.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "java_net_SocketOutputStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#define min(a, b)       ((a) < (b) ? (a) : (b))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * SocketOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Class:     java_net_SocketOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
Java_java_net_SocketOutputStream_init(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Class:     java_net_SocketOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Method:    socketWrite0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Signature: (Ljava/io/FileDescriptor;[BII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                              jobject fdObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                                              jbyteArray data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                                              jint off, jint len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    char *bufP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    int buflen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        /* Bug 4086704 - If the Socket associated with this file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         * was closed (sysCloseFD), the the file descriptor is set to -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    if (len <= MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        buflen = MAX_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        buflen = min(MAX_HEAP_BUFFER_LEN, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        bufP = (char *)malloc((size_t)buflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        /* if heap exhausted resort to stack buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (bufP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            buflen = MAX_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    while(len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        int loff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        int chunkLen = min(buflen, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int llen = chunkLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        while(llen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            int n = NET_Send(fd, bufP + loff, llen, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                llen -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                loff += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            if (n == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                if (errno == ECONNRESET) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    JNU_ThrowByName(env, "sun/net/ConnectionResetException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        "Connection reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    NET_ThrowByNameWithLastError(env, "java/net/SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        "Write failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            if (bufP != BUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                free(bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        len -= chunkLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        off += chunkLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    if (bufP != BUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        free(bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
}