001    /*
002     * Copyright (C) The Apache Software Foundation. All rights reserved.
003     *
004     * This software is published under the terms of the Apache Software License
005     * version 1.1, a copy of which has been included with this distribution in
006     * the LICENSE file.
007     * 
008     * $Id: BufferedServletInputStream.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messagelet.impl;
011    
012    import java.io.ByteArrayInputStream;
013    import java.io.IOException;
014    import java.io.InputStream;
015    
016    import javax.servlet.ServletInputStream;
017    
018    /** 
019     * <p><code>BufferedServletInputStream</code> implements
020     * a ServletInputStream using an underlying InputStream.</p>
021     *
022      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
023      * @version $Revision: 155459 $
024     */
025    public class BufferedServletInputStream extends ServletInputStream {
026        
027        protected static final byte[] NO_DATA = new byte[0];
028        
029        /** The underlying <code>InputStream</code> */
030        private InputStream in;
031        
032        
033        public BufferedServletInputStream() {
034            this.in = new ByteArrayInputStream( NO_DATA );
035        }
036        
037        public BufferedServletInputStream(InputStream in) {
038            this.in = in;
039        }
040        
041        public BufferedServletInputStream(String text) {
042            in = new ByteArrayInputStream( text.getBytes() );
043        }
044        
045        public BufferedServletInputStream(byte[] data) {
046            in = new ByteArrayInputStream(data);
047        }
048        
049        // Delegating methods from InputStream
050        //-------------------------------------------------------------------------    
051        
052        public int available() throws IOException {
053            return in.available();
054        }    
055        
056        public void close() throws IOException {
057            in.close();
058        }
059        
060        public void mark(int readlimit) {
061            in.mark(readlimit);
062        }
063        
064        public boolean markSupported() {
065            return in.markSupported();
066        }
067        
068        public int read(byte[] b) throws IOException {
069            return in.read(b);
070        }
071        
072        public int read(byte[] b, int off, int len) throws IOException {
073            return in.read(b, off, len);
074        }
075        
076        public int read() throws IOException {
077            return in.read();
078        }
079            
080        public void reset() throws IOException {
081            in.reset();
082        }    
083        
084        public long skip(long n) throws IOException {
085            return in.skip(n);
086        }    
087    }