changeset 1490:9a2a2181a58f

FixedLengthInputStream
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2020 20:42:28 -0600
parents fe237d72b234
children 491b355acef7
files src/goodjava/io/FixedLengthInputStream.java src/goodjava/io/LimitedInputStream.java src/goodjava/lucene/logging/LogFile.java src/goodjava/rpc/FixedLengthInputStream.java
diffstat 4 files changed, 72 insertions(+), 115 deletions(-) [+]
line wrap: on
line diff
diff -r fe237d72b234 -r 9a2a2181a58f src/goodjava/io/FixedLengthInputStream.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/io/FixedLengthInputStream.java	Sat May 02 20:42:28 2020 -0600
@@ -0,0 +1,68 @@
+package goodjava.io;
+
+import java.io.InputStream;
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.EOFException;
+
+
+public class FixedLengthInputStream extends FilterInputStream {
+	protected long left;
+
+	public FixedLengthInputStream(InputStream in, long len) {
+		super(in);
+		if( len < 0 )
+			throw new IllegalArgumentException("len can't be negative");
+		this.left = len;
+	}
+
+	public synchronized int read() throws IOException {
+		if( left == 0 )
+			return -1;
+		int n = super.read();
+		if( n == -1 )
+			throw new EOFException();
+		left--;
+		return n;
+	}
+
+	public synchronized 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);
+		if( n == -1 )
+			throw new EOFException();
+		left -= n;
+		return n;
+	}
+
+	public synchronized long skip(long n) throws IOException {
+		if( n > left )
+			n = left;
+		n = in.skip(n);
+		left -= n;
+		return n;
+	}
+
+	public synchronized 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;
+	}
+
+}
diff -r fe237d72b234 -r 9a2a2181a58f src/goodjava/io/LimitedInputStream.java
--- a/src/goodjava/io/LimitedInputStream.java	Sat May 02 16:28:24 2020 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,58 +0,0 @@
-package goodjava.io;
-import java.io.InputStream;
-import java.io.FilterInputStream;
-import java.io.IOException;
-
-
-public final class LimitedInputStream extends FilterInputStream {
-	private final long limit;
-	private long pos = 0;
-
-	public LimitedInputStream(InputStream in, long limit) {
-		super(in);
-		this.limit = limit;
-	}
-
-	public synchronized int read() throws IOException {
-		if( pos >= limit )
-			return -1;
-		int n = super.read();
-		if( n != -1 )
-			pos++;
-		return n;
-	}
-
-	public synchronized int read(byte[] b, int off, int len) throws IOException {
-		long avail = limit - pos;
-		if( avail <= 0 )
-			return -1;
-		if( len > avail )
-			len = (int)avail;
-		int n = super.read(b,off,len);
-		if( n > 0 )
-			pos += n;
-		return n;
-	}
-
-	public synchronized long skip(long n) throws IOException {
-		long avail = limit - pos;
-		if( avail <= 0 )
-			return 0;
-		if( n > avail )
-			n = (int)avail;
-		n = super.skip(n);
-		pos += n;
-		return n;
-	}
-
-	public synchronized int available() throws IOException {
-		long avail = limit - pos;
-		if( avail <= 0 )
-			return 0;
-		int n = super.available();
-		if( n > avail )
-			n = (int)avail;
-		return n;
-	}
-
-}
diff -r fe237d72b234 -r 9a2a2181a58f src/goodjava/lucene/logging/LogFile.java
--- a/src/goodjava/lucene/logging/LogFile.java	Sat May 02 16:28:24 2020 -0600
+++ b/src/goodjava/lucene/logging/LogFile.java	Sat May 02 20:42:28 2020 -0600
@@ -10,7 +10,7 @@
 import java.io.IOException;
 import goodjava.logging.Logger;
 import goodjava.logging.LoggerFactory;
-import goodjava.io.LimitedInputStream;
+import goodjava.io.FixedLengthInputStream;
 import goodjava.io.BufferedInputStream;
 
 
@@ -54,7 +54,7 @@
 
 	public LogInputStream input() throws IOException {
 		InputStream in = new FileInputStream(file);
-		in = new LimitedInputStream(in,end);
+		in = new FixedLengthInputStream(in,end);
 		in = new BufferedInputStream(in);
 		LogInputStream lis = newLogInputStream(in);
 		lis.readLong();  // skip end
diff -r fe237d72b234 -r 9a2a2181a58f src/goodjava/rpc/FixedLengthInputStream.java
--- a/src/goodjava/rpc/FixedLengthInputStream.java	Sat May 02 16:28:24 2020 -0600
+++ b/src/goodjava/rpc/FixedLengthInputStream.java	Sat May 02 20:42:28 2020 -0600
@@ -6,53 +6,10 @@
 import java.io.EOFException;
 
 
-public class FixedLengthInputStream extends FilterInputStream {
-	private long left;
+final class FixedLengthInputStream extends goodjava.io.FixedLengthInputStream {
 
 	public FixedLengthInputStream(InputStream in,long len) {
-		super(in);
-		if( len < 0 )
-			throw new IllegalArgumentException("len can't be negative");
-		this.left = len;
-	}
-
-	public int read() throws IOException {
-		if( left == 0 )
-			return -1;
-		int n = in.read();
-		if( n == -1 )
-			throw new EOFException();
-		left--;
-		return n;
-	}
-
-	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 = in.read(b, off, len);
-		if( n == -1 )
-			throw new EOFException();
-		left -= n;
-		return n;
-	}
-
-	public long skip(long n) throws IOException {
-		if( n > left )
-			n = left;
-		n = in.skip(n);
-		left -= n;
-		return n;
-	}
-
-	public int available() throws IOException {
-		int n = in.available();
-		if( n > left )
-			n = (int)left;
-		return n;
+		super(in,len);
 	}
 
     public void close() throws IOException {
@@ -62,14 +19,4 @@
 		}
     }
 
-	public void mark(int readlimit) {}
-
-	public void reset() throws IOException {
-		throw new IOException("not supported");
-	}
-
-	public boolean markSupported() {
-		return false;
-	}
-
 }