1494
|
1 package goodjava.io;
|
|
2
|
|
3 import java.io.InputStream;
|
|
4 import java.io.IOException;
|
|
5
|
|
6
|
|
7 public final class CountingInputStream extends NoMarkInputStream {
|
|
8 private long count = 0;
|
|
9
|
|
10 public CountingInputStream(InputStream in) {
|
|
11 super(in);
|
|
12 }
|
|
13
|
|
14 public long count() {
|
|
15 return count;
|
|
16 }
|
|
17
|
|
18 public int read() throws IOException {
|
|
19 int n = in.read();
|
|
20 if( n != -1 )
|
|
21 count++;
|
|
22 return n;
|
|
23 }
|
|
24
|
|
25 public int read(byte[] b, int off, int len) throws IOException {
|
|
26 int n = in.read(b,off,len);
|
|
27 if( n != -1 )
|
|
28 count += n;
|
|
29 return n;
|
|
30 }
|
|
31
|
|
32 public long skip(long n) throws IOException {
|
|
33 n = in.skip(n);
|
|
34 if( n != -1 )
|
|
35 count += n;
|
|
36 return n;
|
|
37 }
|
|
38
|
|
39 }
|