info.sswap.impl.empire.io
Class ByteLimitInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by info.sswap.impl.empire.io.ByteLimitInputStream
All Implemented Interfaces:
java.io.Closeable

public class ByteLimitInputStream
extends java.io.InputStream

A wrapper input stream that will not allow to transfer more than the specified amount of bytes

Author:
Blazej Bulka

Field Summary
private  long bytesRead
          The bytes already read
private  java.io.InputStream in
          The wrapped input stream.
private  long maxBytes
          The byte limit
 
Constructor Summary
ByteLimitInputStream(java.io.InputStream in, long maxBytes)
          Creates a new stream wrapping the underlying stream, with the specified byte limit
 
Method Summary
 void close()
          Closes the stream.
 long getBytesRead()
          Gets the number of bytes already read from the underlying stream.
 long getBytesRemaining()
          Gets the number of bytes remaining before the limit is reached.
 long getMaxBytes()
          Gets the byte limit for this stream
 int read()
          Reads a single byte from the underlying stream.
 int read(byte[] b, int off, int len)
          Reads multiple bytes from the underlying stream
 
Methods inherited from class java.io.InputStream
available, mark, markSupported, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

private java.io.InputStream in
The wrapped input stream.


maxBytes

private long maxBytes
The byte limit


bytesRead

private long bytesRead
The bytes already read

Constructor Detail

ByteLimitInputStream

public ByteLimitInputStream(java.io.InputStream in,
                            long maxBytes)
Creates a new stream wrapping the underlying stream, with the specified byte limit

Parameters:
in - the wrapped input stream
maxBytes - the byte limit
Method Detail

getBytesRead

public long getBytesRead()
Gets the number of bytes already read from the underlying stream.

Returns:
the number of bytes already read

getBytesRemaining

public long getBytesRemaining()
Gets the number of bytes remaining before the limit is reached.

Returns:
the number of bytes remaining

getMaxBytes

public long getMaxBytes()
Gets the byte limit for this stream

Returns:
the byte limit

read

public int read()
         throws java.io.IOException
Reads a single byte from the underlying stream.

Specified by:
read in class java.io.InputStream
Returns:
the byte read, or -1 if the end of the stream has been reached
Throws:
ByteLimitExceededException - if reading the byte would cause to go over the byte limit
java.io.IOException - if the underlying stream reports I/O problem

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads multiple bytes from the underlying stream

Overrides:
read in class java.io.InputStream
Parameters:
b - the byte array where the results will be placed
off - the offset where the first byte read should be placed in the array
len - the requested amount of bytes to read
Returns:
the amount of bytes actually read (it may be shorter for multiple reasons; these reasons include the premature end of stream (or other reason reported by the underlying stream), or reaching the byte limit)
Throws:
ByteLimitExceededException - if the limit has already been reached when invoking this method (if the limit is reached in the middle of the read, the read will be successfully completed, but will be truncated).
java.io.IOException - if the underlying stream reports an I/O problem

close

public void close()
           throws java.io.IOException
Closes the stream.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException - if the underlying stream reports a problem while closing a stream.


Copyright (c) 2011, iPlant Collaborative, University of Arizona, Cold Spring Harbor Laboratories, University of Texas at Austin.