01: // CountInputStream.java
02: // $Id: CountInputStream.java,v 1.1 2001/04/11 19:03:06 ylafon Exp $
03: // (c) COPYRIGHT MIT, INRIA and Keio, 2001.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.util;
07:
08: import java.io.InputStream;
09: import java.io.IOException;
10:
11: /**
12: * count the number of bytes read through the stream
13: */
14: public class CountInputStream extends InputStream {
15: long count = 0;
16: long marked = -1;
17:
18: InputStream is;
19:
20: public int available() throws IOException {
21: return is.available();
22: }
23:
24: public boolean markSupported() {
25: return is.markSupported();
26: }
27:
28: public int read() throws IOException {
29: int r = is.read();
30: if (r > 0) {
31: count++;
32: }
33: return r;
34: }
35:
36: public int read(byte[] b, int off, int len) throws IOException {
37: int r = is.read(b, off, len);
38: if (r > 0) {
39: count += r;
40: }
41: return r;
42: }
43:
44: public long skip(long skipped) throws IOException {
45: long l = is.skip(skipped);
46: if (l > 0) {
47: count += l;
48: }
49: return l;
50: }
51:
52: public void mark(int readlimit) {
53: is.mark(readlimit);
54: marked = count;
55: }
56:
57: public void reset() throws IOException {
58: is.reset();
59: count = marked;
60: }
61:
62: public void close() throws IOException {
63: is.close();
64: }
65:
66: /**
67: * get the actual number of bytes read
68: * @return a long, the number of bytes read
69: */
70: public long getBytesRead() {
71: return count;
72: }
73:
74: public CountInputStream(InputStream is) {
75: this.is = is;
76: }
77: }
|