Class OutputBuffer

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable

    public class OutputBuffer
    extends java.io.Writer
    The buffer used by Tomcat response. This is a derivative of the Tomcat 3.3 OutputBuffer, with the removal of some of the state handling (which in Coyote is mostly the Processor's responsibility).
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected C2BConverter conv
      Current char to byte converter.
      static int DEFAULT_BUFFER_SIZE
      Default buffer size in bytes.
      • Fields inherited from class java.io.Writer

        lock
    • Constructor Summary

      Constructors 
      Constructor Description
      OutputBuffer​(int size)
      Create the buffer with the specified initial size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void append​(byte[] src, int off, int len)
      Add data to the buffer.
      void append​(char[] src, int off, int len)
      Add data to the buffer.
      void append​(java.nio.ByteBuffer from)
      Appends bytes from the given buffer to the output.
      void checkConverter()
      Checks and initializes the character-to-byte converter if needed.
      void checkRegisterForWrite()
      Checks and registers for write if needed.
      void close()
      Close the output buffer.
      protected void doFlush​(boolean realFlush)
      Flush bytes or chars contained in the buffer.
      void flush()
      Flush bytes or chars contained in the buffer.
      int getBufferSize()
      Returns the buffer size.
      long getContentWritten()
      Returns the total number of bytes and characters written.
      boolean isBlocking()
      Checks if this output buffer is in blocking mode.
      boolean isClosed()
      Is the response output closed ?
      boolean isNew()
      Has this buffer been used at all?
      boolean isReady()
      Checks if the output is ready for writing.
      boolean isSuspended()
      Is the response output suspended ?
      void realWriteBytes​(java.nio.ByteBuffer buf)
      Sends the buffer data to the client output, checking the state of Response and calling the right interceptors.
      void realWriteChars​(java.nio.CharBuffer from)
      Convert the chars to bytes, then send the data to the client.
      void recycle()
      Recycle the output buffer.
      void reset()
      Resets the output buffer without resetting writer/stream flags.
      void reset​(boolean resetWriterStreamFlags)
      Resets the output buffer.
      void setBufferSize​(int size)
      Sets the buffer size, allocating a new buffer if needed.
      void setErrorException​(java.lang.Exception e)
      Sets the error exception for this output buffer.
      void setResponse​(Response coyoteResponse)
      Associated Coyote response.
      void setSuspended​(boolean suspended)
      Set the suspended flag.
      void setWriteListener​(WriteListener listener)
      Sets the write listener for this output buffer.
      void write​(byte[] b, int off, int len)
      Writes the specified bytes to the output buffer.
      void write​(char[] c)  
      void write​(char[] c, int off, int len)  
      void write​(int c)  
      void write​(java.lang.String s)  
      void write​(java.lang.String s, int off, int len)
      Append a string to the buffer
      void write​(java.nio.ByteBuffer from)
      Writes bytes from the given buffer to the output.
      void writeByte​(int b)
      Writes a single byte to the output buffer.
      • Methods inherited from class java.io.Writer

        append, append, append, nullWriter
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_BUFFER_SIZE

        public static final int DEFAULT_BUFFER_SIZE
        Default buffer size in bytes.
        See Also:
        Constant Field Values
      • conv

        protected C2BConverter conv
        Current char to byte converter.
    • Constructor Detail

      • OutputBuffer

        public OutputBuffer​(int size)
        Create the buffer with the specified initial size.
        Parameters:
        size - Buffer size to use
    • Method Detail

      • setResponse

        public void setResponse​(Response coyoteResponse)
        Associated Coyote response.
        Parameters:
        coyoteResponse - Associated Coyote response
      • isSuspended

        public boolean isSuspended()
        Is the response output suspended ?
        Returns:
        suspended flag value
      • setSuspended

        public void setSuspended​(boolean suspended)
        Set the suspended flag.
        Parameters:
        suspended - New suspended flag value
      • isClosed

        public boolean isClosed()
        Is the response output closed ?
        Returns:
        closed flag value
      • recycle

        public void recycle()
        Recycle the output buffer.
      • close

        public void close()
                   throws java.io.IOException
        Close the output buffer. This tries to calculate the response size if the response has not been committed yet.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class java.io.Writer
        Throws:
        java.io.IOException - An underlying IOException occurred
      • flush

        public void flush()
                   throws java.io.IOException
        Flush bytes or chars contained in the buffer.
        Specified by:
        flush in interface java.io.Flushable
        Specified by:
        flush in class java.io.Writer
        Throws:
        java.io.IOException - An underlying IOException occurred
      • doFlush

        protected void doFlush​(boolean realFlush)
                        throws java.io.IOException
        Flush bytes or chars contained in the buffer.
        Parameters:
        realFlush - true if this should also cause a real network flush
        Throws:
        java.io.IOException - An underlying IOException occurred
      • realWriteBytes

        public void realWriteBytes​(java.nio.ByteBuffer buf)
                            throws java.io.IOException
        Sends the buffer data to the client output, checking the state of Response and calling the right interceptors.
        Parameters:
        buf - the ByteBuffer to be written to the response
        Throws:
        java.io.IOException - An underlying IOException occurred
      • write

        public void write​(byte[] b,
                          int off,
                          int len)
                   throws java.io.IOException
        Writes the specified bytes to the output buffer.
        Parameters:
        b - The byte array
        off - The offset in the array
        len - The number of bytes to write
        Throws:
        java.io.IOException - if an I/O error occurs
      • write

        public void write​(java.nio.ByteBuffer from)
                   throws java.io.IOException
        Writes bytes from the given buffer to the output.
        Parameters:
        from - The byte buffer to read from
        Throws:
        java.io.IOException - if an I/O error occurs
      • writeByte

        public void writeByte​(int b)
                       throws java.io.IOException
        Writes a single byte to the output buffer.
        Parameters:
        b - The byte to write
        Throws:
        java.io.IOException - if an I/O error occurs
      • realWriteChars

        public void realWriteChars​(java.nio.CharBuffer from)
                            throws java.io.IOException
        Convert the chars to bytes, then send the data to the client.
        Parameters:
        from - Char buffer to be written to the response
        Throws:
        java.io.IOException - An underlying IOException occurred
      • write

        public void write​(int c)
                   throws java.io.IOException
        Overrides:
        write in class java.io.Writer
        Throws:
        java.io.IOException
      • write

        public void write​(char[] c)
                   throws java.io.IOException
        Overrides:
        write in class java.io.Writer
        Throws:
        java.io.IOException
      • write

        public void write​(char[] c,
                          int off,
                          int len)
                   throws java.io.IOException
        Specified by:
        write in class java.io.Writer
        Throws:
        java.io.IOException
      • write

        public void write​(java.lang.String s,
                          int off,
                          int len)
                   throws java.io.IOException
        Append a string to the buffer
        Overrides:
        write in class java.io.Writer
        Throws:
        java.io.IOException
      • write

        public void write​(java.lang.String s)
                   throws java.io.IOException
        Overrides:
        write in class java.io.Writer
        Throws:
        java.io.IOException
      • checkConverter

        public void checkConverter()
                            throws java.io.IOException
        Checks and initializes the character-to-byte converter if needed.
        Throws:
        java.io.IOException - if an I/O error occurs
      • getContentWritten

        public long getContentWritten()
        Returns the total number of bytes and characters written.
        Returns:
        The total content written
      • isNew

        public boolean isNew()
        Has this buffer been used at all?
        Returns:
        true if no chars or bytes have been added to the buffer since the last call to recycle()
      • setBufferSize

        public void setBufferSize​(int size)
        Sets the buffer size, allocating a new buffer if needed.
        Parameters:
        size - The new buffer size
      • reset

        public void reset()
        Resets the output buffer without resetting writer/stream flags.
      • reset

        public void reset​(boolean resetWriterStreamFlags)
        Resets the output buffer.
        Parameters:
        resetWriterStreamFlags - Whether to reset writer and stream flags
      • getBufferSize

        public int getBufferSize()
        Returns the buffer size.
        Returns:
        The buffer size in bytes
      • isReady

        public boolean isReady()
        Checks if the output is ready for writing.
        Returns:
        true if ready for writing
      • setWriteListener

        public void setWriteListener​(WriteListener listener)
        Sets the write listener for this output buffer.
        Parameters:
        listener - The write listener
      • isBlocking

        public boolean isBlocking()
        Checks if this output buffer is in blocking mode.
        Returns:
        true if in blocking mode
      • checkRegisterForWrite

        public void checkRegisterForWrite()
        Checks and registers for write if needed.
      • append

        public void append​(byte[] src,
                           int off,
                           int len)
                    throws java.io.IOException
        Add data to the buffer.
        Parameters:
        src - Bytes array
        off - Offset
        len - Length
        Throws:
        java.io.IOException - Writing overflow data to the output channel failed
      • append

        public void append​(char[] src,
                           int off,
                           int len)
                    throws java.io.IOException
        Add data to the buffer.
        Parameters:
        src - Char array
        off - Offset
        len - Length
        Throws:
        java.io.IOException - Writing overflow data to the output channel failed
      • append

        public void append​(java.nio.ByteBuffer from)
                    throws java.io.IOException
        Appends bytes from the given buffer to the output.
        Parameters:
        from - The byte buffer to append
        Throws:
        java.io.IOException - if an I/O error occurs
      • setErrorException

        public void setErrorException​(java.lang.Exception e)
        Sets the error exception for this output buffer.
        Parameters:
        e - The exception