X-Git-Url: http://dolda2000.com/gitweb/?p=jsvc.git;a=blobdiff_plain;f=src%2Fdolda%2Fjsvc%2Fscgi%2FLimitInputStream.java;fp=src%2Fdolda%2Fjsvc%2Fscgi%2FLimitInputStream.java;h=7a37f25366845caf8d577371feb0c5dfd5538d54;hp=0000000000000000000000000000000000000000;hb=13e578b10b388cc0dea88e05b79265c21416e3a0;hpb=b560fc1c45ee31c6d509781b53d5934121990189 diff --git a/src/dolda/jsvc/scgi/LimitInputStream.java b/src/dolda/jsvc/scgi/LimitInputStream.java new file mode 100644 index 0000000..7a37f25 --- /dev/null +++ b/src/dolda/jsvc/scgi/LimitInputStream.java @@ -0,0 +1,65 @@ +package dolda.jsvc.scgi; + +import java.io.*; + +public class LimitInputStream extends InputStream { + private final InputStream bk; + private final long limit; + private long read; + + public LimitInputStream(InputStream bk, long limit) { + this.bk = bk; + this.limit = limit; + } + + public void close() throws IOException { + bk.close(); + } + + public int available() throws IOException { + int av = bk.available(); + synchronized(this) { + if(av > limit - read) + av = (int)(limit - read); + return(av); + } + } + + public int read() throws IOException { + synchronized(this) { + if(read >= limit) + return(-1); + int ret = bk.read(); + if(ret >= 0) + read++; + return(ret); + } + } + + public int read(byte[] b) throws IOException { + return(read(b, 0, b.length)); + } + + public int read(byte[] b, int off, int len) throws IOException { + synchronized(this) { + if(read >= limit) + return(-1); + if(len > limit - read) + len = (int)(limit - read); + int ret = bk.read(b, off, len); + if(ret > 0) + read += ret; + return(ret); + } + } + + public long skip(long n) throws IOException { + synchronized(this) { + if(n > limit - read) + n = limit - read; + long ret = bk.skip(n); + read += ret; + return(ret); + } + } +}