author | lmesnik |
Wed, 11 Sep 2019 16:25:12 -0700 | |
changeset 58093 | 50e1d346a126 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, 2007, 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.httpserver; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import com.sun.net.httpserver.*; |
|
30 |
import com.sun.net.httpserver.spi.*; |
|
31 |
||
32 |
/** |
|
33 |
* a (filter) input stream which can tell us if bytes are "left over" |
|
34 |
* on the underlying stream which can be read (without blocking) |
|
35 |
* on another instance of this class. |
|
36 |
* |
|
37 |
* The class can also report if all bytes "expected" to be read |
|
38 |
* were read, by the time close() was called. In that case, |
|
39 |
* bytes may be drained to consume them (by calling drain() ). |
|
40 |
* |
|
41 |
* isEOF() returns true, when all expected bytes have been read |
|
42 |
*/ |
|
43 |
abstract class LeftOverInputStream extends FilterInputStream { |
|
44913
1b08f0eb012e
8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents:
25859
diff
changeset
|
44 |
final ExchangeImpl t; |
1b08f0eb012e
8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents:
25859
diff
changeset
|
45 |
final ServerImpl server; |
2 | 46 |
protected boolean closed = false; |
47 |
protected boolean eof = false; |
|
48 |
byte[] one = new byte [1]; |
|
49 |
||
50 |
public LeftOverInputStream (ExchangeImpl t, InputStream src) { |
|
51 |
super (src); |
|
52 |
this.t = t; |
|
53 |
this.server = t.getServerImpl(); |
|
54 |
} |
|
55 |
/** |
|
56 |
* if bytes are left over buffered on *the UNDERLYING* stream |
|
57 |
*/ |
|
58 |
public boolean isDataBuffered () throws IOException { |
|
59 |
assert eof; |
|
60 |
return super.available() > 0; |
|
61 |
} |
|
62 |
||
63 |
public void close () throws IOException { |
|
64 |
if (closed) { |
|
65 |
return; |
|
66 |
} |
|
67 |
closed = true; |
|
68 |
if (!eof) { |
|
69 |
eof = drain (ServerConfig.getDrainAmount()); |
|
70 |
} |
|
71 |
} |
|
72 |
||
73 |
public boolean isClosed () { |
|
74 |
return closed; |
|
75 |
} |
|
76 |
||
77 |
public boolean isEOF () { |
|
78 |
return eof; |
|
79 |
} |
|
80 |
||
81 |
protected abstract int readImpl (byte[]b, int off, int len) throws IOException; |
|
82 |
||
83 |
public synchronized int read () throws IOException { |
|
84 |
if (closed) { |
|
85 |
throw new IOException ("Stream is closed"); |
|
86 |
} |
|
87 |
int c = readImpl (one, 0, 1); |
|
88 |
if (c == -1 || c == 0) { |
|
89 |
return c; |
|
90 |
} else { |
|
91 |
return one[0] & 0xFF; |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
public synchronized int read (byte[]b, int off, int len) throws IOException { |
|
96 |
if (closed) { |
|
97 |
throw new IOException ("Stream is closed"); |
|
98 |
} |
|
99 |
return readImpl (b, off, len); |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* read and discard up to l bytes or "eof" occurs, |
|
104 |
* (whichever is first). Then return true if the stream |
|
105 |
* is at eof (ie. all bytes were read) or false if not |
|
106 |
* (still bytes to be read) |
|
107 |
*/ |
|
108 |
public boolean drain (long l) throws IOException { |
|
109 |
int bufSize = 2048; |
|
110 |
byte[] db = new byte [bufSize]; |
|
111 |
while (l > 0) { |
|
44913
1b08f0eb012e
8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents:
25859
diff
changeset
|
112 |
if (server.isFinishing()) { |
1b08f0eb012e
8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents:
25859
diff
changeset
|
113 |
break; |
1b08f0eb012e
8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents:
25859
diff
changeset
|
114 |
} |
2 | 115 |
long len = readImpl (db, 0, bufSize); |
116 |
if (len == -1) { |
|
117 |
eof = true; |
|
118 |
return true; |
|
119 |
} else { |
|
120 |
l = l - len; |
|
121 |
} |
|
122 |
} |
|
123 |
return false; |
|
124 |
} |
|
125 |
} |