Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/java/net/SocketInputStream.c
32287 views
1
/*
2
* Copyright (c) 1997, 2013, 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 <windows.h>
27
#include <winsock2.h>
28
#include <ctype.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <malloc.h>
32
#include <sys/types.h>
33
34
#include "java_net_SocketInputStream.h"
35
36
#include "net_util.h"
37
#include "jni_util.h"
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: socketRead
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 *bufP;
66
char BUF[MAX_BUFFER_LEN];
67
jint fd, newfd;
68
jint nread;
69
70
if (IS_NULL(fdObj)) {
71
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
72
return -1;
73
}
74
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
75
if (fd == -1) {
76
NET_ThrowSocketException(env, "Socket closed");
77
return -1;
78
}
79
80
/*
81
* If the caller buffer is large than our stack buffer then we allocate
82
* from the heap (up to a limit). If memory is exhausted we always use
83
* the stack buffer.
84
*/
85
if (len <= MAX_BUFFER_LEN) {
86
bufP = BUF;
87
} else {
88
if (len > MAX_HEAP_BUFFER_LEN) {
89
len = MAX_HEAP_BUFFER_LEN;
90
}
91
bufP = (char *)malloc((size_t)len);
92
if (bufP == NULL) {
93
/* allocation failed so use stack buffer */
94
bufP = BUF;
95
len = MAX_BUFFER_LEN;
96
}
97
}
98
99
100
if (timeout) {
101
if (timeout <= 5000 || !isRcvTimeoutSupported) {
102
int ret = NET_Timeout (fd, timeout);
103
104
if (ret <= 0) {
105
if (ret == 0) {
106
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
107
"Read timed out");
108
} else if (ret == JVM_IO_ERR) {
109
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
110
} else if (ret == JVM_IO_INTR) {
111
JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
112
"Operation interrupted");
113
}
114
if (bufP != BUF) {
115
free(bufP);
116
}
117
return -1;
118
}
119
120
/*check if the socket has been closed while we were in timeout*/
121
newfd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
122
if (newfd == -1) {
123
NET_ThrowSocketException(env, "Socket Closed");
124
if (bufP != BUF) {
125
free(bufP);
126
}
127
return -1;
128
}
129
}
130
}
131
132
nread = recv(fd, bufP, len, 0);
133
if (nread > 0) {
134
(*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
135
} else {
136
if (nread < 0) {
137
int err = WSAGetLastError();
138
// Check if the socket has been closed since we last checked.
139
// This could be a reason for recv failing.
140
if ((*env)->GetIntField(env, fdObj, IO_fd_fdID) == -1) {
141
NET_ThrowSocketException(env, "Socket closed");
142
} else {
143
switch (err) {
144
case WSAEINTR:
145
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
146
"socket closed");
147
break;
148
149
case WSAECONNRESET:
150
case WSAESHUTDOWN:
151
/*
152
* Connection has been reset - Windows sometimes reports
153
* the reset as a shutdown error.
154
*/
155
JNU_ThrowByName(env, "sun/net/ConnectionResetException",
156
"");
157
break;
158
159
case WSAETIMEDOUT :
160
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
161
"Read timed out");
162
break;
163
164
default:
165
NET_ThrowCurrent(env, "recv failed");
166
}
167
}
168
}
169
}
170
if (bufP != BUF) {
171
free(bufP);
172
}
173
return nread;
174
}
175
176