blob: c343accb8c797504ce909ec9644e59bf6bb3fbc1 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2002 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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 "jvm.h"
33#include "jni_util.h"
34#include "net_util.h"
35
36#include "java_net_SocketInputStream.h"
37
38
39/************************************************************************
40 * SocketInputStream
41 */
42
43static jfieldID IO_fd_fdID;
44
45/*
46 * Class: java_net_SocketInputStream
47 * Method: init
48 * Signature: ()V
49 */
50JNIEXPORT void JNICALL
51Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) {
52 IO_fd_fdID = NET_GetFileDescriptorID(env);
53}
54
55/*
56 * Class: java_net_SocketInputStream
57 * Method: socketRead0
58 * Signature: (Ljava/io/FileDescriptor;[BIII)I
59 */
60JNIEXPORT jint JNICALL
61Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
62 jobject fdObj, jbyteArray data,
63 jint off, jint len, jint timeout)
64{
65 char BUF[MAX_BUFFER_LEN];
66 char *bufP;
67 jint fd, nread;
68 jint n;
69
70 if (IS_NULL(fdObj)) {
71 /* should't this be a NullPointerException? -br */
72 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
73 "Socket closed");
74 return -1;
75 } else {
76 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
77 /* Bug 4086704 - If the Socket associated with this file descriptor
78 * was closed (sysCloseFD), the the file descriptor is set to -1.
79 */
80 if (fd == -1) {
81 JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
82 return -1;
83 }
84 }
85
86 /*
87 * If the read is greater than our stack allocated buffer then
88 * we allocate from the heap (up to a limit)
89 */
90 if (len > MAX_BUFFER_LEN) {
91 if (len > MAX_HEAP_BUFFER_LEN) {
92 len = MAX_HEAP_BUFFER_LEN;
93 }
94 bufP = (char *)malloc((size_t)len);
95 if (bufP == NULL) {
96 bufP = BUF;
97 len = MAX_BUFFER_LEN;
98 }
99 } else {
100 bufP = BUF;
101 }
102
103 if (timeout) {
104 nread = NET_Timeout(fd, timeout);
105 if (nread <= 0) {
106 if (nread == 0) {
107 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
108 "Read timed out");
109 } else if (nread == JVM_IO_ERR) {
110 if (errno == EBADF) {
111 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
112 } else {
113 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
114 "select/poll failed");
115 }
116 } else if (nread == JVM_IO_INTR) {
117 JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
118 "Operation interrupted");
119 }
120 if (bufP != BUF) {
121 free(bufP);
122 }
123 return -1;
124 }
125 }
126
127 nread = NET_Read(fd, bufP, len);
128
129 if (nread <= 0) {
130 if (nread < 0) {
131
132 switch (errno) {
133 case ECONNRESET:
134 case EPIPE:
135 JNU_ThrowByName(env, "sun/net/ConnectionResetException",
136 "Connection reset");
137 break;
138
139 case EBADF:
140 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
141 "Socket closed");
142 break;
143
144 case EINTR:
145 JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
146 "Operation interrupted");
147 break;
148
149 default:
150 NET_ThrowByNameWithLastError(env,
151 JNU_JAVANETPKG "SocketException", "Read failed");
152 }
153 }
154 } else {
155 (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
156 }
157
158 if (bufP != BUF) {
159 free(bufP);
160 }
161 return nread;
162}