author | lana |
Fri, 19 May 2017 04:17:50 +0000 | |
changeset 45184 | d577bc42a1f0 |
parent 44534 | a076dffbc2c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
44534 | 2 |
* Copyright (c) 1995, 2017, 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 |
package java.net; |
|
27 |
||
28 |
import java.io.FileDescriptor; |
|
29 |
import java.io.FileInputStream; |
|
30 |
import java.io.IOException; |
|
31 |
import java.nio.channels.FileChannel; |
|
32 |
||
33 |
import sun.net.ConnectionResetException; |
|
34 |
||
35 |
/** |
|
36 |
* This stream extends FileInputStream to implement a |
|
37 |
* SocketInputStream. Note that this class should <b>NOT</b> be |
|
38 |
* public. |
|
39 |
* |
|
40 |
* @author Jonathan Payne |
|
41 |
* @author Arthur van Hoff |
|
42 |
*/ |
|
43 |
class SocketInputStream extends FileInputStream |
|
44 |
{ |
|
45 |
static { |
|
46 |
init(); |
|
47 |
} |
|
48 |
||
49 |
private boolean eof; |
|
50 |
private AbstractPlainSocketImpl impl = null; |
|
51 |
private byte temp[]; |
|
52 |
private Socket socket = null; |
|
53 |
||
54 |
/** |
|
55 |
* Creates a new SocketInputStream. Can only be called |
|
56 |
* by a Socket. This method needs to hang on to the owner Socket so |
|
57 |
* that the fd will not be closed. |
|
58 |
* @param impl the implemented socket input stream |
|
59 |
*/ |
|
60 |
SocketInputStream(AbstractPlainSocketImpl impl) throws IOException { |
|
61 |
super(impl.getFileDescriptor()); |
|
62 |
this.impl = impl; |
|
63 |
socket = impl.getSocket(); |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* Returns the unique {@link java.nio.channels.FileChannel FileChannel} |
|
68 |
* object associated with this file input stream.</p> |
|
69 |
* |
|
19069 | 70 |
* The {@code getChannel} method of {@code SocketInputStream} |
71 |
* returns {@code null} since it is a socket based stream.</p> |
|
2 | 72 |
* |
73 |
* @return the file channel associated with this file input stream |
|
74 |
* |
|
75 |
* @since 1.4 |
|
76 |
* @spec JSR-51 |
|
77 |
*/ |
|
78 |
public final FileChannel getChannel() { |
|
79 |
return null; |
|
80 |
} |
|
81 |
||
82 |
/** |
|
83 |
* Reads into an array of bytes at the specified offset using |
|
84 |
* the received socket primitive. |
|
85 |
* @param fd the FileDescriptor |
|
86 |
* @param b the buffer into which the data is read |
|
87 |
* @param off the start offset of the data |
|
88 |
* @param len the maximum number of bytes read |
|
89 |
* @param timeout the read timeout in ms |
|
90 |
* @return the actual number of bytes read, -1 is |
|
91 |
* returned when the end of the stream is reached. |
|
92 |
* @exception IOException If an I/O error has occurred. |
|
93 |
*/ |
|
94 |
private native int socketRead0(FileDescriptor fd, |
|
95 |
byte b[], int off, int len, |
|
96 |
int timeout) |
|
97 |
throws IOException; |
|
98 |
||
26190
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
99 |
// wrap native call to allow instrumentation |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
100 |
/** |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
101 |
* Reads into an array of bytes at the specified offset using |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
102 |
* the received socket primitive. |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
103 |
* @param fd the FileDescriptor |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
104 |
* @param b the buffer into which the data is read |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
105 |
* @param off the start offset of the data |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
106 |
* @param len the maximum number of bytes read |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
107 |
* @param timeout the read timeout in ms |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
108 |
* @return the actual number of bytes read, -1 is |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
109 |
* returned when the end of the stream is reached. |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
110 |
* @exception IOException If an I/O error has occurred. |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
111 |
*/ |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
112 |
private int socketRead(FileDescriptor fd, |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
113 |
byte b[], int off, int len, |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
114 |
int timeout) |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
115 |
throws IOException { |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
116 |
return socketRead0(fd, b, off, len, timeout); |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
117 |
} |
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
118 |
|
2 | 119 |
/** |
120 |
* Reads into a byte array data from the socket. |
|
121 |
* @param b the buffer into which the data is read |
|
122 |
* @return the actual number of bytes read, -1 is |
|
123 |
* returned when the end of the stream is reached. |
|
124 |
* @exception IOException If an I/O error has occurred. |
|
125 |
*/ |
|
126 |
public int read(byte b[]) throws IOException { |
|
127 |
return read(b, 0, b.length); |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* Reads into a byte array <i>b</i> at offset <i>off</i>, |
|
132 |
* <i>length</i> bytes of data. |
|
133 |
* @param b the buffer into which the data is read |
|
134 |
* @param off the start offset of the data |
|
14014 | 135 |
* @param length the maximum number of bytes read |
2 | 136 |
* @return the actual number of bytes read, -1 is |
137 |
* returned when the end of the stream is reached. |
|
138 |
* @exception IOException If an I/O error has occurred. |
|
139 |
*/ |
|
140 |
public int read(byte b[], int off, int length) throws IOException { |
|
5147
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
2
diff
changeset
|
141 |
return read(b, off, length, impl.getTimeout()); |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
2
diff
changeset
|
142 |
} |
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
2
diff
changeset
|
143 |
|
96642e83ad41
6223635: Code hangs at connect call even when Timeout is specified when using a socks proxy
chegar
parents:
2
diff
changeset
|
144 |
int read(byte b[], int off, int length, int timeout) throws IOException { |
2 | 145 |
int n; |
146 |
||
147 |
// EOF already encountered |
|
148 |
if (eof) { |
|
149 |
return -1; |
|
150 |
} |
|
151 |
||
152 |
// connection reset |
|
153 |
if (impl.isConnectionReset()) { |
|
154 |
throw new SocketException("Connection reset"); |
|
155 |
} |
|
156 |
||
157 |
// bounds check |
|
43202 | 158 |
if (length <= 0 || off < 0 || length > b.length - off) { |
2 | 159 |
if (length == 0) { |
160 |
return 0; |
|
161 |
} |
|
43202 | 162 |
throw new ArrayIndexOutOfBoundsException("length == " + length |
163 |
+ " off == " + off + " buffer length == " + b.length); |
|
2 | 164 |
} |
165 |
||
166 |
boolean gotReset = false; |
|
167 |
||
168 |
// acquire file descriptor and do the read |
|
169 |
FileDescriptor fd = impl.acquireFD(); |
|
170 |
try { |
|
26190
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
171 |
n = socketRead(fd, b, off, length, timeout); |
2 | 172 |
if (n > 0) { |
173 |
return n; |
|
174 |
} |
|
175 |
} catch (ConnectionResetException rstExc) { |
|
176 |
gotReset = true; |
|
177 |
} finally { |
|
178 |
impl.releaseFD(); |
|
179 |
} |
|
180 |
||
181 |
/* |
|
182 |
* We receive a "connection reset" but there may be bytes still |
|
183 |
* buffered on the socket |
|
184 |
*/ |
|
185 |
if (gotReset) { |
|
186 |
impl.setConnectionResetPending(); |
|
187 |
impl.acquireFD(); |
|
188 |
try { |
|
26190
d183677673d9
8054720: Modifications of I/O methods for instrumentation purposes
bpb
parents:
25859
diff
changeset
|
189 |
n = socketRead(fd, b, off, length, timeout); |
2 | 190 |
if (n > 0) { |
191 |
return n; |
|
192 |
} |
|
193 |
} catch (ConnectionResetException rstExc) { |
|
194 |
} finally { |
|
195 |
impl.releaseFD(); |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
/* |
|
200 |
* If we get here we are at EOF, the socket has been closed, |
|
201 |
* or the connection has been reset. |
|
202 |
*/ |
|
203 |
if (impl.isClosedOrPending()) { |
|
204 |
throw new SocketException("Socket closed"); |
|
205 |
} |
|
206 |
if (impl.isConnectionResetPending()) { |
|
207 |
impl.setConnectionReset(); |
|
208 |
} |
|
209 |
if (impl.isConnectionReset()) { |
|
210 |
throw new SocketException("Connection reset"); |
|
211 |
} |
|
212 |
eof = true; |
|
213 |
return -1; |
|
214 |
} |
|
215 |
||
216 |
/** |
|
217 |
* Reads a single byte from the socket. |
|
218 |
*/ |
|
219 |
public int read() throws IOException { |
|
220 |
if (eof) { |
|
221 |
return -1; |
|
222 |
} |
|
223 |
temp = new byte[1]; |
|
224 |
int n = read(temp, 0, 1); |
|
225 |
if (n <= 0) { |
|
226 |
return -1; |
|
227 |
} |
|
228 |
return temp[0] & 0xff; |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Skips n bytes of input. |
|
14014 | 233 |
* @param numbytes the number of bytes to skip |
2 | 234 |
* @return the actual number of bytes skipped. |
235 |
* @exception IOException If an I/O error has occurred. |
|
236 |
*/ |
|
237 |
public long skip(long numbytes) throws IOException { |
|
238 |
if (numbytes <= 0) { |
|
239 |
return 0; |
|
240 |
} |
|
241 |
long n = numbytes; |
|
242 |
int buflen = (int) Math.min(1024, n); |
|
243 |
byte data[] = new byte[buflen]; |
|
244 |
while (n > 0) { |
|
245 |
int r = read(data, 0, (int) Math.min((long) buflen, n)); |
|
246 |
if (r < 0) { |
|
247 |
break; |
|
248 |
} |
|
249 |
n -= r; |
|
250 |
} |
|
251 |
return numbytes - n; |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* Returns the number of bytes that can be read without blocking. |
|
256 |
* @return the number of immediately available bytes |
|
257 |
*/ |
|
258 |
public int available() throws IOException { |
|
259 |
return impl.available(); |
|
260 |
} |
|
261 |
||
262 |
/** |
|
263 |
* Closes the stream. |
|
264 |
*/ |
|
265 |
private boolean closing = false; |
|
266 |
public void close() throws IOException { |
|
267 |
// Prevent recursion. See BugId 4484411 |
|
268 |
if (closing) |
|
269 |
return; |
|
270 |
closing = true; |
|
271 |
if (socket != null) { |
|
272 |
if (!socket.isClosed()) |
|
273 |
socket.close(); |
|
274 |
} else |
|
275 |
impl.close(); |
|
276 |
closing = false; |
|
277 |
} |
|
278 |
||
279 |
void setEOF(boolean eof) { |
|
280 |
this.eof = eof; |
|
281 |
} |
|
282 |
||
283 |
/** |
|
284 |
* Overrides finalize, the fd is closed by the Socket. |
|
285 |
*/ |
|
44534 | 286 |
@SuppressWarnings("deprecation") |
2 | 287 |
protected void finalize() {} |
288 |
||
289 |
/** |
|
290 |
* Perform class load-time initializations. |
|
291 |
*/ |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
26190
diff
changeset
|
292 |
private static native void init(); |
2 | 293 |
} |