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.input;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  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: CountingInputStream.java,v 1.8 2004/02/23 04:38:52 bayard Exp $
  25. */
  26. public class CountingInputStream extends ProxyInputStream {
  27. private int count;
  28. /**
  29. * Constructs a new CountingInputStream.
  30. * @param in InputStream to delegate to
  31. */
  32. public CountingInputStream( InputStream in ) {
  33. super(in);
  34. }
  35. /**
  36. * Increases the count by super.read(b)'s return count
  37. *
  38. * @see java.io.InputStream#read(byte[])
  39. */
  40. public int read(byte[] b) throws IOException {
  41. int found = super.read(b);
  42. this.count += found;
  43. return found;
  44. }
  45. /**
  46. * Increases the count by super.read(b, off, len)'s return count
  47. *
  48. * @see java.io.InputStream#read(byte[], int, int)
  49. */
  50. public int read(byte[] b, int off, int len) throws IOException {
  51. int found = super.read(b, off, len);
  52. this.count += found;
  53. return found;
  54. }
  55. /**
  56. * Increases the count by 1.
  57. *
  58. * @see java.io.InputStream#read()
  59. */
  60. public int read() throws IOException {
  61. this.count++;
  62. return super.read();
  63. }
  64. /**
  65. * The number of bytes that have passed through this stream.
  66. *
  67. * @return the number of bytes accumulated
  68. */
  69. public int getCount() {
  70. return this.count;
  71. }
  72. }