KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > jclasslib > io > CountedInputStream


1 /*
2     This library is free software; you can redistribute it and/or
3     modify it under the terms of the GNU General Public
4     License as published by the Free Software Foundation; either
5     version 2 of the license, or (at your option) any later version.
6 */

7
8 package org.gjt.jclasslib.io;
9
10 import java.io.*;
11
12 /**
13     <tt>InputStream</tt> which counts the number of bytes read.
14  
15     @author <a HREF="mailto:jclasslib@ej-technologies.com">Ingo Kegel</a>
16     @version $Revision: 1.3 $ $Date: 2003/08/18 07:58:12 $
17 */

18 public class CountedInputStream extends FilterInputStream {
19
20     private int bytesRead = 0;
21
22     /**
23         Constructor.
24         @param in the input stream.
25      */

26     public CountedInputStream(InputStream in) {
27         super(in);
28     }
29     
30     public int read() throws IOException {
31         int b = in.read();
32         //if (b != -1) {
33
bytesRead++;
34         //}
35
return b;
36     }
37
38     public int read(byte[] b) throws IOException {
39         return read(b, 0, b.length);
40     }
41
42     public int read(byte[] b, int offset, int len) throws IOException {
43         int readCount = in.read(b, 0, b.length);
44         bytesRead += readCount;
45         return readCount;
46         
47     }
48     
49     public long skip(long n) throws IOException {
50         long skipCount = in.skip(n);
51         bytesRead += (int)skipCount;
52         return skipCount;
53     }
54
55     // Marking invalidates bytesRead
56
public boolean markSupported() {
57         return false;
58     }
59    
60     /**
61         Get the number of bytes read.
62         @return the number of bytes
63      */

64     public int getBytesRead() {
65         return bytesRead;
66     }
67 }
68
Popular Tags