com.Ostermiller.util Java Utilities


com.Ostermiller.util
Class SizeLimitInputStream

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

public class SizeLimitInputStream
extends InputStream

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

Since:
ostermillerutils 1.04.00
Author:
Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities

Constructor Summary
SizeLimitInputStream(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
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SizeLimitInputStream

public SizeLimitInputStream(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 IOException

Specified by:
read in class InputStream
Throws:
IOException

read

public int read(byte[] b)
         throws IOException

Overrides:
read in class InputStream
Throws:
IOException

read

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

Overrides:
read in class InputStream
Throws:
IOException

skip

public long skip(long n)
          throws IOException

Overrides:
skip in class InputStream
Throws:
IOException

available

public int available()
              throws IOException

Overrides:
available in class InputStream
Throws:
IOException

close

public void close()
           throws 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 Closeable
Overrides:
close in class InputStream
Throws:
IOException
Since:
ostermillerutils 1.04.00

mark

public void mark(int readlimit)

Overrides:
mark in class InputStream

reset

public void reset()
           throws IOException

Overrides:
reset in class InputStream
Throws:
IOException

markSupported

public boolean markSupported()

Overrides:
markSupported in class InputStream

com.Ostermiller.util Java Utilities


Copyright © 2001-2012 by Stephen Ostermiller