Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you 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
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
The BufferedServletOutputStream is a wrapper for OutputStream objects, which do not have their own buffering support. The main use of this class is to wrap the output stream retrieved from the cache handler, which does not have buffering but buffering needs to be supported.

This class is not multithread safe as it is intended to be used on single requests which are assigned to single threads.

 public class BufferedServletOutputStream extends ServletOutputStream implements Buffer {

default logger
     private static final Logger log = LoggerFactory.getLogger(BufferedServletOutputStream.class);

The wrapped ServletOutputStream
     protected OutputStream delegatee;

The size of the buffer in bytes
     private int bufferSize;

The byte buffer itself
     private byte[] buffer;

The current offset of writing bytes into the buffer
     private int offset;

flag to indicate that the stream has been closed
     protected boolean closed;

Creates an instance wrapping the OutputStream and providing an initial buffer bufferSize.

delegatee The OutputStream to wrap with buffering
bufferSize The initial buffer bufferSize in bytes
     public BufferedServletOutputStream(OutputStream delegateeint bufferSize) {
         this. = delegatee;
         this. = 0;
     //---------- buffer handling -----------------------------------------------
Sets the buffer size. If the buffer contains data, this method throws an IllegalStateException.

java.lang.IllegalStateException if the buffer contains data.
     public void setBufferSize(int bufferSize) {
         // check buffer requirements
         if (this. != 0) {
             throw new IllegalStateException("Buffer not empty");
         // only resize buffer if positive value
         if (bufferSize > 0) {
             .debug("setBufferSize: Creating Buffer of {0} characters",
             this. = bufferSize;
             this. = new byte[bufferSize];
         } else {
             .debug("setBufferSize: Disabling Buffering");
             this. = -1;
             this. = null;

Returns the size of the buffer

the size of the buffer
    public int getBufferSize() {
        return this.;

CLears the buffer.
    public void resetBuffer() {
        this. = 0;

Flushes the output buffer to the underlying ServletOutputStream.

Throws: If the stream is already closed or if an I/O error occurrs flushing the buffer.
    public void flushBuffer() throws IOException {
        // write the buffer
        if (this. != null) {
            if (this. > 0) {
                .debug("flush: Flushing {0} bytes", String.valueOf(this.));
                this..write(this., 0, this.);
            } else {
                .debug("flush: Empty buffer");
        } else {
            .debug("write: No buffer to flush due to disabled buffering");
        this. = 0;
    //---------- ServletOutputStream overwrites --------------------------------

Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

The flush method of OutputStream does nothing.

Throws: if an I/O error occurs.
    public void flush() throws IOException {

Closes this output stream and releases any system resources associated with this stream. The general contract of close is that it closes the output stream. A closed stream cannot perform output operations and cannot be reopened.

The close method of OutputStream does nothing.

Throws: if an I/O error occurs.
    public void close() throws IOException {
        if (!this.) {
            this. = true;

Writes the specified byte to this output stream. The general contract for write is that one byte is written to the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits of b are ignored.

Subclasses of OutputStream must provide an implementation for this method.

b the byte.
Throws: if an I/O error occurs. In particular, an IOException may be thrown if the output stream has been closed.
    public void write(int bthrows IOException {
        // assert stream is not closed
        if (this. == null) {
            .debug("write: Direct writing due to disabled buffering");
        } else {
            if (this. >= this.) {
                .debug("write: Buffer full, flushing first");
            this.[this.++] = (byteb;

Writes len bytes from the specified byte array starting at offset off to this output stream. The general contract for write(b, off, len) is that some of the bytes in the array b are written to the output stream in order; element b[off] is the first byte written and b[off+len-1] is the last byte written by this operation.

The write method of OutputStream calls the write method of one argument on each of the bytes to be written out. Subclasses are encouraged to override this method and provide a more efficient implementation.

If b is null, a NullPointerException is thrown.

If off is negative, or len is negative, or off+len is greater than the length of the array b, then an IndexOutOfBoundsException is thrown.

b the data.
off the start offset in the data.
len the number of bytes to write.
Throws: if an I/O error occurs. In particular, an IOException is thrown if the output stream is closed.
    public void write(byte b[], int offint lenthrows IOException {
        // assert stream is not closed
        if (this. == null) {
            .debug("write: Direct writing due to disabled buffering");
        } else {
            // copy all buffer parts bigger than the current space
            while (this. + len - 1 >= this.) {
                // write the first portion to the buffer to flush
                int space = this. - this.;
                off += space;
                len -= space;
                .debug("write: {0} bytes written, flush buffer",
                this. = this.;
            // copy rest of the data
            if (len > 0) {
                .debug("write: Writing {0} bytes to the buffer",
                this. += len;
    //---------- internal ------------------------------------------------------

Throws an IOException if the stream is closed.

Throws: if the stream is already closed.
    private void assertOpen() throws IOException {
        if (this.) {
            throw new IOException("Stream already closed");
New to GrepCode? Check out our FAQ X