1 32 package com.imagero.uio.io; 33 34 import java.io.FilterInputStream ; 35 import java.io.IOException ; 36 import java.io.InputStream ; 37 38 43 public class LimitedInputStream extends FilterInputStream { 44 protected int limit; 45 46 52 public LimitedInputStream(InputStream in, int limit) { 53 super(in); 54 this.limit = limit; 55 } 56 57 public int available() throws IOException { 58 return limit; 59 } 60 61 public int read() throws IOException { 62 if(limit-- <= 0) { 63 return -1; 64 } 65 return in.read(); 66 } 67 68 public int read(byte b[]) throws IOException { 69 return in.read(b, 0, b.length); 70 } 71 72 public int read(byte b[], int off, int len) throws IOException { 73 if(limit > 0) { 74 int length = Math.min(len, limit); 75 int res = in.read(b, off, length); 76 if(res > 0) { 77 limit -= res; 78 } 79 return res; 80 } 81 return -1; 82 } 83 84 public long skip(long n) throws IOException { 85 if(limit > 0) { 86 long length = Math.min(n, limit); 87 long res = in.skip(length); 88 if(res > 0) { 89 limit -= res; 90 } 91 return res; 92 } 93 return -1; 94 } 95 96 int mark; 97 98 public synchronized void mark(int readlimit) { 99 in.mark(readlimit); 100 mark = limit; 101 } 102 103 public synchronized void reset() throws IOException { 104 in.reset(); 105 limit = mark; 106 } 107 } | Popular Tags |