com.evanmclean.evlib.io
Class BoundedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.evanmclean.evlib.io.BoundedInputStream
All Implemented Interfaces:
Closeable

public class BoundedInputStream
extends InputStream

Input stream wrapper which limits and optionally enforces the number of bytes read from an input stream. Particularly useful for reading from HTTP connections which can get out of whack if you don't read all the bytes, or try and read too many bytes from them. mark(int) and reset() will work if the underlying stream supports it.

Author:
Evan McLean McLean Computer Services (see the overview for copyright and licensing.)

Field Summary
protected  InputStream in
           
 
Constructor Summary
BoundedInputStream(InputStream in, long max)
           Only read a maximum number of characters from the input stream.
BoundedInputStream(InputStream in, long max, boolean eat)
          Only read a maximum number of characters from the input stream.
 
Method Summary
 int available()
           
 void close()
           
 long eat()
          Read and discard any remaining bytes in the input stream, or until we reach the end of the file, whichever comes first.
 long getRemaining()
          The remaining maximum number of bytes we can read.
 void mark(int read_limit)
           
 boolean markSupported()
           
 int read()
           
 int read(byte[] arr, int off, int len)
           
 void reset()
           
 long skip(long num)
           
 
Methods inherited from class java.io.InputStream
read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

protected final InputStream in
Constructor Detail

BoundedInputStream

public BoundedInputStream(InputStream in,
                          long max)

Only read a maximum number of characters from the input stream. If max is negative then it is effectively unbounded (actually sets the internal remaining bytes counter to Long.MAX_VALUE).

A call to close() will call eat() before closing the stream to read any remaining bytes.

Parameters:
in - The input stream to read.
max - The maximum number of bytes to read from the input stream.

BoundedInputStream

public BoundedInputStream(InputStream in,
                          long max,
                          boolean eat)
Only read a maximum number of characters from the input stream. If max is negative then it is effectively unbounded (actually sets the internal remaining bytes counter to Long.MAX_VALUE).

Parameters:
in - The input stream to read.
max - The maximum number of bytes to read from the input stream.
eat - If true, an implicit call to eat() is made on a call to close() to read any remaining bytes.
Method Detail

available

public int available()
              throws IOException
Overrides:
available in class InputStream
Throws:
IOException

close

public void close()
           throws IOException
Specified by:
close in interface Closeable
Overrides:
close in class InputStream
Throws:
IOException

eat

public long eat()
         throws IOException
Read and discard any remaining bytes in the input stream, or until we reach the end of the file, whichever comes first.

Returns:
The number of bytes discarded.
Throws:
IOException

getRemaining

public long getRemaining()
The remaining maximum number of bytes we can read.

Returns:
The remaining maximum number of bytes we can read.

mark

public void mark(int read_limit)
Overrides:
mark in class InputStream

markSupported

public boolean markSupported()
Overrides:
markSupported in class InputStream

read

public int read()
         throws IOException
Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] arr,
                int off,
                int len)
         throws IOException
Overrides:
read in class InputStream
Throws:
IOException

reset

public void reset()
           throws IOException
Overrides:
reset in class InputStream
Throws:
IOException

skip

public long skip(long num)
          throws IOException
Overrides:
skip in class InputStream
Throws:
IOException