Mercurial Hosting > luan
comparison src/goodjava/rpc/FixedLengthInputStream.java @ 1490:9a2a2181a58f
FixedLengthInputStream
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 02 May 2020 20:42:28 -0600 |
parents | 27efb1fcbcb5 |
children | 491b355acef7 |
comparison
equal
deleted
inserted
replaced
1489:fe237d72b234 | 1490:9a2a2181a58f |
---|---|
4 import java.io.FilterInputStream; | 4 import java.io.FilterInputStream; |
5 import java.io.IOException; | 5 import java.io.IOException; |
6 import java.io.EOFException; | 6 import java.io.EOFException; |
7 | 7 |
8 | 8 |
9 public class FixedLengthInputStream extends FilterInputStream { | 9 final class FixedLengthInputStream extends goodjava.io.FixedLengthInputStream { |
10 private long left; | |
11 | 10 |
12 public FixedLengthInputStream(InputStream in,long len) { | 11 public FixedLengthInputStream(InputStream in,long len) { |
13 super(in); | 12 super(in,len); |
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 } | 13 } |
57 | 14 |
58 public void close() throws IOException { | 15 public void close() throws IOException { |
59 while( left > 0 ) { | 16 while( left > 0 ) { |
60 if( skip(left) == 0 ) | 17 if( skip(left) == 0 ) |
61 throw new EOFException(); | 18 throw new EOFException(); |
62 } | 19 } |
63 } | 20 } |
64 | 21 |
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 } | 22 } |