Count the number of bytes read through the stream : Stream « File Input Output « Java

Count the number of bytes read through the stream

// $Id:,v 1.1 2001/04/11 19:03:06 ylafon Exp $
// (c) COPYRIGHT MIT, INRIA and Keio, 2001.
// Please first read the full copyright statement in file COPYRIGHT.html


 * count the number of bytes read through the stream
public class CountInputStream extends InputStream {
  long count = 0;

  long marked = -1;

  InputStream is;

  public int available() throws IOException {
    return is.available();

  public boolean markSupported() {
    return is.markSupported();

  public int read() throws IOException {
    int r =;
    if (r > 0) {
    return r;

  public int read(byte[] b, int off, int len) throws IOException {
    int r =, off, len);
    if (r > 0) {
      count += r;
    return r;

  public long skip(long skipped) throws IOException {
    long l = is.skip(skipped);
    if (l > 0) {
      count += l;
    return l;

  public void mark(int readlimit) {
    marked = count;

  public void reset() throws IOException {
    count = marked;

  public void close() throws IOException {

   * get the actual number of bytes read
   * @return a long, the number of bytes read
  public long getBytesRead() {
    return count;

  public CountInputStream(InputStream is) { = is;


Related examples in the same category

1.Show the content of a file
2.Some general utility functions for dealing with Streams
3.Utilities related to file and stream handling.
4.Utility functions related to Streams
5.Utility methods for handling streams
6.Various utility methods that have something to do with I/O
7.General IO Stream manipulation
8.General IO stream manipulation utilities
9.Count OutputStream
10.File utilities for file read and write
11.An InputStream class that terminates the stream when it encounters a particular byte sequence.
12.An InputStream that implements HTTP/1.1 chunking
13.An OutputStream which relays all data written into it into a list of given OutputStreams
14.Utility code for dealing with different endian systems
15.Copy From Stream To File
16.Copy Inputstream To File
17.Load Stream Into String
18.Reads the content of an input stream and writes it into an output stream.