Mercurial Hosting > luan
annotate src/goodjava/io/FixedLengthInputStream.java @ 1678:f54484aae295
fix
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 31 May 2022 14:44:29 -0600 |
parents | 91c167099462 |
children |
rev | line source |
---|---|
1480 | 1 package goodjava.io; |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
2 |
1480 | 3 import java.io.InputStream; |
4 import java.io.IOException; | |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
5 import java.io.EOFException; |
1480 | 6 |
7 | |
1494 | 8 public class FixedLengthInputStream extends NoMarkInputStream { |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
9 protected long left; |
1480 | 10 |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
11 public FixedLengthInputStream(InputStream in, long len) { |
1480 | 12 super(in); |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
13 if( len < 0 ) |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
14 throw new IllegalArgumentException("len can't be negative"); |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
15 this.left = len; |
1480 | 16 } |
17 | |
1494 | 18 public int read() throws IOException { |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
19 if( left == 0 ) |
1480 | 20 return -1; |
1494 | 21 int n = in.read(); |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
22 if( n == -1 ) |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
23 throw new EOFException(); |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
24 left--; |
1480 | 25 return n; |
26 } | |
27 | |
1494 | 28 public int read(byte[] b, int off, int len) throws IOException { |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
29 if( len == 0 ) |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
30 return 0; |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
31 if( left == 0 ) |
1480 | 32 return -1; |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
33 if( len > left ) |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
34 len = (int)left; |
1494 | 35 int n = in.read(b,off,len); |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
36 if( n == -1 ) |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
37 throw new EOFException(); |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
38 left -= n; |
1480 | 39 return n; |
40 } | |
41 | |
1494 | 42 public long skip(long n) throws IOException { |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
43 if( n > left ) |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
44 n = left; |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
45 n = in.skip(n); |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
46 left -= n; |
1480 | 47 return n; |
48 } | |
49 | |
1494 | 50 public int available() throws IOException { |
1490
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
51 int n = in.available(); |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
52 if( n > left ) |
9a2a2181a58f
FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents:
1480
diff
changeset
|
53 n = (int)left; |
1480 | 54 return n; |
55 } | |
56 | |
57 } |