author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 43202 | jdk/src/java.base/unix/native/libnet/SocketOutputStream.c@29180c8db039 |
child 50425 | 43b54a307c89 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
39318
2006d1d41c8b
8158023: SocketExceptions contain too little information sometimes
clanger
parents:
28059
diff
changeset
|
2 |
* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
39318
diff
changeset
|
25 |
#include <errno.h> |
2 | 26 |
#include <stdlib.h> |
27 |
#include <string.h> |
|
28 |
||
29 |
#include "net_util.h" |
|
30 |
||
31 |
#include "java_net_SocketOutputStream.h" |
|
32 |
||
33 |
#define min(a, b) ((a) < (b) ? (a) : (b)) |
|
34 |
||
35 |
/* |
|
36 |
* SocketOutputStream |
|
37 |
*/ |
|
38 |
||
39 |
static jfieldID IO_fd_fdID; |
|
40 |
||
41 |
/* |
|
42 |
* Class: java_net_SocketOutputStream |
|
43 |
* Method: init |
|
44 |
* Signature: ()V |
|
45 |
*/ |
|
46 |
JNIEXPORT void JNICALL |
|
47 |
Java_java_net_SocketOutputStream_init(JNIEnv *env, jclass cls) { |
|
48 |
IO_fd_fdID = NET_GetFileDescriptorID(env); |
|
49 |
} |
|
50 |
||
51 |
/* |
|
52 |
* Class: java_net_SocketOutputStream |
|
53 |
* Method: socketWrite0 |
|
54 |
* Signature: (Ljava/io/FileDescriptor;[BII)V |
|
55 |
*/ |
|
56 |
JNIEXPORT void JNICALL |
|
57 |
Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this, |
|
58 |
jobject fdObj, |
|
59 |
jbyteArray data, |
|
60 |
jint off, jint len) { |
|
61 |
char *bufP; |
|
62 |
char BUF[MAX_BUFFER_LEN]; |
|
63 |
int buflen; |
|
64 |
int fd; |
|
65 |
||
66 |
if (IS_NULL(fdObj)) { |
|
67 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
|
68 |
return; |
|
69 |
} else { |
|
70 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
71 |
/* Bug 4086704 - If the Socket associated with this file descriptor |
|
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
25859
diff
changeset
|
72 |
* was closed (sysCloseFD), the file descriptor is set to -1. |
2 | 73 |
*/ |
74 |
if (fd == -1) { |
|
75 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
|
76 |
return; |
|
77 |
} |
|
78 |
||
79 |
} |
|
80 |
||
81 |
if (len <= MAX_BUFFER_LEN) { |
|
82 |
bufP = BUF; |
|
83 |
buflen = MAX_BUFFER_LEN; |
|
84 |
} else { |
|
85 |
buflen = min(MAX_HEAP_BUFFER_LEN, len); |
|
86 |
bufP = (char *)malloc((size_t)buflen); |
|
87 |
||
88 |
/* if heap exhausted resort to stack buffer */ |
|
89 |
if (bufP == NULL) { |
|
90 |
bufP = BUF; |
|
91 |
buflen = MAX_BUFFER_LEN; |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
while(len > 0) { |
|
96 |
int loff = 0; |
|
97 |
int chunkLen = min(buflen, len); |
|
98 |
int llen = chunkLen; |
|
99 |
(*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP); |
|
100 |
||
43202 | 101 |
if ((*env)->ExceptionCheck(env)) { |
102 |
break; |
|
103 |
} else { |
|
104 |
while(llen > 0) { |
|
105 |
int n = NET_Send(fd, bufP + loff, llen, 0); |
|
106 |
if (n > 0) { |
|
107 |
llen -= n; |
|
108 |
loff += n; |
|
109 |
continue; |
|
110 |
} |
|
111 |
if (errno == ECONNRESET) { |
|
112 |
JNU_ThrowByName(env, "sun/net/ConnectionResetException", |
|
113 |
"Connection reset"); |
|
114 |
} else { |
|
115 |
JNU_ThrowByNameWithMessageAndLastError |
|
116 |
(env, "java/net/SocketException", "Write failed"); |
|
117 |
} |
|
118 |
if (bufP != BUF) { |
|
119 |
free(bufP); |
|
120 |
} |
|
121 |
return; |
|
2 | 122 |
} |
43202 | 123 |
len -= chunkLen; |
124 |
off += chunkLen; |
|
2 | 125 |
} |
126 |
} |
|
127 |
||
128 |
if (bufP != BUF) { |
|
129 |
free(bufP); |
|
130 |
} |
|
131 |
} |