001: //httpBoundedSizeOutputStream.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: // last major change: $LastChangedDate: 2006-08-16 21:49:31 +0200 (Mi, 16 Aug 2006) $ by $LastChangedBy: orbiter $
009: // Revision: $LastChangedRevision: 2414 $
010: //
011: //This program is free software; you can redistribute it and/or modify
012: //it under the terms of the GNU General Public License as published by
013: //the Free Software Foundation; either version 2 of the License, or
014: //(at your option) any later version.
015: //
016: //This program is distributed in the hope that it will be useful,
017: //but WITHOUT ANY WARRANTY; without even the implied warranty of
018: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: //GNU General Public License for more details.
020: //
021: //You should have received a copy of the GNU General Public License
022: //along with this program; if not, write to the Free Software
023: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: //
025: //Using this software in any meaning (reading, learning, copying, compiling,
026: //running) means that you agree that the Author(s) is (are) not responsible
027: //for cost, loss of data or any harm that may be caused directly or indirectly
028: //by usage of this softare or this documentation. The usage of this software
029: //is on your own risk. The installation and usage (starting/running) of this
030: //software may allow other people or application to access your computer and
031: //any attached devices and is highly dependent on the configuration of the
032: //software which must be done by the user of the software; the author(s) is
033: //(are) also not responsible for proper configuration and usage of the
034: //software, even if provoked by documentation provided together with
035: //the software.
036: //
037: //Any changes to this file according to the GPL as documented in the file
038: //gpl.txt aside this file in the shipment you received can be done to the
039: //lines that follows this copyright notice here, but changes must not be
040: //done inside the copyright notive above. A re-distribution must contain
041: //the intact and unchanged copyright notice.
042: //Contributions and changes to the program code must be marked as such.
043:
044: package de.anomic.http;
045:
046: import java.io.IOException;
047: import java.io.OutputStream;
048:
049: public class httpdBoundedSizeOutputStream extends
050: httpdByteCountOutputStream {
051:
052: protected long maxSize = 0;
053:
054: public httpdBoundedSizeOutputStream(OutputStream outputStream,
055: long sizeLimit) {
056: this (outputStream, 0, sizeLimit);
057: }
058:
059: public httpdBoundedSizeOutputStream(OutputStream outputStream,
060: long initByteCount, long sizeLimit) {
061: super (outputStream, initByteCount, null);
062: this .maxSize = sizeLimit;
063: }
064:
065: public void write(byte[] b) throws IOException {
066: if (this .byteCount + b.length > this .maxSize) {
067: // write out the rest until we have reached the limit
068: long rest = this .maxSize - this .byteCount;
069: if (rest > 0)
070: this .write(b, 0, (int) rest);
071:
072: // throw an exception
073: throw new httpdLimitExceededException("Limit exceeded",
074: this .maxSize);
075: }
076: super .write(b);
077: }
078:
079: public void write(byte[] b, int off, int len) throws IOException {
080: if (this .byteCount + len > this .maxSize) {
081: // write out the rest until we reach the limit
082: long rest = this .maxSize - this .byteCount;
083: if (rest > 0)
084: this .write(b, 0, (int) rest);
085:
086: // throw an exception
087: throw new httpdLimitExceededException("Limit exceeded",
088: this .maxSize);
089: }
090: super .write(b, off, len);
091: }
092:
093: public void write(int b) throws IOException {
094: if (this .byteCount + 1 > this .maxSize) {
095: // throw an exception
096: throw new httpdLimitExceededException("Limit exceeded",
097: this .maxSize);
098: }
099: super .write(b);
100: }
101:
102: public long getSizeLimit() {
103: return this.maxSize;
104: }
105: }
|