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 10 package org.tukaani.xz; 11 12 import java.io.FilterInputStream; 13 import java.io.InputStream; 14 import java.io.IOException; 15 16 /** 17 * Counts the number of bytes read from an input stream. 18 * The <code>close()</code> method does nothing, that is, the underlying 19 * <code>InputStream</code> isn't closed. 20 */ 21 class CountingInputStream extends CloseIgnoringInputStream { 22 private long size = 0; 23 CountingInputStream(InputStream in)24 public CountingInputStream(InputStream in) { 25 super(in); 26 } 27 read()28 public int read() throws IOException { 29 int ret = in.read(); 30 if (ret != -1 && size >= 0) 31 ++size; 32 33 return ret; 34 } 35 read(byte[] b, int off, int len)36 public int read(byte[] b, int off, int len) throws IOException { 37 int ret = in.read(b, off, len); 38 if (ret > 0 && size >= 0) 39 size += ret; 40 41 return ret; 42 } 43 getSize()44 public long getSize() { 45 return size; 46 } 47 } 48