diff src/goodjava/io/CountingInputStream.java @ 1494:91c167099462

more io
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 May 2020 11:51:31 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/io/CountingInputStream.java	Sun May 03 11:51:31 2020 -0600
@@ -0,0 +1,39 @@
+package goodjava.io;
+
+import java.io.InputStream;
+import java.io.IOException;
+
+
+public final class CountingInputStream extends NoMarkInputStream {
+	private long count = 0;
+
+	public CountingInputStream(InputStream in) {
+		super(in);
+	}
+
+	public long count() {
+		return count;
+	}
+
+	public int read() throws IOException {
+		int n = in.read();
+		if( n != -1 )
+			count++;
+		return n;
+	}
+
+	public int read(byte[] b, int off, int len) throws IOException {
+		int n = in.read(b,off,len);
+		if( n != -1 )
+			count += n;
+		return n;
+	}
+
+	public long skip(long n) throws IOException {
+		n = in.skip(n);
+		if( n != -1 )
+			count += n;
+		return n;
+	}
+
+}