annotate src/goodjava/io/FixedLengthInputStream.java @ 1606:7c7f28c724e8

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 20 Apr 2021 18:06:50 -0600
parents 91c167099462
children
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.IOException;
1490
9a2a2181a58f FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1480
diff changeset
5 import java.io.EOFException;
1480
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1490
diff changeset
8 public class FixedLengthInputStream extends NoMarkInputStream {
1490
9a2a2181a58f FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1480
diff changeset
9 protected long left;
1480
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
1490
9a2a2181a58f FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1480
diff changeset
11 public FixedLengthInputStream(InputStream in, long len) {
1480
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1490
diff changeset
18 public int read() throws IOException {
1490
9a2a2181a58f FixedLengthInputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1480
diff changeset
19 if( left == 0 )
1480
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 return -1;
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1490
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1490
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1490
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1490
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49
1494
91c167099462 more io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1490
diff changeset
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 }