Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/java/net/SocketInputStream.c
32287 views
1
/*
2
* Copyright (c) 1997, 2016, 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
#if !defined(__solaris__)
56
static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) {
57
int result = 0;
58
long prevtime = NET_GetCurrentTime(), newtime;
59
while (timeout > 0) {
60
result = NET_TimeoutWithCurrentTime(fd, timeout, prevtime);
61
if (result <= 0) {
62
if (result == 0) {
63
JNU_ThrowByName(env, "java/net/SocketTimeoutException", "Read timed out");
64
} else if (result == -1) {
65
if (errno == EBADF) {
66
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
67
} else if (errno == ENOMEM) {
68
JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
69
} else {
70
JNU_ThrowByNameWithMessageAndLastError
71
(env, "java/net/SocketException", "select/poll failed");
72
}
73
}
74
return -1;
75
}
76
result = NET_NonBlockingRead(fd, bufP, len);
77
if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) {
78
newtime = NET_GetCurrentTime();
79
timeout -= newtime - prevtime;
80
if (timeout > 0) {
81
prevtime = newtime;
82
}
83
} else {
84
break;
85
}
86
}
87
return result;
88
}
89
#endif
90
91
/*
92
* Class: java_net_SocketInputStream
93
* Method: socketRead0
94
* Signature: (Ljava/io/FileDescriptor;[BIII)I
95
*/
96
JNIEXPORT jint JNICALL
97
Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
98
jobject fdObj, jbyteArray data,
99
jint off, jint len, jint timeout)
100
{
101
char BUF[MAX_BUFFER_LEN];
102
char *bufP;
103
jint fd, nread;
104
105
if (IS_NULL(fdObj)) {
106
/* shouldn't this be a NullPointerException? -br */
107
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
108
"Socket closed");
109
return -1;
110
} else {
111
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
112
/* Bug 4086704 - If the Socket associated with this file descriptor
113
* was closed (sysCloseFD), then the file descriptor is set to -1.
114
*/
115
if (fd == -1) {
116
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
117
return -1;
118
}
119
}
120
121
/*
122
* If the read is greater than our stack allocated buffer then
123
* we allocate from the heap (up to a limit)
124
*/
125
if (len > MAX_BUFFER_LEN) {
126
if (len > MAX_HEAP_BUFFER_LEN) {
127
len = MAX_HEAP_BUFFER_LEN;
128
}
129
bufP = (char *)malloc((size_t)len);
130
if (bufP == NULL) {
131
bufP = BUF;
132
len = MAX_BUFFER_LEN;
133
}
134
} else {
135
bufP = BUF;
136
}
137
138
#if defined(__solaris__)
139
if (timeout) {
140
nread = NET_Timeout(fd, timeout);
141
if (nread <= 0) {
142
if (nread == 0) {
143
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
144
"Read timed out");
145
} else if (nread == JVM_IO_ERR) {
146
if (errno == EBADF) {
147
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
148
} else if (errno == ENOMEM) {
149
JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
150
} else {
151
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException",
152
"select/poll failed");
153
}
154
} else if (nread == JVM_IO_INTR) {
155
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
156
"Operation interrupted");
157
}
158
if (bufP != BUF) {
159
free(bufP);
160
}
161
return -1;
162
}
163
}
164
165
nread = NET_Read(fd, bufP, len);
166
#else
167
if (timeout) {
168
nread = NET_ReadWithTimeout(env, fd, bufP, len, timeout);
169
if ((*env)->ExceptionCheck(env)) {
170
if (bufP != BUF) {
171
free(bufP);
172
}
173
return nread;
174
}
175
} else {
176
nread = NET_Read(fd, bufP, len);
177
}
178
#endif
179
if (nread <= 0) {
180
if (nread < 0) {
181
182
switch (errno) {
183
case ECONNRESET:
184
case EPIPE:
185
JNU_ThrowByName(env, "sun/net/ConnectionResetException",
186
"Connection reset");
187
break;
188
189
case EBADF:
190
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
191
"Socket closed");
192
break;
193
194
case EINTR:
195
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
196
"Operation interrupted");
197
break;
198
199
default:
200
NET_ThrowByNameWithLastError(env,
201
JNU_JAVANETPKG "SocketException", "Read failed");
202
}
203
}
204
} else {
205
(*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
206
}
207
208
if (bufP != BUF) {
209
free(bufP);
210
}
211
return nread;
212
}
213
214