1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.io.output;
  17. import java.io.IOException;
  18. import java.io.OutputStream;
  19. /**
  20. * Used in debugging, it counts the number of bytes that pass
  21. * through it.
  22. *
  23. * @author <a href="mailto:bayard@apache.org">Henri Yandell</a>
  24. * @version $Id: CountingOutputStream.java,v 1.5 2004/02/23 04:40:29 bayard Exp $
  25. */
  26. public class CountingOutputStream extends ProxyOutputStream {
  27. private int count;
  28. /**
  29. * Constructs a CountingOutputStream.
  30. * @param out the OutputStream to write to
  31. */
  32. public CountingOutputStream( OutputStream out ) {
  33. super(out);
  34. }
  35. /** @see java.io.OutputStream#write(byte[]) */
  36. public void write(byte[] b) throws IOException {
  37. count += b.length;
  38. super.write(b);
  39. }
  40. /** @see java.io.OutputStream#write(byte[], int, int) */
  41. public void write(byte[] b, int off, int len) throws IOException {
  42. count += len;
  43. super.write(b, off, len);
  44. }
  45. /** @see java.io.OutputStream#write(int) */
  46. public void write(int b) throws IOException {
  47. count++;
  48. super.write(b);
  49. }
  50. /**
  51. * The number of bytes that have passed through this stream.
  52. * @return the number of bytes accumulated
  53. */
  54. public int getCount() {
  55. return this.count;
  56. }
  57. }