1 /*
2 * Copyright 2004 by Paulo Soares.
3 *
4 * The contents of this file are subject to the Mozilla Public License Version 1.1
5 * (the "License"); you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7 *
8 * Software distributed under the License is distributed on an "AS IS" basis,
9 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10 * for the specific language governing rights and limitations under the License.
11 *
12 * The Original Code is 'iText, a free JAVA-PDF library'.
13 *
14 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
15 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
16 * All Rights Reserved.
17 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
18 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
19 *
20 * Contributor(s): all the names of the contributors are added in the source code
21 * where applicable.
22 *
23 * Alternatively, the contents of this file may be used under the terms of the
24 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
25 * provisions of LGPL are applicable instead of those above. If you wish to
26 * allow use of your version of this file only under the terms of the LGPL
27 * License and not to allow others to use your version of this file under
28 * the MPL, indicate your decision by deleting the provisions above and
29 * replace them with the notice and other provisions required by the LGPL.
30 * If you do not delete the provisions above, a recipient may use your version
31 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
32 *
33 * This library is free software; you can redistribute it and/or modify it
34 * under the terms of the MPL as stated above or under the terms of the GNU
35 * Library General Public License as published by the Free Software Foundation;
36 * either version 2 of the License, or any later version.
37 *
38 * This library is distributed in the hope that it will be useful, but WITHOUT
39 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
41 * details.
42 *
43 * If you didn't download this code from the following link, you should check if
44 * you aren't using an obsolete version:
45 * http://www.lowagie.com/iText/
46 */
47 package com.lowagie.text.pdf;
48 import java.io.IOException;
49 import java.io.OutputStream;
50
51 /**
52 *
53 * @author psoares
54 */
55 public class OutputStreamCounter extends OutputStream {
56
57 protected OutputStream out;
58 protected int counter = 0;
59
60 /** Creates a new instance of OutputStreamCounter */
61 public OutputStreamCounter(OutputStream out) {
62 this.out = out;
63 }
64
65 /** Closes this output stream and releases any system resources
66 * associated with this stream. The general contract of <code>close</code>
67 * is that it closes the output stream. A closed stream cannot perform
68 * output operations and cannot be reopened.
69 * <p>
70 * The <code>close</code> method of <code>OutputStream</code> does nothing.
71 *
72 * @exception IOException if an I/O error occurs.
73 *
74 */
75 public void close() throws IOException {
76 out.close();
77 }
78
79 /** Flushes this output stream and forces any buffered output bytes
80 * to be written out. The general contract of <code>flush</code> is
81 * that calling it is an indication that, if any bytes previously
82 * written have been buffered by the implementation of the output
83 * stream, such bytes should immediately be written to their
84 * intended destination.
85 * <p>
86 * The <code>flush</code> method of <code>OutputStream</code> does nothing.
87 *
88 * @exception IOException if an I/O error occurs.
89 *
90 */
91 public void flush() throws IOException {
92 out.flush();
93 }
94
95 /** Writes <code>b.length</code> bytes from the specified byte array
96 * to this output stream. The general contract for <code>write(b)</code>
97 * is that it should have exactly the same effect as the call
98 * <code>write(b, 0, b.length)</code>.
99 *
100 * @param b the data.
101 * @exception IOException if an I/O error occurs.
102 * @see java.io.OutputStream#write(byte[], int, int)
103 *
104 */
105 public void write(byte[] b) throws IOException {
106 counter += b.length;
107 out.write(b);
108 }
109
110 /** Writes the specified byte to this output stream. The general
111 * contract for <code>write</code> is that one byte is written
112 * to the output stream. The byte to be written is the eight
113 * low-order bits of the argument <code>b</code>. The 24
114 * high-order bits of <code>b</code> are ignored.
115 * <p>
116 * Subclasses of <code>OutputStream</code> must provide an
117 * implementation for this method.
118 *
119 * @param b the <code>byte</code>.
120 * @exception IOException if an I/O error occurs. In particular,
121 * an <code>IOException</code> may be thrown if the
122 * output stream has been closed.
123 *
124 */
125 public void write(int b) throws IOException {
126 ++counter;
127 out.write(b);
128 }
129
130 /** Writes <code>len</code> bytes from the specified byte array
131 * starting at offset <code>off</code> to this output stream.
132 * The general contract for <code>write(b, off, len)</code> is that
133 * some of the bytes in the array <code>b</code> are written to the
134 * output stream in order; element <code>b[off]</code> is the first
135 * byte written and <code>b[off+len-1]</code> is the last byte written
136 * by this operation.
137 * <p>
138 * The <code>write</code> method of <code>OutputStream</code> calls
139 * the write method of one argument on each of the bytes to be
140 * written out. Subclasses are encouraged to override this method and
141 * provide a more efficient implementation.
142 * <p>
143 * If <code>b</code> is <code>null</code>, a
144 * <code>NullPointerException</code> is thrown.
145 * <p>
146 * If <code>off</code> is negative, or <code>len</code> is negative, or
147 * <code>off+len</code> is greater than the length of the array
148 * <code>b</code>, then an <tt>IndexOutOfBoundsException</tt> is thrown.
149 *
150 * @param b the data.
151 * @param off the start offset in the data.
152 * @param len the number of bytes to write.
153 * @exception IOException if an I/O error occurs. In particular,
154 * an <code>IOException</code> is thrown if the output
155 * stream is closed.
156 *
157 */
158 public void write(byte[] b, int off, int len) throws IOException {
159 counter += len;
160 out.write(b, off, len);
161 }
162
163 public int getCounter() {
164 return counter;
165 }
166
167 public void resetCounter() {
168 counter = 0;
169 }
170 }
171