annotate src/goodjava/rpc/FixedLengthInputStream.java @ 1467:509d49c493c0

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 13 Apr 2020 22:16:59 -0600
parents 27efb1fcbcb5
children 9a2a2181a58f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1118
diff changeset
1 package goodjava.rpc;
1118
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.InputStream;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.FilterInputStream;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.io.IOException;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.io.EOFException;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 public class FixedLengthInputStream extends FilterInputStream {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 private long left;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 public FixedLengthInputStream(InputStream in,long len) {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 super(in);
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 if( len < 0 )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 throw new IllegalArgumentException("len can't be negative");
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 this.left = len;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 public int read() throws IOException {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 if( left == 0 )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 return -1;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 int n = in.read();
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 if( n == -1 )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 throw new EOFException();
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 left--;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 return n;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 public int read(byte b[], int off, int len) throws IOException {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 if( len == 0 )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 return 0;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 if( left == 0 )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 return -1;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 if( len > left )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 len = (int)left;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 int n = in.read(b, off, len);
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 if( n == -1 )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 throw new EOFException();
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 left -= n;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 return n;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 public long skip(long n) throws IOException {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 if( n > left )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 n = left;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 n = in.skip(n);
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 left -= n;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 return n;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 public int available() throws IOException {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 int n = in.available();
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 if( n > left )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 n = (int)left;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 return n;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 public void close() throws IOException {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 while( left > 0 ) {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 if( skip(left) == 0 )
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 throw new EOFException();
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 public void mark(int readlimit) {}
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 public void reset() throws IOException {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 throw new IOException("not supported");
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 public boolean markSupported() {
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 return false;
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 }
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74
e4710ddfd287 start luan/lib/rpc
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 }