1 /* 2 * Copyright (C) The Apache Software Foundation. All rights reserved. 3 * 4 * This software is published under the terms of the Apache Software License 5 * version 1.1, a copy of which has been included with this distribution in 6 * the LICENSE file. 7 * 8 * $Id: BufferedServletOutputStream.java 155459 2005-02-26 13:24:44Z dirkv $ 9 */ 10 package org.apache.commons.messagelet.impl; 11 12 import java.io.ByteArrayOutputStream; 13 import java.io.IOException; 14 import java.io.OutputStream; 15 import java.io.UnsupportedEncodingException; 16 17 import javax.servlet.ServletOutputStream; 18 19 /** 20 * <p><code>BufferedServletOutputStream</code> implements 21 * a buffered ServletOutputStream in a similar way to 22 * ByteArrayOutputStream represents a buffered OutputStream.</p> 23 * 24 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 25 * @version $Revision: 155459 $ 26 */ 27 public class BufferedServletOutputStream extends ServletOutputStream { 28 29 /** The underlying <code>ByteArrayOutputStream</code> that the output 30 * is written to */ 31 protected ByteArrayOutputStream buffer; 32 33 34 public BufferedServletOutputStream() { 35 buffer = new ByteArrayOutputStream(); 36 } 37 38 public BufferedServletOutputStream(int size) { 39 buffer = new ByteArrayOutputStream(size); 40 } 41 42 // Delegating methods from OutputStream 43 //------------------------------------------------------------------------- 44 45 public void write(byte[] b) throws IOException { 46 buffer.write(b); 47 } 48 49 public void write(byte[] b, int off, int len) throws IOException { 50 buffer.write(b, off, len); 51 } 52 53 public void write(int b) throws IOException { 54 buffer.write(b); 55 } 56 57 public void close() throws IOException { 58 buffer.close(); 59 } 60 61 public void flush() throws IOException { 62 buffer.flush(); 63 } 64 65 66 // Methods promoted from ByteArrayOutputStream 67 //------------------------------------------------------------------------- 68 69 /** Resets the count field of this buffer to zero, 70 * so that all currently accumulated output in the 71 * ouput stream is discarded. 72 */ 73 public void reset() { 74 buffer.reset(); 75 } 76 77 /** Returns the current size of the buffer. 78 */ 79 public int size() { 80 return buffer.size(); 81 } 82 83 84 85 /** Creates a newly allocated byte array. 86 */ 87 public byte[] toByteArray() { 88 return buffer.toByteArray(); 89 } 90 91 /** Converts the buffer's contents into a string, 92 * translating bytes into characters according 93 * to the platform's default character encoding. 94 */ 95 public String toString() { 96 return buffer.toString(); 97 } 98 99 /** 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 }