src/java.base/unix/native/libnet/SocketOutputStream.c
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43202 jdk/src/java.base/unix/native/libnet/SocketOutputStream.c@29180c8db039
child 50425 43b54a307c89
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39318
2006d1d41c8b 8158023: SocketExceptions contain too little information sometimes
clanger
parents: 28059
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1247
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 39318
diff changeset
    25
#include <errno.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "java_net_SocketOutputStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#define min(a, b)       ((a) < (b) ? (a) : (b))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * SocketOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Class:     java_net_SocketOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
Java_java_net_SocketOutputStream_init(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Class:     java_net_SocketOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Method:    socketWrite0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Signature: (Ljava/io/FileDescriptor;[BII)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                              jobject fdObj,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                              jbyteArray data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                              jint off, jint len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    char *bufP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int buflen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        /* Bug 4086704 - If the Socket associated with this file descriptor
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
    72
         * was closed (sysCloseFD), the file descriptor is set to -1.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    if (len <= MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        buflen = MAX_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        buflen = min(MAX_HEAP_BUFFER_LEN, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        bufP = (char *)malloc((size_t)buflen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        /* if heap exhausted resort to stack buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (bufP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            buflen = MAX_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    while(len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        int loff = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        int chunkLen = min(buflen, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        int llen = chunkLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
43202
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   101
        if ((*env)->ExceptionCheck(env)) {
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   102
            break;
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   103
        } else {
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   104
            while(llen > 0) {
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   105
                int n = NET_Send(fd, bufP + loff, llen, 0);
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   106
                if (n > 0) {
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   107
                    llen -= n;
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   108
                    loff += n;
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   109
                    continue;
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   110
                }
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   111
                if (errno == ECONNRESET) {
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   112
                    JNU_ThrowByName(env, "sun/net/ConnectionResetException",
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   113
                        "Connection reset");
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   114
                } else {
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   115
                    JNU_ThrowByNameWithMessageAndLastError
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   116
                        (env, "java/net/SocketException", "Write failed");
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   117
                }
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   118
                if (bufP != BUF) {
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   119
                    free(bufP);
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   120
                }
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   121
                return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
43202
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   123
            len -= chunkLen;
29180c8db039 8164147: Improve streaming socket output
msheppar
parents: 41771
diff changeset
   124
            off += chunkLen;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    if (bufP != BUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        free(bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}