2
|
1 |
/*
|
57968
|
2 |
* Copyright (c) 1994, 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;
|
|
27 |
|
|
28 |
import java.io.*;
|
57968
|
29 |
import java.util.concurrent.locks.ReentrantLock;
|
2
|
30 |
import sun.net.ProgressSource;
|
|
31 |
import sun.net.www.http.ChunkedInputStream;
|
|
32 |
|
|
33 |
|
|
34 |
public class MeteredStream extends FilterInputStream {
|
|
35 |
|
|
36 |
// Instance variables.
|
|
37 |
/* if expected != -1, after we've read >= expected, we're "closed" and return -1
|
|
38 |
* from subsequest read() 's
|
|
39 |
*/
|
|
40 |
protected boolean closed = false;
|
|
41 |
protected long expected;
|
|
42 |
protected long count = 0;
|
|
43 |
protected long markedCount = 0;
|
|
44 |
protected int markLimit = -1;
|
|
45 |
protected ProgressSource pi;
|
57968
|
46 |
protected final ReentrantLock readLock = new ReentrantLock();
|
2
|
47 |
|
|
48 |
public MeteredStream(InputStream is, ProgressSource pi, long expected)
|
|
49 |
{
|
|
50 |
super(is);
|
|
51 |
|
|
52 |
this.pi = pi;
|
|
53 |
this.expected = expected;
|
|
54 |
|
|
55 |
if (pi != null) {
|
|
56 |
pi.updateProgress(0, expected);
|
|
57 |
}
|
|
58 |
}
|
|
59 |
|
57968
|
60 |
private final void justRead(long n) throws IOException {
|
|
61 |
assert readLock.isHeldByCurrentThread();
|
|
62 |
|
2
|
63 |
if (n == -1) {
|
|
64 |
|
|
65 |
/*
|
|
66 |
* don't close automatically when mark is set and is valid;
|
|
67 |
* cannot reset() after close()
|
|
68 |
*/
|
|
69 |
if (!isMarked()) {
|
|
70 |
close();
|
|
71 |
}
|
|
72 |
return;
|
|
73 |
}
|
|
74 |
|
|
75 |
count += n;
|
|
76 |
|
|
77 |
/**
|
|
78 |
* If read beyond the markLimit, invalidate the mark
|
|
79 |
*/
|
|
80 |
if (count - markedCount > markLimit) {
|
|
81 |
markLimit = -1;
|
|
82 |
}
|
|
83 |
|
|
84 |
if (pi != null)
|
|
85 |
pi.updateProgress(count, expected);
|
|
86 |
|
|
87 |
if (isMarked()) {
|
|
88 |
return;
|
|
89 |
}
|
|
90 |
|
|
91 |
// if expected length is known, we could determine if
|
|
92 |
// read overrun.
|
|
93 |
if (expected > 0) {
|
|
94 |
if (count >= expected) {
|
|
95 |
close();
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
/**
|
|
101 |
* Returns true if the mark is valid, false otherwise
|
|
102 |
*/
|
|
103 |
private boolean isMarked() {
|
57968
|
104 |
assert readLock.isHeldByCurrentThread();
|
2
|
105 |
|
|
106 |
if (markLimit < 0) {
|
|
107 |
return false;
|
|
108 |
}
|
|
109 |
|
|
110 |
// mark is set, but is not valid anymore
|
|
111 |
if (count - markedCount > markLimit) {
|
|
112 |
return false;
|
|
113 |
}
|
|
114 |
|
|
115 |
// mark still holds
|
|
116 |
return true;
|
|
117 |
}
|
|
118 |
|
57968
|
119 |
public int read() throws java.io.IOException {
|
|
120 |
if (closed) return -1;
|
|
121 |
readLock.lock();
|
|
122 |
try {
|
|
123 |
if (closed) return -1;
|
|
124 |
int c = in.read();
|
|
125 |
if (c != -1) {
|
|
126 |
justRead(1);
|
|
127 |
} else {
|
|
128 |
justRead(c);
|
|
129 |
}
|
|
130 |
return c;
|
|
131 |
} finally {
|
|
132 |
readLock.unlock();
|
2
|
133 |
}
|
|
134 |
}
|
|
135 |
|
57968
|
136 |
public int read(byte b[], int off, int len)
|
2
|
137 |
throws java.io.IOException {
|
57968
|
138 |
if (closed) return -1;
|
|
139 |
readLock.lock();
|
|
140 |
try {
|
|
141 |
if (closed) return -1;
|
|
142 |
|
|
143 |
int n = in.read(b, off, len);
|
|
144 |
justRead(n);
|
|
145 |
return n;
|
|
146 |
} finally {
|
|
147 |
readLock.unlock();
|
2
|
148 |
}
|
|
149 |
}
|
|
150 |
|
57968
|
151 |
public long skip(long n) throws IOException {
|
2
|
152 |
|
57968
|
153 |
if (closed) return 0;
|
|
154 |
readLock.lock();
|
|
155 |
try {
|
|
156 |
// REMIND: what does skip do on EOF????
|
|
157 |
if (closed) return 0;
|
2
|
158 |
|
57968
|
159 |
if (in instanceof ChunkedInputStream) {
|
|
160 |
n = in.skip(n);
|
|
161 |
} else {
|
|
162 |
// just skip min(n, num_bytes_left)
|
|
163 |
long min = (n > expected - count) ? expected - count : n;
|
|
164 |
n = in.skip(min);
|
|
165 |
}
|
|
166 |
justRead(n);
|
|
167 |
return n;
|
|
168 |
} finally {
|
|
169 |
readLock.unlock();
|
2
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
public void close() throws IOException {
|
57968
|
174 |
if (closed) return;
|
|
175 |
readLock.lock();
|
|
176 |
try {
|
|
177 |
if (closed) return;
|
|
178 |
if (pi != null)
|
|
179 |
pi.finishTracking();
|
|
180 |
|
|
181 |
closed = true;
|
|
182 |
in.close();
|
|
183 |
} finally {
|
|
184 |
readLock.unlock();
|
2
|
185 |
}
|
|
186 |
}
|
|
187 |
|
57968
|
188 |
public int available() throws IOException {
|
|
189 |
if (closed) return 0;
|
|
190 |
readLock.lock();
|
|
191 |
try {
|
|
192 |
return closed ? 0 : in.available();
|
|
193 |
} finally {
|
|
194 |
readLock.unlock();
|
|
195 |
}
|
2
|
196 |
}
|
|
197 |
|
57968
|
198 |
public void mark(int readLimit) {
|
|
199 |
if (closed) return;
|
|
200 |
readLock.lock();
|
|
201 |
try {
|
|
202 |
if (closed) return;
|
|
203 |
super.mark(readLimit);
|
2
|
204 |
|
57968
|
205 |
/*
|
|
206 |
* mark the count to restore upon reset
|
|
207 |
*/
|
|
208 |
markedCount = count;
|
|
209 |
markLimit = readLimit;
|
|
210 |
} finally {
|
|
211 |
readLock.unlock();
|
|
212 |
}
|
2
|
213 |
}
|
|
214 |
|
57968
|
215 |
public void reset() throws IOException {
|
|
216 |
if (closed) return;
|
2
|
217 |
|
57968
|
218 |
readLock.lock();
|
|
219 |
try {
|
|
220 |
if (closed) return;
|
|
221 |
if (!isMarked()) {
|
|
222 |
throw new IOException("Resetting to an invalid mark");
|
|
223 |
}
|
|
224 |
|
|
225 |
count = markedCount;
|
|
226 |
super.reset();
|
|
227 |
} finally {
|
|
228 |
readLock.unlock();
|
2
|
229 |
}
|
|
230 |
}
|
|
231 |
|
|
232 |
public boolean markSupported() {
|
57968
|
233 |
if (closed) return false;
|
|
234 |
readLock.lock();
|
|
235 |
try {
|
|
236 |
if (closed) return false;
|
|
237 |
return super.markSupported();
|
|
238 |
} finally {
|
|
239 |
readLock.unlock();
|
2
|
240 |
}
|
|
241 |
}
|
|
242 |
|
44534
|
243 |
@SuppressWarnings("deprecation")
|
2
|
244 |
protected void finalize() throws Throwable {
|
|
245 |
try {
|
|
246 |
close();
|
|
247 |
if (pi != null)
|
|
248 |
pi.close();
|
|
249 |
}
|
|
250 |
finally {
|
|
251 |
// Call super class
|
|
252 |
super.finalize();
|
|
253 |
}
|
|
254 |
}
|
|
255 |
}
|