001: //httpContentLengthInputStream.java
002: //-----------------------
003: //(C) by Michael Peter Christen; mc@anomic.de
004: //first published on http://www.anomic.de
005: //Frankfurt, Germany, 2004
006: //
007: // This file is contributed by Martin Thelian
008: //
009: // $LastChangedDate: 2006-09-15 17:01:25 +0200 (Fr, 15 Sep 2006) $
010: // $LastChangedRevision: 2598 $
011: // $LastChangedBy: theli $
012: //
013: //This program is free software; you can redistribute it and/or modify
014: //it under the terms of the GNU General Public License as published by
015: //the Free Software Foundation; either version 2 of the License, or
016: //(at your option) any later version.
017: //
018: //This program is distributed in the hope that it will be useful,
019: //but WITHOUT ANY WARRANTY; without even the implied warranty of
020: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: //GNU General Public License for more details.
022: //
023: //You should have received a copy of the GNU General Public License
024: //along with this program; if not, write to the Free Software
025: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
026: //
027: //Using this software in any meaning (reading, learning, copying, compiling,
028: //running) means that you agree that the Author(s) is (are) not responsible
029: //for cost, loss of data or any harm that may be caused directly or indirectly
030: //by usage of this softare or this documentation. The usage of this software
031: //is on your own risk. The installation and usage (starting/running) of this
032: //software may allow other people or application to access your computer and
033: //any attached devices and is highly dependent on the configuration of the
034: //software which must be done by the user of the software; the author(s) is
035: //(are) also not responsible for proper configuration and usage of the
036: //software, even if provoked by documentation provided together with
037: //the software.
038: //
039: //Any changes to this file according to the GPL as documented in the file
040: //gpl.txt aside this file in the shipment you received can be done to the
041: //lines that follows this copyright notice here, but changes must not be
042: //done inside the copyright notive above. A re-distribution must contain
043: //the intact and unchanged copyright notice.
044: //Contributions and changes to the program code must be marked as such.
045:
046: package de.anomic.http;
047:
048: import java.io.IOException;
049: import java.io.InputStream;
050:
051: public class httpContentLengthInputStream extends
052: httpdByteCountInputStream {
053:
054: private long contentLength;
055: private boolean closed = false;
056:
057: public httpContentLengthInputStream(InputStream inputStream,
058: long contentLength) {
059: super (inputStream);
060: this .contentLength = contentLength;
061: }
062:
063: public long getContentLength() {
064: return this .contentLength;
065: }
066:
067: public boolean isClosed() {
068: return this .closed;
069: }
070:
071: /**
072: * Closes this input stream.
073: * <b>Attention:</b> This does not close the wrapped input stream, because
074: * otherwise keep-alive connections would terminate
075: */
076: public void close() throws IOException {
077: if (!this .closed) {
078: try {
079: // read to the end of the stream and throw read bytes away
080: httpChunkedInputStream.exhaustInputStream(this );
081: } finally {
082: this .closed = true;
083: }
084: }
085: }
086:
087: public int read() throws IOException {
088: if (this .closed)
089: throw new IOException("Stream already closed.");
090:
091: // if we have already finished reading ...
092: if (this .byteCount >= this .contentLength)
093: return -1;
094: return super .read();
095: }
096:
097: public int read(byte[] buffer) throws IOException {
098: return read(buffer, 0, buffer.length);
099: }
100:
101: public int read(byte[] buffer, int off, int len) throws IOException {
102: if (this .closed)
103: throw new IOException("Stream already closed.");
104:
105: // if we have already finished reading ...
106: if (this .byteCount >= this .contentLength)
107: return -1;
108:
109: // only read until body end
110: if (this .byteCount + len > this .contentLength) {
111: len = (int) (this .contentLength - this .byteCount);
112: }
113:
114: return super .read(buffer, off, len);
115: }
116:
117: public long skip(long len) throws IOException {
118: long skipLength = Math.min(len, this.contentLength
119: - this.byteCount);
120: return super.skip(skipLength);
121: }
122:
123: }
|