/** * This work was created by participants in the DataONE project, and is * jointly copyrighted by participating institutions in DataONE. For * more information on DataONE, see our web site at http://dataone.org. * * Copyright ${year} * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * $Id$ */ package org.dataone.cn.servlet.http; 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( ); // private WriteListener wListener; /** * @return the contents of the buffer. */ public byte[] getBuffer( ) { return this.bos.toByteArray( ); } /** * This method must be defined for custom servlet output streams. */ @Override public void write(int data) throws IOException { this.bos.write(data); // wListener.notifyAll(); } /** * This method must be defined for custom servlet output streams. */ @Override public void write(byte[] b) throws IOException { this.bos.write(b); // wListener.notifyAll(); } /** * This method must be defined for custom servlet output streams. */ @Override public void write(byte[] b, int off, int len) throws IOException { this.bos.write(b,off,len); // wListener.notifyAll(); } // 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); } // @Override // public boolean isReady() { // // can data be written without blocking? // return true; // } // @Override // public void setWriteListener(WriteListener arg0) { // this.wListener = arg0; // // } }