KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > input > CountingInputStream


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
18 import java.io.IOException JavaDoc;
19 import java.io.InputStream JavaDoc;
20
21 /**
22  * Used in debugging, it counts the number of bytes that pass
23  * through it.
24  *
25  * @author <a HREF="mailto:bayard@apache.org">Henri Yandell</a>
26  * @version $Id: CountingInputStream.java,v 1.8 2004/02/23 04:38:52 bayard Exp $
27  */

28 public class CountingInputStream extends ProxyInputStream {
29
30     private int count;
31
32     /**
33      * Constructs a new CountingInputStream.
34      * @param in InputStream to delegate to
35      */

36     public CountingInputStream( InputStream JavaDoc in ) {
37         super(in);
38     }
39
40     /**
41      * Increases the count by super.read(b)'s return count
42      *
43      * @see java.io.InputStream#read(byte[])
44      */

45     public int read(byte[] b) throws IOException JavaDoc {
46         int found = super.read(b);
47         this.count += found;
48         return found;
49     }
50
51     /**
52      * Increases the count by super.read(b, off, len)'s return count
53      *
54      * @see java.io.InputStream#read(byte[], int, int)
55      */

56     public int read(byte[] b, int off, int len) throws IOException JavaDoc {
57         int found = super.read(b, off, len);
58         this.count += found;
59         return found;
60     }
61
62     /**
63      * Increases the count by 1.
64      *
65      * @see java.io.InputStream#read()
66      */

67     public int read() throws IOException JavaDoc {
68         this.count++;
69         return super.read();
70     }
71
72     /**
73      * The number of bytes that have passed through this stream.
74      *
75      * @return the number of bytes accumulated
76      */

77     public int getCount() {
78         return this.count;
79     }
80
81 }
82
Popular Tags