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: BufferedServletOutputStream.java 155459 2005-02-26 13:24:44Z dirkv $
009     */
010    package org.apache.commons.messagelet.impl;
011    
012    import java.io.ByteArrayOutputStream;
013    import java.io.IOException;
014    import java.io.OutputStream;
015    import java.io.UnsupportedEncodingException;
016    
017    import javax.servlet.ServletOutputStream;
018    
019    /** 
020     * <p><code>BufferedServletOutputStream</code> implements
021     * a buffered ServletOutputStream in a similar way to
022     * ByteArrayOutputStream represents a buffered OutputStream.</p>
023     *
024      * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
025      * @version $Revision: 155459 $
026     */
027    public class BufferedServletOutputStream extends ServletOutputStream {
028        
029        /** The underlying <code>ByteArrayOutputStream</code> that the output
030         * is written to */
031        protected ByteArrayOutputStream buffer;
032        
033        
034        public BufferedServletOutputStream() {
035            buffer = new ByteArrayOutputStream();
036        }
037        
038        public BufferedServletOutputStream(int size) {
039            buffer = new ByteArrayOutputStream(size);
040        }
041        
042        // Delegating methods from OutputStream
043        //-------------------------------------------------------------------------    
044        
045        public void write(byte[] b) throws  IOException {
046            buffer.write(b);
047        }
048        
049        public void write(byte[] b, int off, int len) throws  IOException {
050            buffer.write(b, off, len);
051        }
052        
053        public void write(int b) throws  IOException {
054            buffer.write(b);
055        }
056            
057        public void close() throws  IOException {
058            buffer.close();
059        }
060        
061        public void flush() throws  IOException {
062            buffer.flush();
063        }    
064        
065        
066        // Methods promoted from ByteArrayOutputStream
067        //-------------------------------------------------------------------------    
068        
069        /** Resets the count field of this buffer to zero, 
070         * so that all currently accumulated output in the 
071         * ouput stream is discarded. 
072         */
073        public void reset() {
074            buffer.reset();
075        }
076     
077        /** Returns the current size of the buffer. 
078         */ 
079        public int size() {
080            return buffer.size();
081        }
082        
083    
084        
085        /** Creates a newly allocated byte array. 
086         */
087        public byte[] toByteArray() {
088            return buffer.toByteArray();
089        }
090        
091        /** Converts the buffer's contents into a string, 
092         * translating bytes into characters according 
093         * to the platform's default character encoding. 
094         */          
095        public String toString() {
096            return buffer.toString();
097        }
098           
099        /** Converts the buffer's contents into a string, 
100         * translating bytes into characters according 
101         * to the specified character encoding. 
102         */
103        public String toString(String enc) throws UnsupportedEncodingException {
104            return buffer.toString(enc);
105        }
106              
107        /** Writes the complete contents of this buffer
108         * to the specified output stream argument, 
109         * as if by calling the output stream's write 
110         * method using out.write(buf, 0, count). 
111         */
112        public void writeTo(OutputStream out) throws  IOException {
113            buffer.writeTo(out);
114        }
115        
116    }