CountingInputStream.java revision 1e977d75f657dacb4cb5c36ab6054bcf66b08076
1/* 2 * CountingInputStream 3 * 4 * Author: Lasse Collin <lasse.collin@tukaani.org> 5 * 6 * This file has been put into the public domain. 7 * You can do whatever you want with this file. 8 */ 9 10package org.tukaani.xz; 11 12import java.io.FilterInputStream; 13import java.io.InputStream; 14import java.io.IOException; 15 16/** 17 * Counts the number of bytes read from an input stream. 18 */ 19class CountingInputStream extends FilterInputStream { 20 private long size = 0; 21 22 public CountingInputStream(InputStream in) { 23 super(in); 24 } 25 26 public int read() throws IOException { 27 int ret = in.read(); 28 if (ret != -1 && size >= 0) 29 ++size; 30 31 return ret; 32 } 33 34 public int read(byte[] b, int off, int len) throws IOException { 35 int ret = in.read(b, off, len); 36 if (ret > 0 && size >= 0) 37 size += ret; 38 39 return ret; 40 } 41 42 public long getSize() { 43 return size; 44 } 45} 46