diff options
| author | David Daney <ddaney@avtrex.com> | 2005-10-12 19:48:24 +0000 |
|---|---|---|
| committer | David Daney <ddaney@avtrex.com> | 2005-10-12 19:48:24 +0000 |
| commit | f66ea401a708a042dc9a1058798c58599d642941 (patch) | |
| tree | f6884bf2ed3d57dd64c24e7be7dc0e1ff12b4dc6 /gnu/java/net/protocol/http/LimitedLengthInputStream.java | |
| parent | 39ab427324144ed0178e2b4602743dd984a27d4a (diff) | |
| download | classpath-f66ea401a708a042dc9a1058798c58599d642941.tar.gz | |
2005-10-12 David Daney <ddaney@avtrex.com>
PR classpath/24086, PR classpath/24091, PR classpath/24104
* gnu/java/net/protocol/http/ByteArrayResponseBodyReader.java: Removed.
* gnu/java/net/protocol/http/ResponseBodyReader.java: Removed.
* gnu/java/net/protocol/http/event/ConnectionEvent.java: Removed.
* gnu/java/net/protocol/http/event/ConnectionListener.java: Removed.
* gnu/java/net/protocol/http/event/RequestEvent.java: Removed.
* gnu/java/net/protocol/http/event/RequestListener.java: Removed.
* gnu/java/net/protocol/http/event/package.html: Removed.
* gnu/java/net/protocol/http/HTTPConnection.java: Cleaned up imports.
(connectionListeners): Removed.
(requestListeners): Removed.
(pool): New field.
(Constructor): Don't initialize connectionListeners or
requestListeners.
(useCount): New field.
(getPoolKey): New method.
(setPool): New method.
(release): New method.
(newRequest): Don't call fireRequestEvent. Use StringBuilder instead
of StringBuffer.
(close): Don't call fireConnectionEvent.
(getURI):Use StringBuilder instead of StringBuffer.
(addConnectionListener): Removed.
(removeConnectionListener): Removed.
(fireConnectionEvent): Removed.
(addRequestListener): Removed.
(removeRequestListener): Removed.
(fireRequestEvent): Removed.
* gnu/java/net/protocol/http/HTTPURLConnection.java:Cleaned up imports.
(connectionPool): Changed type to LinkedHashMap.
(maxConnections): Made static.
(responseSink): Changed type to InputStream.
(errorSink): Likewise.
(connect): Eliminate reader. Get responseSink from response.
(getConnection): Rewrote.
(getHeaderFields): Use null as key for status line. Return
unmodifiable Map.
* gnu/java/net/protocol/http/Headers.java: Extend LinkedHashMap,
instead of implement Map.
(headers): Removed.
(Constructor): Do not initialize headers.
(size): Removed.
(isEmpth): Removed.
(containsKey): Rewrote.
(containsValue): Removed.
(get): Call super.get().
(getValue): Likewise.
(getLongValue): New method.
(put): Call super.put().
(remove): Call super.remove().
(putAll): Rewrote.
(clear): Removed.
(keySet): Call super.keySet().
(values): Removed.
(entrySet): Call super.entrySet().
(equals): Removed.
(hashCode): Removed.
(parse): Use StringBuilder instead of StringBuffer.
(addValue): Call super.* instead of headers.*.
* gnu/java/net/protocol/http/LimitedLengthInputStream.java: New class.
* gnu/java/net/protocol/http/Request.java: Cleaned up imports.
(responseBodyReader): Removed.
(setResponseBodyReader): Removed.
(dispatch): Don't create LineInputStream. Don't call fireRequestEvent.
Don't append CRLF to Request body. Handle unsolicited 100 Continue
response.
(readResponse): Rewrote.
(readResponseBody): Renamed to ...
(createResponseBodyStream): ... And rewritten.
(parseAuthParams): Use StringBuilder instead of StringBuffer.
(getNonceCount): Likewise.
(handleSetCookie): Likewise.
* gnu/java/net/protocol/http/Response.java: Cleaned up imports.
(codeClass): Removed.
(body): New field.
(Constructor): Don't initialize codeClass. Initialize body.
(getCodeClass): Rewrote.
(getLongHeader): New method.
(getBody): New method.
* gnu/java/net/protocol/http/ChunkedInputStream.java (read): Use
StringBuilder instead of StringBuffer.
* gnu/java/net/protocol/http/Cookie.java (toString): Use StringBuilder
instead of StringBuffer.
Diffstat (limited to 'gnu/java/net/protocol/http/LimitedLengthInputStream.java')
| -rw-r--r-- | gnu/java/net/protocol/http/LimitedLengthInputStream.java | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/gnu/java/net/protocol/http/LimitedLengthInputStream.java b/gnu/java/net/protocol/http/LimitedLengthInputStream.java new file mode 100644 index 000000000..16cf56a29 --- /dev/null +++ b/gnu/java/net/protocol/http/LimitedLengthInputStream.java @@ -0,0 +1,220 @@ +/* LimitedLengthInputStream.java -- + Copyright (C) 2005 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package gnu.java.net.protocol.http; + +import java.io.IOException; +import java.io.InputStream; + +/** + * InputStream that limits the total number of bytes that can be read + * from an underlying stream. In addition to limiting the number of + * bytes read, close() is not propagated to the underlying stream. + * + * @author David Daney (ddaney@avtrex.com) + */ +class LimitedLengthInputStream + extends InputStream +{ + private long remainingLen; + private boolean restrictLen; + private HTTPConnection connection; + private boolean eof; + private InputStream in; + private boolean doClose; + + + private void handleClose() + throws IOException + { + eof = true; + if (doClose) + { + in.close(); + } + else + { + connection.release(); + } + in = null; + connection = null; + } + + /** + * Constructor. + * + * @param in the underlying stream + * + * @param maxLen the maximum number of bytes to read + * + * @param restrictLen if true the number of bytes that can be read + * from this stream will be limited to maxLen, otherwise the number + * of bytes is not restricted. + * + * @param con the HTTPConnection associated with this stream + * + * @param doClose if true con will be closed when finished reading, + * else it will be placed back in the connection pool. + * + */ + LimitedLengthInputStream(InputStream in, + long maxLen, + boolean restrictLen, + HTTPConnection con, + boolean doClose) + throws IOException + + { + this.in = in; + this.remainingLen = maxLen; + this.restrictLen = restrictLen; + this.connection = con; + this.doClose = doClose; + + if (restrictLen) + { + if (maxLen < 0) + throw new IllegalArgumentException(); + else if (maxLen == 0) + handleClose(); // Nothing to do, release the connection. + } + } + + public synchronized int read() + throws IOException + { + if (eof) + return -1; // EOF + + int r; + + if (restrictLen) + { + r = in.read(); + if (-1 != r) + remainingLen--; + + if (0 == remainingLen) + handleClose(); + } + else + { + r = in.read(); + if (r == -1) + handleClose(); + } + + return r; + } + + public int read(byte[] buffer) + throws IOException + { + return read(buffer, 0, buffer.length); + } + + public synchronized int read(byte[] buffer, int offset, int length) + throws IOException + { + if (eof) + return -1; // EOF + + if (restrictLen && length > remainingLen) + length = (int) remainingLen; + + int r = in.read(buffer, offset, length); + + if (-1 == r) + handleClose(); + + if (restrictLen && r > 0) + { + remainingLen -= r; + if (0 == remainingLen) + handleClose(); + } + return r; + } + + public synchronized long skip(long n) + throws IOException + { + + if (eof) + return 0; + + if (restrictLen && n > remainingLen) + n = remainingLen; + + long r = in.skip(n); + + if (restrictLen) + { + remainingLen -= r; + if (0 == remainingLen) + handleClose(); + } + return r; + } + + public synchronized int available() + throws IOException + { + if (eof) + return 0; + + int a = in.available(); + if (restrictLen && a > remainingLen) + a = (int)remainingLen; + return a; + } + + public synchronized void close() + throws IOException + { + if (eof) + return; + + // If we get to here, the stream was not fully read. Just throw + // it away. + + doClose = true; + + handleClose(); + } +} |
