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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1480
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.io;
1490
9a2a2181a58f FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1480
diff changeset
2
1480
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.InputStream;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.FilterInputStream;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.io.IOException;
1490
9a2a2181a58f FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1480
diff changeset
6 import java.io.EOFException;
1480
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11
1490
9a2a2181a58f FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1480
diff changeset
12 public FixedLengthInputStream(InputStream in, long len) {
1480
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 public synchronized int read() throws IOException {
1490
9a2a2181a58f FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1480
diff changeset
20 if( left == 0 )
1480
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 return -1;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 }