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/KeepAliveStreamCleaner.java
41161 views
1
/*
2
* Copyright (c) 2005, 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.IOException;
29
import java.util.LinkedList;
30
import sun.net.NetProperties;
31
import java.security.AccessController;
32
import java.security.PrivilegedAction;
33
import java.util.concurrent.TimeUnit;
34
import java.util.concurrent.locks.Condition;
35
import java.util.concurrent.locks.ReentrantLock;
36
37
/**
38
* This class is used to cleanup any remaining data that may be on a KeepAliveStream
39
* so that the connection can be cached in the KeepAliveCache.
40
* Instances of this class can be used as a FIFO queue for KeepAliveCleanerEntry objects.
41
* Executing this Runnable removes each KeepAliveCleanerEntry from the Queue, reads
42
* the reamining bytes on its KeepAliveStream, and if successful puts the connection in
43
* the KeepAliveCache.
44
*
45
* @author Chris Hegarty
46
*/
47
48
@SuppressWarnings("serial") // never serialized
49
class KeepAliveStreamCleaner
50
extends LinkedList<KeepAliveCleanerEntry>
51
implements Runnable
52
{
53
// maximum amount of remaining data that we will try to cleanup
54
protected static int MAX_DATA_REMAINING = 512;
55
56
// maximum amount of KeepAliveStreams to be queued
57
protected static int MAX_CAPACITY = 10;
58
59
// timeout for both socket and poll on the queue
60
protected static final int TIMEOUT = 5000;
61
62
// max retries for skipping data
63
private static final int MAX_RETRIES = 5;
64
65
static {
66
final String maxDataKey = "http.KeepAlive.remainingData";
67
@SuppressWarnings("removal")
68
int maxData = AccessController.doPrivileged(
69
new PrivilegedAction<Integer>() {
70
public Integer run() {
71
return NetProperties.getInteger(maxDataKey, MAX_DATA_REMAINING);
72
}}).intValue() * 1024;
73
MAX_DATA_REMAINING = maxData;
74
75
final String maxCapacityKey = "http.KeepAlive.queuedConnections";
76
@SuppressWarnings("removal")
77
int maxCapacity = AccessController.doPrivileged(
78
new PrivilegedAction<Integer>() {
79
public Integer run() {
80
return NetProperties.getInteger(maxCapacityKey, MAX_CAPACITY);
81
}}).intValue();
82
MAX_CAPACITY = maxCapacity;
83
84
}
85
86
private final ReentrantLock queueLock = new ReentrantLock();
87
private final Condition waiter = queueLock.newCondition();
88
89
final void signalAll() {
90
waiter.signalAll();
91
}
92
93
final void lock() {
94
queueLock.lock();
95
}
96
97
final void unlock() {
98
queueLock.unlock();
99
}
100
101
@Override
102
public boolean offer(KeepAliveCleanerEntry e) {
103
if (size() >= MAX_CAPACITY)
104
return false;
105
106
return super.offer(e);
107
}
108
109
@Override
110
public void run()
111
{
112
KeepAliveCleanerEntry kace = null;
113
114
do {
115
try {
116
lock();
117
try {
118
long before = System.currentTimeMillis();
119
long timeout = TIMEOUT;
120
while ((kace = poll()) == null) {
121
waiter.await(timeout, TimeUnit.MILLISECONDS);
122
123
long after = System.currentTimeMillis();
124
long elapsed = after - before;
125
if (elapsed > timeout) {
126
/* one last try */
127
kace = poll();
128
break;
129
}
130
before = after;
131
timeout -= elapsed;
132
}
133
} finally {
134
unlock();
135
}
136
137
if(kace == null)
138
break;
139
140
KeepAliveStream kas = kace.getKeepAliveStream();
141
142
if (kas != null) {
143
kas.lock();
144
try {
145
HttpClient hc = kace.getHttpClient();
146
try {
147
if (hc != null && !hc.isInKeepAliveCache()) {
148
int oldTimeout = hc.getReadTimeout();
149
hc.setReadTimeout(TIMEOUT);
150
long remainingToRead = kas.remainingToRead();
151
if (remainingToRead > 0) {
152
long n = 0;
153
int retries = 0;
154
while (n < remainingToRead && retries < MAX_RETRIES) {
155
remainingToRead = remainingToRead - n;
156
n = kas.skip(remainingToRead);
157
if (n == 0)
158
retries++;
159
}
160
remainingToRead = remainingToRead - n;
161
}
162
if (remainingToRead == 0) {
163
hc.setReadTimeout(oldTimeout);
164
hc.finished();
165
} else
166
hc.closeServer();
167
}
168
} catch (IOException ioe) {
169
hc.closeServer();
170
} finally {
171
kas.setClosed();
172
}
173
} finally {
174
kas.unlock();
175
}
176
}
177
} catch (InterruptedException ie) { }
178
} while (kace != null);
179
}
180
}
181
182