changeset 1494:91c167099462

more io
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 03 May 2020 11:51:31 -0600
parents 471ef3e6a84e
children 2e8a5df45d56
files src/goodjava/io/BufferedInputStream.java src/goodjava/io/CountingInputStream.java src/goodjava/io/FixedLengthInputStream.java src/goodjava/io/IoUtils.java src/goodjava/io/NoMarkInputStream.java src/goodjava/rpc/RpcCon.java
diffstat 6 files changed, 86 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/io/BufferedInputStream.java	Sun May 03 00:12:15 2020 -0600
+++ b/src/goodjava/io/BufferedInputStream.java	Sun May 03 11:51:31 2020 -0600
@@ -6,7 +6,6 @@
 package goodjava.io;
 
 import java.io.InputStream;
-import java.io.FilterInputStream;
 import java.io.IOException;
 
 /**
@@ -22,7 +21,7 @@
  * @author  Arthur van Hoff
  * @since   JDK1.0
  */
-public final class BufferedInputStream extends FilterInputStream {
+public final class BufferedInputStream extends NoMarkInputStream {
 	private final byte buf[];
 
 	/**
@@ -83,14 +82,13 @@
 
 	/**
 	 * Fills the buffer with more data.
-	 * Assumes that it is being called by a synchronized method.
 	 * This method also assumes that all data has already been read in,
 	 * hence pos > count.
 	 */
 	private void fill() throws IOException {
 		pos = 0;
 		count = 0;
-		int n = super.read(buf, 0, buf.length);
+		int n = in.read(buf, 0, buf.length);
 		if (n > 0)
 			count = n;
 	}
@@ -107,7 +105,7 @@
 	 *                          or an I/O error occurs.
 	 * @see        java.io.FilterInputStream#in
 	 */
-	public synchronized int read() throws IOException {
+	public int read() throws IOException {
 		if (pos >= count) {
 			fill();
 			if (pos >= count)
@@ -133,7 +131,7 @@
 			off += cnt;
 		}
 		if (len >= buf.length) {
-			return cnt + Math.max( 0, super.read(b, off, len) );
+			return cnt + Math.max( 0, in.read(b, off, len) );
 		}
 		fill();
 		if (count <= 0)
@@ -143,7 +141,7 @@
 		return cnt + len;
 	}
 
-	public synchronized int read(byte[] b, int off, int len) throws IOException {
+	public int read(byte[] b, int off, int len) throws IOException {
 		if( len == 0 )
 			return 0;
 		int n = read1(b,off,len);
@@ -159,7 +157,7 @@
 	 *                          invoking its {@link #close()} method, or an
 	 *                          I/O error occurs.
 	 */
-	public synchronized long skip(long n) throws IOException {
+	public long skip(long n) throws IOException {
 		if( n <= 0 )
 			return 0;
 		long skipped = 0;
@@ -171,7 +169,7 @@
 			if( n == 0 )
 				return skipped;
 		}
-		return skipped + super.skip(n);
+		return skipped + in.skip(n);
 	}
 
 	/**
@@ -191,22 +189,12 @@
 	 *                          invoking its {@link #close()} method,
 	 *                          or an I/O error occurs.
 	 */
-	public synchronized int available() throws IOException {
+	public int available() throws IOException {
 		int n = count - pos;
-		int avail = super.available();
+		int avail = in.available();
 		return n > (Integer.MAX_VALUE - avail)
 					? Integer.MAX_VALUE
 					: n + avail;
 	}
 
-	public void mark(int readlimit) {}
-
-	public void reset() throws IOException {
-		throw new IOException("mark/reset not supported");
-	}
-
-	public boolean markSupported() {
-		return false;
-	}
-
 }
--- /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;
+	}
+
+}
--- a/src/goodjava/io/FixedLengthInputStream.java	Sun May 03 00:12:15 2020 -0600
+++ b/src/goodjava/io/FixedLengthInputStream.java	Sun May 03 11:51:31 2020 -0600
@@ -1,12 +1,11 @@
 package goodjava.io;
 
 import java.io.InputStream;
-import java.io.FilterInputStream;
 import java.io.IOException;
 import java.io.EOFException;
 
 
-public class FixedLengthInputStream extends FilterInputStream {
+public class FixedLengthInputStream extends NoMarkInputStream {
 	protected long left;
 
 	public FixedLengthInputStream(InputStream in, long len) {
@@ -16,31 +15,31 @@
 		this.left = len;
 	}
 
-	public synchronized int read() throws IOException {
+	public int read() throws IOException {
 		if( left == 0 )
 			return -1;
-		int n = super.read();
+		int n = in.read();
 		if( n == -1 )
 			throw new EOFException();
 		left--;
 		return n;
 	}
 
-	public synchronized int read(byte[] b, int off, int len) throws IOException {
+	public int read(byte[] b, int off, int len) throws IOException {
 		if( len == 0 )
 			return 0;
 		if( left == 0 )
 			return -1;
 		if( len > left )
 			len = (int)left;
-		int n = super.read(b,off,len);
+		int n = in.read(b,off,len);
 		if( n == -1 )
 			throw new EOFException();
 		left -= n;
 		return n;
 	}
 
-	public synchronized long skip(long n) throws IOException {
+	public long skip(long n) throws IOException {
 		if( n > left )
 			n = left;
 		n = in.skip(n);
@@ -48,21 +47,11 @@
 		return n;
 	}
 
-	public synchronized int available() throws IOException {
+	public int available() throws IOException {
 		int n = in.available();
 		if( n > left )
 			n = (int)left;
 		return n;
 	}
 
-	public void mark(int readlimit) {}
-
-	public void reset() throws IOException {
-		throw new IOException("not supported");
-	}
-
-	public boolean markSupported() {
-		return false;
-	}
-
 }
--- a/src/goodjava/io/IoUtils.java	Sun May 03 00:12:15 2020 -0600
+++ b/src/goodjava/io/IoUtils.java	Sun May 03 11:51:31 2020 -0600
@@ -35,17 +35,14 @@
 		Files.createLink( to.toPath(), from.toPath() );
 	}
 
-	public static long copyAll(InputStream in,OutputStream out)
+	public static void copyAll(InputStream in,OutputStream out)
 		throws IOException
 	{
-		long total = 0;
 		byte[] a = new byte[8192];
 		int n;
 		while( (n=in.read(a)) != -1 ) {
 			out.write(a,0,n);
-			total += n;
 		}
-		return total;
 	}
 
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/io/NoMarkInputStream.java	Sun May 03 11:51:31 2020 -0600
@@ -0,0 +1,24 @@
+package goodjava.io;
+
+import java.io.InputStream;
+import java.io.FilterInputStream;
+import java.io.IOException;
+
+
+public class NoMarkInputStream extends FilterInputStream {
+
+	public NoMarkInputStream(InputStream in) {
+		super(in);
+	}
+
+	public final void mark(int readlimit) {}
+
+	public final void reset() throws IOException {
+		throw new IOException("mark/reset not supported");
+	}
+
+	public final boolean markSupported() {
+		return false;
+	}
+
+}
--- a/src/goodjava/rpc/RpcCon.java	Sun May 03 00:12:15 2020 -0600
+++ b/src/goodjava/rpc/RpcCon.java	Sun May 03 11:51:31 2020 -0600
@@ -12,6 +12,7 @@
 import goodjava.io.DataInputStream;
 import goodjava.io.DataOutputStream;
 import goodjava.io.IoUtils;
+import goodjava.io.CountingInputStream;
 
 
 public class RpcCon {
@@ -57,10 +58,12 @@
 		try {
 			out.writeString(json);
 			if( in != null ) {
-				long total = IoUtils.copyAll(in,out);
-				if( total != lenIn ) {
+				CountingInputStream countIn = new CountingInputStream(in);
+				IoUtils.copyAll(countIn,out);
+				countIn.close();
+				if( countIn.count() != lenIn ) {
 					close();
-					throw new RpcError("InputStream wrong length "+total+" when should be "+lenIn);
+					throw new RpcError("InputStream wrong length "+countIn.count()+" when should be "+lenIn);
 				}
 			}
 			out.flush();