/*
 * The Apache Software License, Version 1.1
 *
 *
 * Copyright (c) 2001 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. The names "Axis" and "Apache Software Foundation" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache",
 *    nor may "Apache" appear in their name, without prior written
 *    permission of the Apache Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */


package org.apache.axis.transport.http;

import java.io.InputStream;
import java.io.IOException;

/**
 * HTTPInputStream is a wrapper class of any InputStream with HTTP content. 
 * The content must either has content length or uses chunked transfer encoding.
 * The major purpose of this class is to be used in HTTP Keep-Alive connection.
 * Between each requests in a Keep-Alive connection, <code>reset( int contentlength )
 * </code> method needs to be invoked before reuse it. If the content length is 
 * set to -1, it assume chunked transfer encoding is used. When ever the length
 * of read bytes reach the content length, or the final chunk is found, read 
 * methods will return -1 before <code>reset( int contentlength ) is invoked.
 *
 * @author <a href="mailto:shih-chang_chen@hp.com">Shih-Chang Chen</a>
 */
public class HTTPInputStream extends InputStream {

    /**
     * True if the final chunk was read, or the content lenght is reached.
     */
    private boolean m_closed;

    /**
     * The buffer.
     */
    private byte[] m_buffer;
    
    /**
     * The number of bytes in the buffer which have already been returned by 
     * this stream.
     */
    private int m_bufferOffset;
    
    /**
     * Length of current chunk. It is also the valid length of the m_buffer.
     */
    private int m_chunkSize;

    /**
     * The underlying input stream with HTTP content.
     */
    private InputStream m_stream;
    
    /**
     * Content length. It can be -1 if not availble.
     */
    private int m_contentLength;

    /**
     * The number of bytes in the current response content which has already 
     * been read.
     */
    private int m_contentOffset;
    
    /**
     * Creates a <code>HTTPInputStream</code> form a <code>InputStream</code>.
     * It assumes the first HTTP content in the underlying stream is chunking
     * encoded. Otherwise, the other constructer <code>
     * HTTPInputStream( InputStream stream, int contentLength )</code> should be
     * used, and the content length is required.
     *
     * @param stream can not be null.
     */
    public HTTPInputStream( InputStream stream ) {
        if (null == stream) {
            throw new NullPointerException();
        }
        m_closed = false;
        m_bufferOffset = 0;
        m_chunkSize  = 0;
        m_buffer = null;
        m_contentLength = -1;
        m_contentOffset = -1;
        m_stream = stream;
    }
    
    /**
     * Creates a <code>HTTPInputStream</code> form a <code>InputStream</code>. 
     * The content length is not allowed to be negative for this constructor.
     * It assumes the first HTTP content in the underlying stream does not use
     * chunking encoding. Otherwise, the other constructer <code>
     * HTTPInputStream( InputStream stream )</code> should be used.
     *
     * @param stream can not be null.
     * @param contentLength content length. This value cannot be negative.
     */
    public HTTPInputStream( InputStream stream, int contentLength ) {
        if (null == stream) {
            throw new NullPointerException();
        }
        m_closed = false;
        m_bufferOffset = 0;
        m_chunkSize  = 0;
        m_buffer = null;
        if( contentLength < 0 ) 
        	throw new java.lang.IllegalArgumentException( 
        		"Content-Length cannot be negative: " + contentLength );
        m_contentLength = contentLength;
        m_contentOffset = 0;
        m_stream = stream;
    }
    
    /**
     * Reset this <code>HTTPInputStream</code>. This method needs to be invoked 
     * before read the following content in the input. Any attempt to read from 
     * this stream will return -1 if this mothed is not invoke after finish 
     * reading a content.
     *
     * @param contentLength content length. If the value is -1, it will assume
     *        chunked transfer-encoding is used for the following content.
     *
     * @exception IOException if an input/output error occurs
     */
    public void reset( int contentLength ) throws IOException {
    	close();
        m_closed = false;
        m_bufferOffset = 0;
        m_chunkSize  = 0;
        m_contentLength = contentLength;
        m_contentOffset = 0;
    }

    /**
     * Close this input stream. Any further attempt to read from this stream 
     * will return -1. All remaining bytes will be skipped.
     *
     * @exception IOException if an input/output error occurs
     */
    public void close() throws IOException {
        if ( !m_closed ) {
    		IOException e = null;
    		int i = 0;
            try {
                while ( !m_closed && i >= 0 ) i = read();
            } catch ( IOException ex ) {
                e = ex;
            } finally {
                m_closed = true;
                if( e != null ) throw e;
            }
        }
    }


    /**
     * Read up to <code>len</code> bytes of data from the input stream
     * into an array of bytes.  An attempt is made to read as many as
     * <code>len</code> bytes, but a smaller number may be read,
     * possibly zero.  The number of bytes actually read is returned as
     * an integer.  This method blocks until input data is available,
     * end of file is detected, or an exception is thrown.
     *
     * @param      b     the buffer into which the data is read.
     * @param      off   the start offset in array <code>b</code>
     *                   at which the data is written.
     * @param      len   the maximum number of bytes to read.
     *
     * @return     the total number of bytes read into the buffer, or
     *             <code>-1</code> if there is no more data because the end of
     *             the current content has been reached.
     *
     * @exception IOException if an input/output error occurs
     */
    public int read ( byte b[], int off, int len ) throws IOException {
        int available = m_chunkSize - m_bufferOffset;
        if( ( available <= 0 ) && ( !fill() ) ) return ( -1 );
		available = m_chunkSize - m_bufferOffset;
        if( available <= 0 ) return ( -1 );
        int read = Math.min( available, len );
        System.arraycopy( m_buffer, m_bufferOffset, b, off, read );
        m_bufferOffset += read;
        return read;
    }

    /**
     * Read and return a single byte from this input stream, or -1 if end of
     * current content has been encountered.
     *
     * @return     the next byte of data, or <code>-1</code> if the end of the
     *             current content is reached.
     *
     * @exception IOException if an input/output error occurs
     */
    public int read() throws IOException {
        if( m_bufferOffset == m_chunkSize && !fill() ) return (-1);
        return( m_buffer[m_bufferOffset++] & 0xff );
    }

    /**
     * Fills the buffer. If the content is chunking encoded, the size of buffer
     * is set to be the same as the chunk size. Return false if the content 
     * length is reached or the final chunk is read.
     *
     * @return  boolean false if the content length is reached or the final 
     *          chunk is read.
     *
     * @exception IOException if an input/output error occurs
     */
    protected boolean fill() throws IOException {
        if ( m_closed ) return false;
        m_chunkSize = 0;
        if( m_contentLength >= 0) {
        	if( m_contentLength <= m_contentOffset ||
        	    m_contentLength == 0 ) {
        		m_closed = true;
        		return false;
        	} else {
        		m_chunkSize = Math.min( 8192, (m_contentLength - m_contentOffset) );
        		m_contentOffset += m_chunkSize;
        	}
        } else {
			try {
				String chunkSizeStr = readLine();
				if( chunkSizeStr == null ) throw new NumberFormatException();
				m_chunkSize = Integer.parseInt( chunkSizeStr.trim(), 16 );
			} catch( NumberFormatException e ) {
				m_chunkSize = 0;
				m_closed = true;
				return false;
			}
		}
        if ( m_chunkSize == 0 ) {
            String trailing = readLine();
            while ( trailing != null && !trailing.equals( "" ) ) 
            	trailing = readLine();
            m_closed = true;
            return false;
        } else {
			if ( (m_buffer == null) || (m_chunkSize > m_buffer.length) )
            	m_buffer = new byte[m_chunkSize];
            int offset = 0;
            int read   = 0;
            m_bufferOffset = 0;
            while( offset < m_chunkSize && read >= 0 ) {
                read = m_stream.
                	read( m_buffer, offset, m_chunkSize - offset );
                offset += read;
            }
            //chunked transfer-encoding
            if( m_contentLength == -1 ) readLine();
        }
        return true;

    }

    /**
     * Reads a line (up to CRLF) from input stream.
     *
     * @return  String a line from input stream
     *
     * @exception IOException if an input/output error occurs
     */
    protected String readLine() throws IOException {
        StringBuffer strbuffer = null;
        int i = m_stream.read();
        while( i >= 0 && i != '\n' ) {   
            if( i != '\r' ) {
            	//in most cases, the stringbuffer is not needed
            	if( strbuffer == null )
            		strbuffer = new StringBuffer();
            	strbuffer.append( (char) i );
            }
            i = m_stream.read();
        }
        return ( strbuffer != null && strbuffer.length() > 0 ) ? 
        	( strbuffer.toString() ) : null;
    }
}
