001: /*
002: * Copyright 2004 by Paulo Soares.
003: *
004: * The contents of this file are subject to the Mozilla Public License Version 1.1
005: * (the "License"); you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the License.
011: *
012: * The Original Code is 'iText, a free JAVA-PDF library'.
013: *
014: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
015: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
016: * All Rights Reserved.
017: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
018: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
019: *
020: * Contributor(s): all the names of the contributors are added in the source code
021: * where applicable.
022: *
023: * Alternatively, the contents of this file may be used under the terms of the
024: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
025: * provisions of LGPL are applicable instead of those above. If you wish to
026: * allow use of your version of this file only under the terms of the LGPL
027: * License and not to allow others to use your version of this file under
028: * the MPL, indicate your decision by deleting the provisions above and
029: * replace them with the notice and other provisions required by the LGPL.
030: * If you do not delete the provisions above, a recipient may use your version
031: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
032: *
033: * This library is free software; you can redistribute it and/or modify it
034: * under the terms of the MPL as stated above or under the terms of the GNU
035: * Library General Public License as published by the Free Software Foundation;
036: * either version 2 of the License, or any later version.
037: *
038: * This library is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
040: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
041: * details.
042: *
043: * If you didn't download this code from the following link, you should check if
044: * you aren't using an obsolete version:
045: * http://www.lowagie.com/iText/
046: */
047: package com.lowagie.text.pdf;
048:
049: import java.io.IOException;
050: import java.io.OutputStream;
051:
052: /**
053: *
054: * @author psoares
055: */
056: public class OutputStreamCounter extends OutputStream {
057:
058: protected OutputStream out;
059: protected int counter = 0;
060:
061: /** Creates a new instance of OutputStreamCounter */
062: public OutputStreamCounter(OutputStream out) {
063: this .out = out;
064: }
065:
066: /** Closes this output stream and releases any system resources
067: * associated with this stream. The general contract of <code>close</code>
068: * is that it closes the output stream. A closed stream cannot perform
069: * output operations and cannot be reopened.
070: * <p>
071: * The <code>close</code> method of <code>OutputStream</code> does nothing.
072: *
073: * @exception IOException if an I/O error occurs.
074: *
075: */
076: public void close() throws IOException {
077: out.close();
078: }
079:
080: /** Flushes this output stream and forces any buffered output bytes
081: * to be written out. The general contract of <code>flush</code> is
082: * that calling it is an indication that, if any bytes previously
083: * written have been buffered by the implementation of the output
084: * stream, such bytes should immediately be written to their
085: * intended destination.
086: * <p>
087: * The <code>flush</code> method of <code>OutputStream</code> does nothing.
088: *
089: * @exception IOException if an I/O error occurs.
090: *
091: */
092: public void flush() throws IOException {
093: out.flush();
094: }
095:
096: /** Writes <code>b.length</code> bytes from the specified byte array
097: * to this output stream. The general contract for <code>write(b)</code>
098: * is that it should have exactly the same effect as the call
099: * <code>write(b, 0, b.length)</code>.
100: *
101: * @param b the data.
102: * @exception IOException if an I/O error occurs.
103: * @see java.io.OutputStream#write(byte[], int, int)
104: *
105: */
106: public void write(byte[] b) throws IOException {
107: counter += b.length;
108: out.write(b);
109: }
110:
111: /** Writes the specified byte to this output stream. The general
112: * contract for <code>write</code> is that one byte is written
113: * to the output stream. The byte to be written is the eight
114: * low-order bits of the argument <code>b</code>. The 24
115: * high-order bits of <code>b</code> are ignored.
116: * <p>
117: * Subclasses of <code>OutputStream</code> must provide an
118: * implementation for this method.
119: *
120: * @param b the <code>byte</code>.
121: * @exception IOException if an I/O error occurs. In particular,
122: * an <code>IOException</code> may be thrown if the
123: * output stream has been closed.
124: *
125: */
126: public void write(int b) throws IOException {
127: ++counter;
128: out.write(b);
129: }
130:
131: /** Writes <code>len</code> bytes from the specified byte array
132: * starting at offset <code>off</code> to this output stream.
133: * The general contract for <code>write(b, off, len)</code> is that
134: * some of the bytes in the array <code>b</code> are written to the
135: * output stream in order; element <code>b[off]</code> is the first
136: * byte written and <code>b[off+len-1]</code> is the last byte written
137: * by this operation.
138: * <p>
139: * The <code>write</code> method of <code>OutputStream</code> calls
140: * the write method of one argument on each of the bytes to be
141: * written out. Subclasses are encouraged to override this method and
142: * provide a more efficient implementation.
143: * <p>
144: * If <code>b</code> is <code>null</code>, a
145: * <code>NullPointerException</code> is thrown.
146: * <p>
147: * If <code>off</code> is negative, or <code>len</code> is negative, or
148: * <code>off+len</code> is greater than the length of the array
149: * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
150: *
151: * @param b the data.
152: * @param off the start offset in the data.
153: * @param len the number of bytes to write.
154: * @exception IOException if an I/O error occurs. In particular,
155: * an <code>IOException</code> is thrown if the output
156: * stream is closed.
157: *
158: */
159: public void write(byte[] b, int off, int len) throws IOException {
160: counter += len;
161: out.write(b, off, len);
162: }
163:
164: public int getCounter() {
165: return counter;
166: }
167:
168: public void resetCounter() {
169: counter = 0;
170: }
171: }
|