jdk/src/solaris/native/java/net/SocketInputStream.c
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 910 1f53246fb014
permissions -rw-r--r--
Initial load
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 "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "jni_util.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_SocketInputStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Class:     java_net_SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Class:     java_net_SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Method:    socketRead0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Signature: (Ljava/io/FileDescriptor;[BIII)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                            jobject fdObj, jbyteArray data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                            jint off, jint len, jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    char *bufP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    jint fd, nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    jint n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        /* should't this be a NullPointerException? -br */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        /* Bug 4086704 - If the Socket associated with this file descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         * was closed (sysCloseFD), the the file descriptor is set to -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            return -1;
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
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * If the read is greater than our stack allocated buffer then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * we allocate from the heap (up to a limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    if (len > MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (len > MAX_HEAP_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            len = MAX_HEAP_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        bufP = (char *)malloc((size_t)len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (bufP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            len = MAX_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        nread = NET_Timeout(fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (nread <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (nread == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                            "Read timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } else if (nread == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                if (errno == EBADF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                     JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                     NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                                  "select/poll failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            } else if (nread == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                            "Operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            if (bufP != BUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                free(bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    nread = NET_Read(fd, bufP, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    if (nread <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (nread < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            switch (errno) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                case ECONNRESET:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                case EPIPE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    JNU_ThrowByName(env, "sun/net/ConnectionResetException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        "Connection reset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                case EBADF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                case EINTR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                     JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                           "Operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                     break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    NET_ThrowByNameWithLastError(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        JNU_JAVANETPKG "SocketException", "Read failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    if (bufP != BUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        free(bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    return nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
}