com.Ostermiller.util
Class SizeLimitInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by com.Ostermiller.util.SizeLimitInputStream
All Implemented Interfaces:
java.io.Closeable

public class SizeLimitInputStream
extends java.io.InputStream

An input stream wrapper that will read only a set number of bytes from the underlying stream.

Since:
ostermillerutils 1.04.00

Field Summary
protected  long bytesRead
          The number of bytes that have been read from this stream.
protected  long bytesReadSinceMark
          The number of bytes that have been read from this stream since mark() was called.
protected  java.io.InputStream in
          The input stream that is being protected.
protected  long markReadLimitBytes
          The number of bytes the user has request to have been marked for reset.
protected  long maxBytesToRead
          The number of bytes to read at most from this Stream.
 
Constructor Summary
SizeLimitInputStream(java.io.InputStream in, long maxBytesToRead)
          Create a new size limit input stream from another stream given a size limit.
 
Method Summary
 boolean allBytesRead()
          Tell whether the number of bytes specified in the constructor have been read yet.
 int available()
          
 void close()
          Close this stream and underlying streams.
 long getBytesLeft()
          Get the maximum number of bytes left to read before the limit (set in the constructor) is reached.
 long getBytesRead()
          Get the number of bytes actually read from this stream.
 long getMaxBytesToRead()
          Get the number of total bytes (including bytes already read) that can be read from this stream (as set in the constructor).
 void mark(int readlimit)
          
 boolean markSupported()
          
 int read()
          
 int read(byte[] b)
          
 int read(byte[] b, int off, int len)
          
 void reset()
          
 long skip(long n)
          
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

protected java.io.InputStream in
The input stream that is being protected. All methods should be forwarded to it, after checking the size that has been read.

Since:
ostermillerutils 1.04.00

maxBytesToRead

protected long maxBytesToRead
The number of bytes to read at most from this Stream. Read methods should check to ensure that bytesRead never exceeds maxBytesToRead.

Since:
ostermillerutils 1.04.00

bytesRead

protected long bytesRead
The number of bytes that have been read from this stream. Read methods should check to ensure that bytesRead never exceeds maxBytesToRead.

Since:
ostermillerutils 1.04.00

bytesReadSinceMark

protected long bytesReadSinceMark
The number of bytes that have been read from this stream since mark() was called.

Since:
ostermillerutils 1.04.00

markReadLimitBytes

protected long markReadLimitBytes
The number of bytes the user has request to have been marked for reset.

Since:
ostermillerutils 1.04.00
Constructor Detail

SizeLimitInputStream

public SizeLimitInputStream(java.io.InputStream in,
                            long maxBytesToRead)
Create a new size limit input stream from another stream given a size limit.

Parameters:
in - The input stream.
maxBytesToRead - the max number of bytes to allow to be read from the underlying stream.
Since:
ostermillerutils 1.04.00
Method Detail

getBytesRead

public long getBytesRead()
Get the number of bytes actually read from this stream.

Returns:
number of bytes that have already been taken from this stream.
Since:
ostermillerutils 1.04.00

getBytesLeft

public long getBytesLeft()
Get the maximum number of bytes left to read before the limit (set in the constructor) is reached.

Returns:
The number of bytes that (at a maximum) are left to be taken from this stream.
Since:
ostermillerutils 1.04.00

allBytesRead

public boolean allBytesRead()
Tell whether the number of bytes specified in the constructor have been read yet.

Returns:
true iff the specified number of bytes have all been read.
Since:
ostermillerutils 1.04.00

getMaxBytesToRead

public long getMaxBytesToRead()
Get the number of total bytes (including bytes already read) that can be read from this stream (as set in the constructor).

Returns:
Maximum bytes that can be read until the size limit runs out
Since:
ostermillerutils 1.04.00

read

public int read()
         throws java.io.IOException

Specified by:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b)
         throws java.io.IOException

Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException

Overrides:
read in class java.io.InputStream
Throws:
java.io.IOException

skip

public long skip(long n)
          throws java.io.IOException

Overrides:
skip in class java.io.InputStream
Throws:
java.io.IOException

available

public int available()
              throws java.io.IOException

Overrides:
available in class java.io.InputStream
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Close this stream and underlying streams. Calling this method may make data on the underlying stream unavailable.

Consider wrapping this stream in a NoCloseStream so that clients can call close() with no effect.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException
Since:
ostermillerutils 1.04.00

mark

public void mark(int readlimit)

Overrides:
mark in class java.io.InputStream

reset

public void reset()
           throws java.io.IOException

Overrides:
reset in class java.io.InputStream
Throws:
java.io.IOException

markSupported

public boolean markSupported()

Overrides:
markSupported in class java.io.InputStream