org.apache.cocoon.util
Class PostInputStream

java.lang.Object
  extended byjava.io.InputStream
      extended byorg.apache.cocoon.util.PostInputStream

public class PostInputStream
extends InputStream

The class PostInputStream is a wrapper for InputStream associated with POST message. It allows to control read operation, restricting the number of bytes read to the value returned by getContentLen() method.

Version:
$Id: PostInputStream.html 1304280 2012-03-23 11:18:01Z ilgrosso $

Field Summary
static String CLASS
          Class name
protected  int m_bytesRead
          The number of bytes read
 
Constructor Summary
PostInputStream()
          Creates a PostInputStream
PostInputStream(InputStream input, int len)
          Creates a PostInputStream based on a real InputStream object with the specified post message body length.
 
Method Summary
 int available()
          Returns the number of bytes available from this input stream that can be read without the stream blocking.
protected  void checkOpen()
          Checks to see if this stream is closed; if it is, an IOException is thrown.
 void close()
          Closes this input stream by closing the underlying stream and marking this one as closed.
 int getContentLen()
          Returns the post message body length.
 InputStream getInputStream()
          Returns the underlying input stream.
protected  void init(InputStream input, int len)
          Sets the underlying input stream and contentLen value .
 boolean markSupported()
          Tests if this input stream supports the mark and reset methods.
 int read()
          Reads the next byte from the input stream.
 int read(byte[] buffer)
           
 int read(byte[] buffer, int offset, int len)
          Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.
 void setInputStream(InputStream input, int len)
          Sets the underlying input stream and contentLen value .
 long skip(long n)
          See the general contract of the skip method of InputStream.
 String toString()
          Returns a String representation of this.
 
Methods inherited from class java.io.InputStream
mark, reset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CLASS

public static final String CLASS
Class name


m_bytesRead

protected int m_bytesRead
The number of bytes read

Constructor Detail

PostInputStream

public PostInputStream()
Creates a PostInputStream


PostInputStream

public PostInputStream(InputStream input,
                       int len)
                throws IllegalArgumentException
Creates a PostInputStream based on a real InputStream object with the specified post message body length. Saves its argument, the input stream m_inputStream, for later use.

Parameters:
input - the underlying input stream.
len - the post message body length.
Throws:
IllegalArgumentException - len <= 0.
Method Detail

init

protected void init(InputStream input,
                    int len)
             throws IllegalArgumentException
Sets the underlying input stream and contentLen value .

Parameters:
input - the input stream; can not be null.
len - the post message body length.
Throws:
IllegalArgumentException

setInputStream

public void setInputStream(InputStream input,
                           int len)
                    throws IOException
Sets the underlying input stream and contentLen value .

Parameters:
input - the input stream; can not be null.
len - the post message body length.
Throws:
IOException

getInputStream

public InputStream getInputStream()
Returns the underlying input stream.

Returns:
inputStream the underlying InputStream.

getContentLen

public int getContentLen()
Returns the post message body length.

Returns:
m_contentLen;

read

public int read()
         throws IOException
Reads the next byte from the input stream. If the end of the stream has been reached, this method returns -1.

Returns:
the next byte or -1 if at the end of the stream.
Throws:
IOException

read

public int read(byte[] buffer,
                int offset,
                int len)
         throws IOException
Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.

This method implements the general contract of the corresponding read method of the InputStream class. This method delegetes the read operation to the underlying InputStream implementation class but it controlls the number of bytes read from the stream. In the remote situation the underlying InputStream has no knowledge of the length of the stream and the notion of the "end" is undefined. This wrapper class has a knowledge of the length of data send by the requestor by the means of contentLength. This method returns the number of bytes read and accumulates the total number of bytes read in m_bytesRead. When the m_bytesRead is equal to the specified contentLength value the method returns returns -1 to signal the end of data.

Parameters:
buffer - the byte array to read into; can not be null.
offset - the starting offset in the byte array.
len - the maximum number of bytes to read.
Returns:
the number of bytes read, or -1 if the end of the stream has been reached.
Throws:
IOException - if an I/O error occurs.

read

public int read(byte[] buffer)
         throws IOException
Throws:
IOException

checkOpen

protected void checkOpen()
                  throws IOException
Checks to see if this stream is closed; if it is, an IOException is thrown.

Throws:
IOException

skip

public long skip(long n)
          throws IOException
See the general contract of the skip method of InputStream. Delegates execution to the underlying InputStream implementation class. Checks to see if this stream is closed; if it is, an IOException is thrown.

Parameters:
n - the number of bytes to be skipped.
Returns:
the actual number of bytes skipped.
Throws:
IOException - if an I/O error occurs.

available

public int available()
              throws IOException
Returns the number of bytes available from this input stream that can be read without the stream blocking. Delegates execution to the underlying InputStream implementation class.

Returns:
available the number of available bytes.
Throws:
IOException

markSupported

public boolean markSupported()
Tests if this input stream supports the mark and reset methods. The markSupported method of BufferedInputStream returns false.

Returns:
a boolean indicating if this stream type supports the mark and reset methods.
See Also:
InputStream.mark(int), InputStream.reset()

close

public void close()
           throws IOException
Closes this input stream by closing the underlying stream and marking this one as closed.

Throws:
IOException

toString

public String toString()
Returns a String representation of this.

Returns:
string the String representation of this.


Copyright 1999-2008 The Apache Software Foundation. All Rights Reserved.