Mercurial Hosting > luan
comparison src/goodjava/io/FixedLengthInputStream.java @ 1494:91c167099462
more io
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 03 May 2020 11:51:31 -0600 |
parents | 9a2a2181a58f |
children |
comparison
equal
deleted
inserted
replaced
1493:471ef3e6a84e | 1494:91c167099462 |
---|---|
1 package goodjava.io; | 1 package goodjava.io; |
2 | 2 |
3 import java.io.InputStream; | 3 import java.io.InputStream; |
4 import java.io.FilterInputStream; | |
5 import java.io.IOException; | 4 import java.io.IOException; |
6 import java.io.EOFException; | 5 import java.io.EOFException; |
7 | 6 |
8 | 7 |
9 public class FixedLengthInputStream extends FilterInputStream { | 8 public class FixedLengthInputStream extends NoMarkInputStream { |
10 protected long left; | 9 protected long left; |
11 | 10 |
12 public FixedLengthInputStream(InputStream in, long len) { | 11 public FixedLengthInputStream(InputStream in, long len) { |
13 super(in); | 12 super(in); |
14 if( len < 0 ) | 13 if( len < 0 ) |
15 throw new IllegalArgumentException("len can't be negative"); | 14 throw new IllegalArgumentException("len can't be negative"); |
16 this.left = len; | 15 this.left = len; |
17 } | 16 } |
18 | 17 |
19 public synchronized int read() throws IOException { | 18 public int read() throws IOException { |
20 if( left == 0 ) | 19 if( left == 0 ) |
21 return -1; | 20 return -1; |
22 int n = super.read(); | 21 int n = in.read(); |
23 if( n == -1 ) | 22 if( n == -1 ) |
24 throw new EOFException(); | 23 throw new EOFException(); |
25 left--; | 24 left--; |
26 return n; | 25 return n; |
27 } | 26 } |
28 | 27 |
29 public synchronized int read(byte[] b, int off, int len) throws IOException { | 28 public int read(byte[] b, int off, int len) throws IOException { |
30 if( len == 0 ) | 29 if( len == 0 ) |
31 return 0; | 30 return 0; |
32 if( left == 0 ) | 31 if( left == 0 ) |
33 return -1; | 32 return -1; |
34 if( len > left ) | 33 if( len > left ) |
35 len = (int)left; | 34 len = (int)left; |
36 int n = super.read(b,off,len); | 35 int n = in.read(b,off,len); |
37 if( n == -1 ) | 36 if( n == -1 ) |
38 throw new EOFException(); | 37 throw new EOFException(); |
39 left -= n; | 38 left -= n; |
40 return n; | 39 return n; |
41 } | 40 } |
42 | 41 |
43 public synchronized long skip(long n) throws IOException { | 42 public long skip(long n) throws IOException { |
44 if( n > left ) | 43 if( n > left ) |
45 n = left; | 44 n = left; |
46 n = in.skip(n); | 45 n = in.skip(n); |
47 left -= n; | 46 left -= n; |
48 return n; | 47 return n; |
49 } | 48 } |
50 | 49 |
51 public synchronized int available() throws IOException { | 50 public int available() throws IOException { |
52 int n = in.available(); | 51 int n = in.available(); |
53 if( n > left ) | 52 if( n > left ) |
54 n = (int)left; | 53 n = (int)left; |
55 return n; | 54 return n; |
56 } | 55 } |
57 | 56 |
58 public void mark(int readlimit) {} | |
59 | |
60 public void reset() throws IOException { | |
61 throw new IOException("not supported"); | |
62 } | |
63 | |
64 public boolean markSupported() { | |
65 return false; | |
66 } | |
67 | |
68 } | 57 } |