annotate src/goodjava/io/LimitedInputStream.java @ 1485:6a24c8b33d6b

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 01 May 2020 11:23:29 -0600
parents 1f41e5921090
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;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 import java.io.InputStream;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.FilterInputStream;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.io.IOException;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 public final class LimitedInputStream extends FilterInputStream {
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 private final long limit;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 private long pos = 0;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 public LimitedInputStream(InputStream in, long limit) {
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 super(in);
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 this.limit = limit;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 public synchronized int read() throws IOException {
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 if( pos >= limit )
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 return -1;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 int n = super.read();
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 if( n != -1 )
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 pos++;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 public synchronized int read(byte[] b, int off, int len) throws IOException {
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 long avail = limit - pos;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 if( avail <= 0 )
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 return -1;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 if( len > avail )
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 len = (int)avail;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 int n = super.read(b,off,len);
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 if( n > 0 )
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 pos += n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 public synchronized long skip(long n) throws IOException {
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 long avail = limit - pos;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 if( avail <= 0 )
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 return 0;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 if( n > avail )
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 n = (int)avail;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 n = super.skip(n);
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 pos += n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 return n;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 }
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 public synchronized int available() throws IOException {
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 long avail = limit - pos;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 if( avail <= 0 )
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 return 0;
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 int n = super.available();
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 if( n > avail )
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 n = (int)avail;
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
1f41e5921090 input buffering
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 }