blob: 54c9a8a88feff33e662f870f0a183cb4a316bf96 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2003 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any 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
43static jfieldID IO_fd_fdID;
44
45/*
46 * Class: java_net_SocketInputStream
47 * Method: init
48 * Signature: ()V
49 */
50JNIEXPORT void JNICALL
51Java_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 */
60JNIEXPORT jint JNICALL
61Java_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 return -1;
125 }
126 }
127 }
128
129 nread = recv(fd, bufP, len, 0);
130 if (nread > 0) {
131 (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
132 } else {
133 if (nread < 0) {
134 /*
135 * Recv failed.
136 */
137 switch (WSAGetLastError()) {
138 case WSAEINTR:
139 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
140 "socket closed");
141 break;
142
143 case WSAECONNRESET:
144 case WSAESHUTDOWN:
145 /*
146 * Connection has been reset - Windows sometimes reports
147 * the reset as a shutdown error.
148 */
149 JNU_ThrowByName(env, "sun/net/ConnectionResetException",
150 "");
151 break;
152
153 case WSAETIMEDOUT :
154 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
155 "Read timed out");
156 break;
157
158 default:
159 NET_ThrowCurrent(env, "recv failed");
160 }
161 }
162 }
163 if (bufP != BUF) {
164 free(bufP);
165 }
166 return nread;
167}