1 /*
2 * Copyright (c) 1997, 2012, 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 "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
43 static jfieldID IO_fd_fdID;
44
45 /*
46 * Class: java_net_SocketInputStream
47 * Method: init
48 * Signature: ()V
49 */
50 JNIEXPORT void JNICALL
51 Java_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 */
60 JNIEXPORT jint JNICALL
61 Java_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
69 if (IS_NULL(fdObj)) {
70 /* shouldn't this be a NullPointerException? -br */
71 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
72 "Socket closed");
73 return -1;
74 } else {
75 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
76 /* Bug 4086704 - If the Socket associated with this file descriptor
77 * was closed (sysCloseFD), then the file descriptor is set to -1.
78 */
79 if (fd == -1) {
80 JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
81 return -1;
82 }
83 }
84
85 /*
86 * If the read is greater than our stack allocated buffer then
87 * we allocate from the heap (up to a limit)
88 */
89 if (len > MAX_BUFFER_LEN) {
90 if (len > MAX_HEAP_BUFFER_LEN) {
91 len = MAX_HEAP_BUFFER_LEN;
92 }
93 bufP = (char *)malloc((size_t)len);
94 if (bufP == NULL) {
95 bufP = BUF;
96 len = MAX_BUFFER_LEN;
97 }
98 } else {
99 bufP = BUF;
100 }
101
102 if (timeout) {
103 nread = NET_Timeout(fd, timeout);
104 if (nread <= 0) {
105 if (nread == 0) {
106 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
107 "Read timed out");
108 } else if (nread == -1) {
109 if (errno == EBADF) {
110 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
111 } else {
112 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
113 "select/poll failed");
114 }
115 }
116 if (bufP != BUF) {
117 free(bufP);
118 }
119 return -1;
120 }
121 }
122
123 nread = NET_Read(fd, bufP, len);
124
125 if (nread <= 0) {
126 if (nread < 0) {
127
128 switch (errno) {
129 case ECONNRESET:
130 case EPIPE:
131 JNU_ThrowByName(env, "sun/net/ConnectionResetException",
132 "Connection reset");
133 break;
134
135 case EBADF:
136 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
137 "Socket closed");
138 break;
139
140 case EINTR:
141 JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
142 "Operation interrupted");
143 break;
144
145 default:
146 NET_ThrowByNameWithLastError(env,
147 JNU_JAVANETPKG "SocketException", "Read failed");
148 }
149 }
150 } else {
151 (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
152 }
153
154 if (bufP != BUF) {
155 free(bufP);
156 }
157 return nread;
158 }
--- EOF ---