infoservice.mailsystem.central.server.util
Class LimitedLengthInputStream

java.lang.Object
  extended by java.io.InputStream
      extended by infoservice.mailsystem.central.server.util.LimitedLengthInputStream
All Implemented Interfaces:
java.io.Closeable

public class LimitedLengthInputStream
extends java.io.InputStream

This class is the implementation for an InputStream where only a specified number of bytes can be read from.


Field Summary
private  long m_readLimit
          Stores the maximum number of bytes which can still be read from the stream.
private  java.io.InputStream m_underlyingStream
          Stores the underlying stream from which the data are read.
 
Constructor Summary
LimitedLengthInputStream(java.io.InputStream a_underlyingStream, long a_maximumLength)
          Creates an input stream with a limited length.
 
Method Summary
 int available()
          Returns the number of bytes which can be read from this input stream without blocking.
 void close()
          Closes the underlying stream.
 long getRemainingBytes()
          Returns the number of remaining bytes for the read limit.
 int read()
          Reads one byte from the underlying InputStream.
 int read(byte[] a_buffer)
          Reads bytes into a portion of an array.
 int read(byte[] a_buffer, int a_offset, int a_length)
          Reads bytes into a portion of an array.
 
Methods inherited from class java.io.InputStream
mark, markSupported, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_underlyingStream

private java.io.InputStream m_underlyingStream
Stores the underlying stream from which the data are read.


m_readLimit

private long m_readLimit
Stores the maximum number of bytes which can still be read from the stream.

Constructor Detail

LimitedLengthInputStream

public LimitedLengthInputStream(java.io.InputStream a_underlyingStream,
                                long a_maximumLength)
Creates an input stream with a limited length. After reading the specified number of bytes, the end of this stream is reached (any read call will return -1).

Parameters:
a_underlyingStream - The stream to read the data from. If this is null, this constructor will throw a NullPointerException.
a_maximumLength - The maximum number of bytes to read from the underlying stream. This value needs to be >= -1. A value of -1 disables the limit end enables reading until the end of the underlying stream is reached.
Method Detail

read

public int read()
         throws java.io.IOException
Reads one byte from the underlying InputStream. If the call is successful, the limit of bytes able to read from the stream is also decremented by 1. If the byte limit is exhausted -1 is returned and no byte is read from the underlying stream. In the case of a read exception, that exception is thrown.

Specified by:
read in class java.io.InputStream
Returns:
The byte read from the stream or -1, if the end of the underlying stream or the read limit is reached.
Throws:
java.io.IOException - If an I/O error occurs.

read

public int read(byte[] a_buffer)
         throws java.io.IOException
Reads bytes into a portion of an array. This method will block until some input is available, an I/O error occurs or the end of the stream or the byte limit is reached. It's always tried to fill the buffer completely with data from the underlying stream. If the end of stream is detected or the byte limit is reached, this method returns the number of bytes read until the end of the stream or the byte limit. If the end of stream is reached or the byte limit is exhausted and no byte was read from the stream, -1 is returned (of course only, if at least one byte should be read from the stream).

Overrides:
read in class java.io.InputStream
Parameters:
a_buffer - The destination buffer.
Returns:
The number of bytes read, or -1 if the end of the stream was reached or the read limit is already exhausted and no byte could be read.
Throws:
java.io.IOException - If an I/O error occurs.

read

public int read(byte[] a_buffer,
                int a_offset,
                int a_length)
         throws java.io.IOException
Reads bytes into a portion of an array. This method will block until some input is available, an I/O error occurs or the end of the stream or the byte limit is reached. It's always tried to fill the buffer completely with data from the underlying stream. If the end of stream is detected or the byte limit is reached, this method returns the number of bytes read until the end of the stream or the byte limit. If the end of stream is reached or the byte limit is exhausted and no byte was read from the stream, -1 is returned (of course only, if at least one byte should be read from the stream).

Overrides:
read in class java.io.InputStream
Parameters:
a_buffer - The destination buffer.
a_offset - The offset in the destination buffer at which to start storing bytes.
a_length - The maximum number of bytes to read.
Returns:
The number of bytes read, or -1 if the end of the stream was reached or the read limit is already exhausted and no byte could be read.
Throws:
java.io.IOException - If an I/O error occurs.

available

public int available()
              throws java.io.IOException
Returns the number of bytes which can be read from this input stream without blocking. If a read limit was specified, this number is never bigger than the number of remaining bytes from the read limit.

Overrides:
available in class java.io.InputStream
Returns:
The number of bytes which can be read from the stream without blocking or reaching the end of the read limit.
Throws:
java.io.IOException - If an I/O error occurs.

close

public void close()
           throws java.io.IOException
Closes the underlying stream. The read limit is set to 0 bytes.

Specified by:
close in interface java.io.Closeable
Overrides:
close in class java.io.InputStream
Throws:
java.io.IOException - If an I/O error occurs.

getRemainingBytes

public long getRemainingBytes()
Returns the number of remaining bytes for the read limit. If no read limit was specified, -1 is returned.

Returns:
The number of remaining bytes for the read limit (after the stream is closed, always 0 is returned).