de.vxart.io
Class LimitedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by de.vxart.io.LimitedInputStream
All Implemented Interfaces:
java.io.Closeable

public class LimitedInputStream
extends java.io.FilterInputStream

Allows to only read a limited number of bytes from the underlying stream and then signals EOF.

Author:
Philipp Reichart, philipp.reichart@vxart.de

Constructor Summary
LimitedInputStream(java.io.InputStream in, long limit)
          Wraps the given InputStream and will only read as many bytes from it as the limit allows; it will then signal EOF by returning -1 from the read methods.
 
Method Summary
 int available()
           
 void close()
          "Closes" the InputStream by skipping all remaining bytes.
 long getBytesRead()
          Returns the number of bytes already read from this stream.
 long getBytesRemaining()
          Returns the number of bytes remaining before the limit of this stream.
 int read()
           
 int read(byte[] b)
           
 int read(byte[] b, int off, int len)
           
 long skip(long n)
           
 long skipAll()
          Skips all bytes left in this stream before the limit is reached.
 
Methods inherited from class java.io.FilterInputStream
mark, markSupported, reset
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LimitedInputStream

public LimitedInputStream(java.io.InputStream in,
                          long limit)
Wraps the given InputStream and will only read as many bytes from it as the limit allows; it will then signal EOF by returning -1 from the read methods.

Parameters:
in - the stream to limit read access to
limit - the number of bytes allowed to be read
Method Detail

getBytesRead

public long getBytesRead()
Returns the number of bytes already read from this stream.

Returns:
the number of bytes read from this stream

getBytesRemaining

public long getBytesRemaining()
Returns the number of bytes remaining before the limit of this stream.

Returns:
the number of bytes remaining before the limit

available

public int available()
              throws java.io.IOException
Overrides:
available in class java.io.FilterInputStream
Throws:
java.io.IOException

read

public int read()
         throws java.io.IOException
Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException
Overrides:
read in class java.io.FilterInputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException
Overrides:
skip in class java.io.FilterInputStream
Throws:
java.io.IOException

skipAll

public long skipAll()
             throws java.io.IOException
Skips all bytes left in this stream before the limit is reached. Equal to calling in.skip(in.getBytesRemaining()).

Returns:
the number of bytes skipped
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
"Closes" the InputStream by skipping all remaining bytes.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.FilterInputStream
Throws:
java.io.IOException