annotate src/goodjava/io/CountingInputStream.java @ 1779:96dbbdf1e368

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 15 Aug 2023 23:52:20 -0600
parents 91c167099462
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.io;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.InputStream;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.IOException;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 public final class CountingInputStream extends NoMarkInputStream {
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 private long count = 0;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 public CountingInputStream(InputStream in) {
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 super(in);
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 }
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 public long count() {
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 return count;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 }
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 public int read() throws IOException {
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 int n = in.read();
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 if( n != -1 )
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 count++;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 return n;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 }
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 public int read(byte[] b, int off, int len) throws IOException {
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 int n = in.read(b,off,len);
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 if( n != -1 )
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 count += n;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 return n;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 public long skip(long n) throws IOException {
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 n = in.skip(n);
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 if( n != -1 )
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 count += n;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 return n;
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 }
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 }