author | dfuchs |
Fri, 30 Aug 2019 15:42:27 +0100 | |
branch | JDK-8229867-branch |
changeset 57968 | 8595871a5446 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57968 | 2 |
* Copyright (c) 1996, 2019, 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 sun.net.www.http; |
|
27 |
||
28 |
import java.io.*; |
|
57968 | 29 |
import java.util.concurrent.locks.ReentrantLock; |
2 | 30 |
import sun.net.ProgressSource; |
31 |
import sun.net.www.MeteredStream; |
|
35786
9d26db88aa14
8149653: Move sun.misc.InnocuousThread to jdk.internal.misc
chegar
parents:
29920
diff
changeset
|
32 |
import jdk.internal.misc.InnocuousThread; |
2 | 33 |
|
34 |
/** |
|
35 |
* A stream that has the property of being able to be kept alive for |
|
36 |
* multiple downloads from the same server. |
|
37 |
* |
|
38 |
* @author Stephen R. Pietrowicz (NCSA) |
|
39 |
* @author Dave Brown |
|
40 |
*/ |
|
41 |
public |
|
42 |
class KeepAliveStream extends MeteredStream implements Hurryable { |
|
43 |
||
44 |
// instance variables |
|
45 |
HttpClient hc; |
|
46 |
||
47 |
boolean hurried; |
|
48 |
||
49 |
// has this KeepAliveStream been put on the queue for asynchronous cleanup. |
|
57968 | 50 |
protected volatile boolean queuedForCleanup = false; |
2 | 51 |
|
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
52 |
private static final KeepAliveStreamCleaner queue = new KeepAliveStreamCleaner(); |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
53 |
private static Thread cleanerThread; // null |
2 | 54 |
|
55 |
/** |
|
56 |
* Constructor |
|
57 |
*/ |
|
58 |
public KeepAliveStream(InputStream is, ProgressSource pi, long expected, HttpClient hc) { |
|
59 |
super(is, pi, expected); |
|
60 |
this.hc = hc; |
|
61 |
} |
|
62 |
||
57968 | 63 |
final ReentrantLock readLock() { |
64 |
return readLock; |
|
65 |
} |
|
66 |
||
2 | 67 |
/** |
68 |
* Attempt to cache this connection |
|
69 |
*/ |
|
70 |
public void close() throws IOException { |
|
57968 | 71 |
// If the inputstream is closed already, or if this stream |
72 |
// has already been queued for cleanup.just return. |
|
73 |
if (closed || queuedForCleanup) return; |
|
2 | 74 |
|
75 |
// Skip past the data that's left in the Inputstream because |
|
76 |
// some sort of error may have occurred. |
|
77 |
// Do this ONLY if the skip won't block. The stream may have |
|
78 |
// been closed at the beginning of a big file and we don't want |
|
79 |
// to hang around for nothing. So if we can't skip without blocking |
|
80 |
// we just close the socket and, therefore, terminate the keepAlive |
|
81 |
// NOTE: Don't close super class |
|
57968 | 82 |
// For consistency, access to `expected` and `count` should be |
83 |
// protected by readLock |
|
84 |
readLock.lock(); |
|
2 | 85 |
try { |
57968 | 86 |
if (closed || queuedForCleanup) return; |
87 |
try { |
|
88 |
if (expected > count) { |
|
89 |
long nskip = expected - count; |
|
90 |
if (nskip <= available()) { |
|
91 |
do { |
|
92 |
} while ((nskip = (expected - count)) > 0L |
|
93 |
&& skip(Math.min(nskip, available())) > 0L); |
|
94 |
} else if (expected <= KeepAliveStreamCleaner.MAX_DATA_REMAINING && !hurried) { |
|
95 |
//put this KeepAliveStream on the queue so that the data remaining |
|
96 |
//on the socket can be cleanup asyncronously. |
|
97 |
queueForCleanup(new KeepAliveCleanerEntry(this, hc)); |
|
98 |
} else { |
|
99 |
hc.closeServer(); |
|
100 |
} |
|
101 |
} |
|
102 |
if (!closed && !hurried && !queuedForCleanup) { |
|
103 |
hc.finished(); |
|
104 |
} |
|
105 |
} finally { |
|
106 |
if (pi != null) |
|
107 |
pi.finishTracking(); |
|
108 |
||
109 |
if (!queuedForCleanup) { |
|
110 |
// nulling out the underlying inputstream as well as |
|
111 |
// httpClient to let gc collect the memories faster |
|
112 |
in = null; |
|
113 |
hc = null; |
|
114 |
closed = true; |
|
2 | 115 |
} |
116 |
} |
|
117 |
} finally { |
|
57968 | 118 |
readLock.unlock(); |
2 | 119 |
} |
120 |
} |
|
121 |
||
122 |
/* we explicitly do not support mark/reset */ |
|
123 |
||
124 |
public boolean markSupported() { |
|
125 |
return false; |
|
126 |
} |
|
127 |
||
128 |
public void mark(int limit) {} |
|
129 |
||
130 |
public void reset() throws IOException { |
|
131 |
throw new IOException("mark/reset not supported"); |
|
132 |
} |
|
133 |
||
57968 | 134 |
public boolean hurry() { |
135 |
readLock.lock(); |
|
2 | 136 |
try { |
137 |
/* CASE 0: we're actually already done */ |
|
138 |
if (closed || count >= expected) { |
|
139 |
return false; |
|
140 |
} else if (in.available() < (expected - count)) { |
|
141 |
/* CASE I: can't meet the demand */ |
|
142 |
return false; |
|
143 |
} else { |
|
144 |
/* CASE II: fill our internal buffer |
|
145 |
* Remind: possibly check memory here |
|
146 |
*/ |
|
147 |
int size = (int) (expected - count); |
|
148 |
byte[] buf = new byte[size]; |
|
149 |
DataInputStream dis = new DataInputStream(in); |
|
150 |
dis.readFully(buf); |
|
151 |
in = new ByteArrayInputStream(buf); |
|
152 |
hurried = true; |
|
153 |
return true; |
|
154 |
} |
|
155 |
} catch (IOException e) { |
|
156 |
// e.printStackTrace(); |
|
157 |
return false; |
|
57968 | 158 |
} finally { |
159 |
readLock.unlock(); |
|
2 | 160 |
} |
161 |
} |
|
162 |
||
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
163 |
private static void queueForCleanup(KeepAliveCleanerEntry kace) { |
57968 | 164 |
queue.queueLock.lock(); |
165 |
try { |
|
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
166 |
if(!kace.getQueuedForCleanup()) { |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
167 |
if (!queue.offer(kace)) { |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
168 |
kace.getHttpClient().closeServer(); |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
169 |
return; |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
170 |
} |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
171 |
|
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
172 |
kace.setQueuedForCleanup(); |
57968 | 173 |
queue.waiter.signalAll(); |
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
174 |
} |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
175 |
|
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
176 |
boolean startCleanupThread = (cleanerThread == null); |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
177 |
if (!startCleanupThread) { |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
178 |
if (!cleanerThread.isAlive()) { |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
179 |
startCleanupThread = true; |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
180 |
} |
2 | 181 |
} |
182 |
||
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
183 |
if (startCleanupThread) { |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
184 |
java.security.AccessController.doPrivileged( |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
185 |
new java.security.PrivilegedAction<Void>() { |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
186 |
public Void run() { |
36680
c9fe8f8e7e6f
8151571: InnocuousThread cannot be created during early startup
mchung
parents:
35786
diff
changeset
|
187 |
cleanerThread = InnocuousThread.newSystemThread("Keep-Alive-SocketCleaner", queue); |
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
188 |
cleanerThread.setDaemon(true); |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
189 |
cleanerThread.setPriority(Thread.MAX_PRIORITY - 2); |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
190 |
cleanerThread.start(); |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
191 |
return null; |
2 | 192 |
} |
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
193 |
}); |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
194 |
} |
57968 | 195 |
} finally { |
196 |
queue.queueLock.unlock(); |
|
197 |
} |
|
2 | 198 |
} |
199 |
||
200 |
protected long remainingToRead() { |
|
57968 | 201 |
assert readLock.isHeldByCurrentThread(); |
2 | 202 |
return expected - count; |
203 |
} |
|
204 |
||
205 |
protected void setClosed() { |
|
57968 | 206 |
assert readLock.isHeldByCurrentThread(); |
2 | 207 |
in = null; |
208 |
hc = null; |
|
209 |
closed = true; |
|
210 |
} |
|
211 |
} |