net.noderunner.http
Class LimitedInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by net.noderunner.http.LimitedInputStream
All Implemented Interfaces:
java.io.Closeable

public class LimitedInputStream
extends java.io.InputStream

An InputStream wrapper which allows only a certain number of bytes to be read. This is used in conjunction reading from HTTP requests or responses with supplied Content-Length headers. This stream is unsynchronized.

Author:
Elias Ross
See Also:
LimitedOutputStream

Constructor Summary
LimitedInputStream(java.io.InputStream stream, int lengthLimit)
          Constructs a limited input stream.
 
Method Summary
 int available()
          Calls the wrapped stream.
 void close()
          Closes the underlying input stream.
 void mark(int readlimit)
          Calls the wrapped stream.
 boolean markSupported()
          Calls the wrapped stream.
 int read()
          Reads and returns a single byte from this input stream, or -1 if end of file or the read limit has been encountered.
 int read(byte[] b, int off, int len)
          Reads up to len bytes of data from the input stream into an array of bytes, possibly less if the read limit is reached.
 void reset()
          Calls the wrapped stream.
 java.lang.String toString()
          Returns a debug string.
 
Methods inherited from class java.io.InputStream
read, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LimitedInputStream

public LimitedInputStream(java.io.InputStream stream,
                          int lengthLimit)
Constructs a limited input stream.

Parameters:
stream - Must be non-null.
lengthLimit - limit of bytes of input.
Method Detail

close

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

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

read

public int read(byte[] b,
                int off,
                int len)
         throws java.io.IOException
Reads up to len bytes of data from the input stream into an array of bytes, possibly less if the read limit is reached.

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

available

public int available()
              throws java.io.IOException
Calls the wrapped stream.

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

markSupported

public boolean markSupported()
Calls the wrapped stream.

Overrides:
markSupported in class java.io.InputStream

reset

public void reset()
           throws java.io.IOException
Calls the wrapped stream.

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

mark

public void mark(int readlimit)
Calls the wrapped stream.

Overrides:
mark in class java.io.InputStream

read

public int read()
         throws java.io.IOException
Reads and returns a single byte from this input stream, or -1 if end of file or the read limit has been encountered.

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

toString

public java.lang.String toString()
Returns a debug string.

Overrides:
toString in class java.lang.Object


Copyright © 2010. All Rights Reserved.