Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/sun/net/www/http/KeepAliveStream.java
41161 views
1
/*
2
* Copyright (c) 1996, 2021, Oracle and/or its affiliates. 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. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle 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 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.
24
*/
25
26
package sun.net.www.http;
27
28
import java.io.*;
29
30
import sun.net.ProgressSource;
31
import sun.net.www.MeteredStream;
32
import jdk.internal.misc.InnocuousThread;
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.
50
// This flag is read from within KeepAliveCleanerEntry outside of any lock.
51
protected volatile boolean queuedForCleanup = false;
52
53
private static final KeepAliveStreamCleaner queue = new KeepAliveStreamCleaner();
54
private static Thread cleanerThread; // null
55
56
/**
57
* Constructor
58
*/
59
public KeepAliveStream(InputStream is, ProgressSource pi, long expected, HttpClient hc) {
60
super(is, pi, expected);
61
this.hc = hc;
62
}
63
64
/**
65
* Attempt to cache this connection
66
*/
67
public void close() throws IOException {
68
// If the inputstream is queued for cleanup, just return.
69
if (queuedForCleanup) return;
70
71
// Skip past the data that's left in the Inputstream because
72
// some sort of error may have occurred.
73
// Do this ONLY if the skip won't block. The stream may have
74
// been closed at the beginning of a big file and we don't want
75
// to hang around for nothing. So if we can't skip without blocking
76
// we just close the socket and, therefore, terminate the keepAlive
77
// NOTE: Don't close super class
78
// For consistency, access to `expected` and `count` should be
79
// protected by readLock
80
lock();
81
try {
82
// If the inputstream is closed already, or if this stream
83
// has already been queued for cleanup, just return.
84
if (closed || queuedForCleanup) return;
85
try {
86
if (expected > count) {
87
long nskip = expected - count;
88
if (nskip <= available()) {
89
do {
90
} while ((nskip = (expected - count)) > 0L
91
&& skip(Math.min(nskip, available())) > 0L);
92
} else if (expected <= KeepAliveStreamCleaner.MAX_DATA_REMAINING && !hurried) {
93
//put this KeepAliveStream on the queue so that the data remaining
94
//on the socket can be cleanup asyncronously.
95
queueForCleanup(new KeepAliveCleanerEntry(this, hc));
96
} else {
97
hc.closeServer();
98
}
99
}
100
if (!closed && !hurried && !queuedForCleanup) {
101
hc.finished();
102
}
103
} finally {
104
if (pi != null)
105
pi.finishTracking();
106
107
if (!queuedForCleanup) {
108
// nulling out the underlying inputstream as well as
109
// httpClient to let gc collect the memories faster
110
in = null;
111
hc = null;
112
closed = true;
113
}
114
}
115
} finally {
116
unlock();
117
}
118
}
119
120
/* we explicitly do not support mark/reset */
121
122
public boolean markSupported() {
123
return false;
124
}
125
126
public void mark(int limit) {}
127
128
public void reset() throws IOException {
129
throw new IOException("mark/reset not supported");
130
}
131
132
public boolean hurry() {
133
lock();
134
try {
135
/* CASE 0: we're actually already done */
136
if (closed || count >= expected) {
137
return false;
138
} else if (in.available() < (expected - count)) {
139
/* CASE I: can't meet the demand */
140
return false;
141
} else {
142
/* CASE II: fill our internal buffer
143
* Remind: possibly check memory here
144
*/
145
int size = (int) (expected - count);
146
byte[] buf = new byte[size];
147
DataInputStream dis = new DataInputStream(in);
148
dis.readFully(buf);
149
in = new ByteArrayInputStream(buf);
150
hurried = true;
151
return true;
152
}
153
} catch (IOException e) {
154
// e.printStackTrace();
155
return false;
156
} finally {
157
unlock();
158
}
159
}
160
161
@SuppressWarnings("removal")
162
private static void queueForCleanup(KeepAliveCleanerEntry kace) {
163
queue.lock();
164
try {
165
if(!kace.getQueuedForCleanup()) {
166
if (!queue.offer(kace)) {
167
kace.getHttpClient().closeServer();
168
return;
169
}
170
171
kace.setQueuedForCleanup();
172
queue.signalAll();
173
}
174
175
boolean startCleanupThread = (cleanerThread == null);
176
if (!startCleanupThread) {
177
if (!cleanerThread.isAlive()) {
178
startCleanupThread = true;
179
}
180
}
181
182
if (startCleanupThread) {
183
java.security.AccessController.doPrivileged(
184
new java.security.PrivilegedAction<Void>() {
185
public Void run() {
186
cleanerThread = InnocuousThread.newSystemThread("Keep-Alive-SocketCleaner", queue);
187
cleanerThread.setDaemon(true);
188
cleanerThread.setPriority(Thread.MAX_PRIORITY - 2);
189
cleanerThread.start();
190
return null;
191
}
192
});
193
}
194
} finally {
195
queue.unlock();
196
}
197
}
198
199
// Only called from KeepAliveStreamCleaner
200
protected long remainingToRead() {
201
assert isLockHeldByCurrentThread();
202
return expected - count;
203
}
204
205
// Only called from KeepAliveStreamCleaner
206
protected void setClosed() {
207
assert isLockHeldByCurrentThread();
208
in = null;
209
hc = null;
210
closed = true;
211
}
212
}
213
214