FileDocCategorySizeDatePackage
BufferedServletOutputStream.javaAPI DocExample1062Sun Sep 02 14:59:04 BST 2001com.oreilly.javaxslt.util

BufferedServletOutputStream.java

package com.oreilly.javaxslt.util;

import java.io.*;
import javax.servlet.*;

/**
 * A custom servlet output stream that stores its data in a buffer,
 * rather than sending it directly to the client.
 *
 * @author Eric M. Burke
 */
public class BufferedServletOutputStream extends ServletOutputStream {
    // the actual buffer
    private ByteArrayOutputStream bos = new ByteArrayOutputStream();

    /**
     * @return the contents of the buffer.
     */
    public byte[] getBuffer() {
        return this.bos.toByteArray();
    }

    /**
     * This method must be defined for custom servlet output streams.
     */
    public void write(int data) {
        this.bos.write(data);
    }

    // BufferedHttpResponseWrapper calls this method
    public void reset() {
        this.bos.reset();
    }

    // BufferedHttpResponseWrapper calls this method
    public void setBufferSize(int size) {
        // no way to resize an existing ByteArrayOutputStream
        this.bos = new ByteArrayOutputStream(size);
    }
}