eap.tar
Class LimitedInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byeap.tar.LimitedInputStream

public class LimitedInputStream
extends InputStream

Reads a subset of another stream. All the I/O is redirected to an undelying stream, but this stream will signal EOF after a preset number of bytes have been read.


Constructor Summary
LimitedInputStream(InputStream in, long length)
          Create a new stream.
 
Method Summary
 int available()
          Returns the number of bytes remaining before EOF.
 void close()
          This method skips to the limit, but does not close the underlying input stream.
 long getBytesRead()
          Returns the number of bytes which have been read from this stream since it was created.
 long getLength()
          Returns the total number of bytes which can be read by this stream.
 void mark(int read_limit)
          sets a mark in the underlying stream.
 boolean markSupported()
          Returns true if the underlying stream supports marks.
 int read()
          Read a single byte
 int read(byte[] buffer)
          Read a block of bytes.
 int read(byte[] buffer, int offset, int length)
          Read a block of bytes.
 void reset()
          Resets the underlying stream and also adjusts the bytes read accounting.
 long skip(long nbytes)
          Skip over a number of bytes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LimitedInputStream

public LimitedInputStream(InputStream in,
                          long length)
Create a new stream.

Parameters:
in - The underlying stream to which I/O will be redirected.
length - The number of bytes which can be read before signaling EOF.
Method Detail

getBytesRead

public long getBytesRead()
Returns the number of bytes which have been read from this stream since it was created.


getLength

public long getLength()
Returns the total number of bytes which can be read by this stream.


available

public int available()
Returns the number of bytes remaining before EOF. Note that the underlying stream may not actually have this number of bytes available. This means that the I/O could block if you try to read this many bytes. This is bad behavior, since it breaks the contract for what availabel is sposed to do. So I should change this, and instead rely on getBytesRead() for this functionality. This will be important if you want to abort in the middle of a "stuck" download.


close

public void close()
           throws IOException
This method skips to the limit, but does not close the underlying input stream. It does nothing if the stream is already at the end, so calling this method more than once does no harm.

Throws:
IOException

read

public int read()
         throws IOException
Read a single byte

Throws:
IOException

read

public int read(byte[] buffer,
                int offset,
                int length)
         throws IOException
Read a block of bytes.

Throws:
IOException

read

public int read(byte[] buffer)
         throws IOException
Read a block of bytes.

Throws:
IOException

skip

public long skip(long nbytes)
          throws IOException
Skip over a number of bytes.

Throws:
IOException

markSupported

public boolean markSupported()
Returns true if the underlying stream supports marks.


mark

public void mark(int read_limit)
sets a mark in the underlying stream.


reset

public void reset()
           throws IOException
Resets the underlying stream and also adjusts the bytes read accounting.

Throws:
IOException