Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 1 | package aQute.lib.io; |
| 2 | |
| 3 | import java.io.*; |
| 4 | |
| 5 | public class LimitedInputStream extends InputStream { |
| 6 | |
| 7 | final InputStream in; |
| 8 | final int size; |
| 9 | int left; |
| 10 | |
| 11 | public LimitedInputStream(InputStream in, int size) { |
| 12 | this.in = in; |
| 13 | this.left = size; |
| 14 | this.size = size; |
| 15 | } |
| 16 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 17 | @Override |
| 18 | public int read() throws IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 19 | if (left <= 0) { |
| 20 | eof(); |
| 21 | return -1; |
| 22 | } |
| 23 | |
| 24 | left--; |
| 25 | return in.read(); |
| 26 | } |
| 27 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 28 | @Override |
| 29 | public int available() throws IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 30 | return Math.min(left, in.available()); |
| 31 | } |
| 32 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 33 | @Override |
| 34 | public void close() throws IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 35 | eof(); |
| 36 | in.close(); |
| 37 | } |
| 38 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 39 | protected void eof() {} |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 40 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 41 | @Override |
| 42 | public synchronized void mark(int readlimit) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 43 | throw new UnsupportedOperationException(); |
| 44 | } |
| 45 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 46 | @Override |
| 47 | public boolean markSupported() { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 48 | return false; |
| 49 | } |
| 50 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 51 | @Override |
| 52 | public int read(byte[] b, int off, int len) throws IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 53 | int min = Math.min(len, left); |
| 54 | if (min == 0) |
| 55 | return 0; |
| 56 | |
| 57 | int read = in.read(b, off, min); |
| 58 | if (read > 0) |
| 59 | left -= read; |
| 60 | return read; |
| 61 | } |
| 62 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 63 | @Override |
| 64 | public int read(byte[] b) throws IOException { |
| 65 | return read(b, 0, b.length); |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 68 | @Override |
| 69 | public synchronized void reset() throws IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 70 | throw new UnsupportedOperationException(); |
| 71 | } |
| 72 | |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 73 | @Override |
| 74 | public long skip(long n) throws IOException { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 75 | long count = 0; |
| 76 | byte buffer[] = new byte[1024]; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 77 | while (n > 0 && read() >= 0) { |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 78 | int size = read(buffer); |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 79 | if (size <= 0) |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 80 | return count; |
Stuart McCulloch | 2286f23 | 2012-06-15 13:27:53 +0000 | [diff] [blame] | 81 | count += size; |
| 82 | n -= size; |
Stuart McCulloch | bb01437 | 2012-06-07 21:57:32 +0000 | [diff] [blame] | 83 | } |
| 84 | return count; |
| 85 | } |
| 86 | } |