author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 4508 | 6675f4c35817 |
child 16866 | e659009ee08d |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1997, 2003, 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 |
*/ |
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"); |
|
4508
6675f4c35817
6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents:
2
diff
changeset
|
124 |
if (bufP != BUF) { |
6675f4c35817
6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents:
2
diff
changeset
|
125 |
free(bufP); |
6675f4c35817
6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents:
2
diff
changeset
|
126 |
} |
2 | 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 |
/* |
|
138 |
* Recv failed. |
|
139 |
*/ |
|
140 |
switch (WSAGetLastError()) { |
|
141 |
case WSAEINTR: |
|
142 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
143 |
"socket closed"); |
|
144 |
break; |
|
145 |
||
146 |
case WSAECONNRESET: |
|
147 |
case WSAESHUTDOWN: |
|
148 |
/* |
|
149 |
* Connection has been reset - Windows sometimes reports |
|
150 |
* the reset as a shutdown error. |
|
151 |
*/ |
|
152 |
JNU_ThrowByName(env, "sun/net/ConnectionResetException", |
|
153 |
""); |
|
154 |
break; |
|
155 |
||
156 |
case WSAETIMEDOUT : |
|
157 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException", |
|
158 |
"Read timed out"); |
|
159 |
break; |
|
160 |
||
161 |
default: |
|
162 |
NET_ThrowCurrent(env, "recv failed"); |
|
163 |
} |
|
164 |
} |
|
165 |
} |
|
166 |
if (bufP != BUF) { |
|
167 |
free(bufP); |
|
168 |
} |
|
169 |
return nread; |
|
170 |
} |