org.yajul.io
Class LimitedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by java.io.FilterInputStream
          extended by org.yajul.io.LimitedInputStream
All Implemented Interfaces:
java.io.Closeable

public class LimitedInputStream
extends java.io.FilterInputStream

A stream that will return an end of stream code (-1) when the maximum length is reached.
User: josh Date: Jan 17, 2004 Time: 12:56:05 PM


Field Summary
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
LimitedInputStream(java.io.InputStream in, int maxLength)
          Creates an filter intput stream that will read at most 'maxLength' bytes from the underlying stream.
 
Method Summary
 int read()
          Reads the next byte of data from this input stream.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from this input stream into an array of bytes.
 long skip(long n)
          Skips over and discards n bytes of data from the input stream.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LimitedInputStream

public LimitedInputStream(java.io.InputStream in,
                          int maxLength)
Creates an filter intput stream that will read at most 'maxLength' bytes from the underlying stream. The read methods will return -1 if the maximum length is reached.

Parameters:
in - The underlying input stream.
maxLength - The maximum number of bytes to read.
Method Detail

read

public int read()
         throws java.io.IOException
Reads the next byte of data from this input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

This method simply performs in.read() and returns the result.

Overrides:
read in class java.io.FilterInputStream
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
FilterInputStream.in

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads up to len bytes of data from this input stream into an array of bytes. This method blocks until some input is available.

This method simply performs in.read(b, off, len) and returns the result.

Overrides:
read in class java.io.FilterInputStream
Parameters:
b - the buffer into which the data is read.
off - the start offset of the data.
len - the maximum number of bytes read.
Returns:
the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached.
Throws:
java.io.IOException - if an I/O error occurs.
See Also:
FilterInputStream.in

skip

public long skip(long n)
          throws java.io.IOException
Skips over and discards n bytes of data from the input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. The actual number of bytes skipped is returned.

This method simply performs in.skip(n).

Overrides:
skip in class java.io.FilterInputStream
Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
java.io.IOException - if an I/O error occurs.


Copyright © 2008. All Rights Reserved.