comparison src/goodjava/rpc/FixedLengthInputStream.java @ 1402:27efb1fcbcb5

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