CountingInputStream.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package com.acdp.transceivr;
  2. import java.io.FilterInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. public class CountingInputStream extends FilterInputStream {
  6. private int count=0;
  7. public CountingInputStream(InputStream is) {
  8. super(is);
  9. }
  10. @Override
  11. public int read() throws IOException {
  12. int x=super.read();
  13. if(x>0)
  14. count++;
  15. return x;
  16. }
  17. @Override
  18. public int read(byte[] b) throws IOException {
  19. int bytes=super.read(b);
  20. count+=bytes;
  21. return bytes;
  22. }
  23. @Override
  24. public byte[] readNBytes(int len) throws IOException {
  25. byte[] ret= super.readNBytes(len);
  26. count+=ret.length;
  27. return ret;
  28. }
  29. @Override
  30. public byte[] readAllBytes() throws IOException {
  31. byte[] ret= super.readAllBytes();
  32. count+=ret.length;
  33. return ret;
  34. }
  35. @Override
  36. public int read(byte[] b, int off, int len) throws IOException {
  37. int c= super.read(b, off, len);
  38. count+=c;
  39. return c;
  40. }
  41. @Override
  42. public int readNBytes(byte[] b, int off, int len) throws IOException {
  43. int c= super.readNBytes(b, off, len);
  44. count+=c;
  45. return c;
  46. }
  47. public int getCount() {
  48. return count;
  49. }
  50. }