001: package com.quadcap.io;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.*;
042:
043: import com.quadcap.util.Debug;
044: import com.quadcap.util.Util;
045:
046: /**
047: * A limited size output stream.
048: *
049: * @author Stan Bailes
050: */
051:
052: public class LimitedOutputStream extends FilterOutputStream {
053: int count = 0;
054: int limit;
055:
056: public LimitedOutputStream(OutputStream os, int limit) {
057: super (os);
058: this .limit = limit;
059: }
060:
061: public void write(int b) throws IOException {
062: if (count + 1 > limit) {
063: throw new LimitExceededException("Stream limit (" + limit
064: + ") exceeded");
065: }
066: count++;
067: out.write(b);
068: }
069:
070: /**
071: * Writes <code>len</code> bytes from the specified
072: * <code>byte</code> array starting at offset <code>off</code> to
073: * this output stream.
074: * <p>
075: * The <code>write</code> method of <code>FilterOutputStream</code>
076: * calls the <code>write</code> method of one argument on each
077: * <code>byte</code> to output.
078: * <p>
079: * Note that this method does not call the <code>write</code> method
080: * of its underlying input stream with the same arguments. Subclasses
081: * of <code>FilterOutputStream</code> should provide a more efficient
082: * implementation of this method.
083: *
084: * @param b the data.
085: * @param off the start offset in the data.
086: * @param len the number of bytes to write.
087: * @exception IOException if an I/O error occurs.
088: * @see java.io.FilterOutputStream#write(int)
089: * @since JDK1.0
090: */
091: public void write(byte b[], int off, int len) throws IOException {
092: if (count + len > limit) {
093: if (limit - count > 0) {
094: out.write(b, off, limit - count);
095: }
096: throw new LimitExceededException("Stream limit (" + limit
097: + ") exceeded");
098: }
099: count += len;
100: out.write(b, off, len);
101: }
102:
103: }
|