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