Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   *
   * 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.
  */
 
 
 package org.jboss.remoting.transport.coyote;
 
 
 import java.io.Writer;
 import  org.apache.coyote.ActionCode;
 import  org.apache.coyote.Response;
 import  org.apache.tomcat.util.buf.ByteChunk;
 import  org.apache.tomcat.util.buf.C2BConverter;
 import  org.apache.tomcat.util.buf.CharChunk;
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 responsability).

Author(s):
Costin Manolache
Remy Maucherat
 
 public class OutputBuffer extends Writer
       implements ByteChunk.ByteOutputChannel, CharChunk.CharOutputChannel
 {
 
    // -------------------------------------------------------------- Constants
 
 
    public static final String DEFAULT_ENCODING =
          org.apache.coyote.Constants.DEFAULT_CHARACTER_ENCODING;
    public static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
 
 
    // The buffer can be used for byte[] and char[] writing
    // ( this is needed to support ServletOutputStream and for
    // efficient implementations of templating systems )
    public final int INITIAL_STATE = 0;
    public final int CHAR_STATE = 1;
    public final int BYTE_STATE = 2;
 
    // ----------------------------------------------------- Instance Variables
 

   
The byte buffer.
 
    private ByteChunk bb;


   
The chunk buffer.
 
    private CharChunk cb;


   
State of the output buffer.
 
    private int state = 0;


   
Number of bytes written.
 
    private int bytesWritten = 0;


   
Number of chars written.
 
    private int charsWritten = 0;


   
Flag which indicates if the output buffer is closed.
 
    private boolean closed = false;


   
Do a flush on the next operation.
   private boolean doFlush = false;


   
Byte chunk used to output bytes.
   private ByteChunk outputChunk = new ByteChunk();


   
Encoding to use.
   private String enc;


   
Encoder is set.
   private boolean gotEnc = false;


   
List of encoders.
   protected HashMap encoders = new HashMap();


   
Current char to byte converter.
   protected C2BConverter conv;


   
Associated Coyote response.
   private Response coyoteResponse;


   
Suspended flag. All output bytes will be swallowed if this is true.
   private boolean suspended = false;
   // ----------------------------------------------------------- Constructors


   
Default constructor. Allocate the buffer with the default buffer size.
   public OutputBuffer()
   {
      this();
   }


   
Alternate constructor which allows specifying the initial buffer size.

Parameters:
size Buffer size to use
   public OutputBuffer(int size)
   {
       = new ByteChunk(size);
      // Allow buffer to grow, to avoid using chunking
      // FIXME: Allowing chunking could be a better idea
      .setLimit(-1);
      .setByteOutputChannel(this);
       = new CharChunk(size);
      .setCharOutputChannel(this);
      .setLimit(size);
   }
   // ------------------------------------------------------------- Properties


   
Associated Coyote response.

Parameters:
coyoteResponse Associated Coyote response
   public void setResponse(Response coyoteResponse)
   {
      this. = coyoteResponse;
   }


   
Get associated Coyote response.

Returns:
the associated Coyote response
   public Response getResponse()
   {
      return this.;
   }


   
Is the response output suspended ?

Returns:
suspended flag value
   public boolean isSuspended()
   {
      return this.;
   }


   
Set the suspended flag.

Parameters:
suspended New suspended flag value
   public void setSuspended(boolean suspended)
   {
      this. = suspended;
   }
   // --------------------------------------------------------- Public Methods


   
Recycle the output buffer.
   public void recycle()
   {
       = ;
       = 0;
       = 0;
      .recycle();
      .recycle();
       = false;
       = false;
      if( != null)
      {
         .recycle();
      }
       = false;
       = null;
   }


   
Close the output buffer. This tries to calculate the response size if the response has not been committed yet.

Throws:
IOException An underlying IOException occurred
   public void close()
         throws IOException
   {
      if()
      {
         return;
      }
      if()
      {
         return;
      }
      if((!.isCommitted())
         && (.getContentLengthLong() == -1))
      {
         // Flushing the char buffer
         if( == )
         {
            .flushBuffer();
             = ;
         }
         // If this didn't cause a commit of the response, the final content
         // length can be calculated
         if(!.isCommitted())
         {
            .setContentLength(.getLength());
         }
      }
      doFlush(false);
       = true;
      .finish();
   }


   
Flush bytes or chars contained in the buffer.

Throws:
IOException An underlying IOException occurred
   public void flush()
         throws IOException
   {
      doFlush(true);
   }


   
Flush bytes or chars contained in the buffer.

Throws:
IOException An underlying IOException occurred
   protected void doFlush(boolean realFlush)
         throws IOException
   {
      if()
      {
         return;
      }
       = true;
      if( == )
      {
         .flushBuffer();
         .flushBuffer();
          = ;
      }
      else if( == )
      {
         .flushBuffer();
      }
      else if( == )
      {
         // If the buffers are empty, commit the response header
         .sendHeaders();
      }
       = false;
      if(realFlush)
      {
         .action(ActionCode.ACTION_CLIENT_FLUSH,
                               );
         // If some exception occurred earlier, or if some IOE occurred
         // here, notify the servlet with an IOE
         if(.isExceptionPresent())
         {
            throw new ClientAbortException
                  (.getErrorException());
         }
      }
   }
   // ------------------------------------------------- Bytes Handling Methods


   
Sends the buffer data to the client output, checking the state of Response and calling the right interceptors.

Parameters:
buf Byte buffer to be written to the response
off Offset
cnt Length
Throws:
IOException An underlying IOException occurred
   public void realWriteBytes(byte buf[], int offint cnt)
         throws IOException
   {
      if()
      {
         return;
      }
      if( == null)
      {
         return;
      }
      // If we really have something to write
      if(cnt > 0)
      {
         // real write to the adapter
         .setBytes(bufoffcnt);
         try
         {
            .doWrite();
         }
         catch(IOException e)
         {
            // An IOException on a write is almost always due to
            // the remote client aborting the request.  Wrap this
            // so that it can be handled better by the error dispatcher.
            throw new ClientAbortException(e);
         }
      }
   }
   public void write(byte b[], int offint lenthrows IOException
   {
      if()
      {
         return;
      }
      if( == )
      {
         .flushBuffer();
      }
       = ;
      writeBytes(bofflen);
   }
   private void writeBytes(byte b[], int offint len)
         throws IOException
   {
      if()
      {
         return;
      }
      .append(bofflen);
       += len;
      // if called from within flush(), then immediately flush
      // remaining bytes
      if()
      {
         .flushBuffer();
      }
   }
   public void writeByte(int b)
         throws IOException
   {
      if()
      {
         return;
      }
      if( == )
      {
         .flushBuffer();
      }
       = ;
      .append((byteb);
      ++;
   }
   // ------------------------------------------------- Chars Handling Methods
   public void write(int c)
         throws IOException
   {
      if()
      {
         return;
      }
       = ;
      .append((charc);
      ++;
   }
   public void write(char c[])
         throws IOException
   {
      if()
      {
         return;
      }
      write(c, 0, c.length);
   }
   public void write(char c[], int offint len)
         throws IOException
   {
      if()
      {
         return;
      }
       = ;
      .append(cofflen);
       += len;
   }
   public void write(StringBuffer sb)
         throws IOException
   {
      if()
      {
         return;
      }
       = ;
      int len = sb.length();
       += len;
      .append(sb);
   }


   
Append a string to the buffer
   public void write(String sint offint len)
         throws IOException
   {
      if()
      {
         return;
      }
       = ;
       += len;
      if(s == null)
      {
         s = "null";
      }
      .append(sofflen);
   }
   public void write(String s)
         throws IOException
   {
      if()
      {
         return;
      }
       = ;
      if(s == null)
      {
         s = "null";
      }
      write(s, 0, s.length());
   }
   public void flushChars()
         throws IOException
   {
      .flushBuffer();
       = ;
   }
   public boolean flushCharsNeeded()
   {
      return  == ;
   }
   public void setEncoding(String s)
   {
       = s;
   }
   public void realWriteChars(char c[], int offint len)
         throws IOException
   {
      if(!)
      {
         setConverter();
      }
      // The following has been updated to conform to jbossweb 2.1.
      .setChars(cofflen);
      while (.getLength() > 0) { 
          .convert();
          if (.getLength() > 0) {
              .flushBuffer();
          }
      }
   }
   public void checkConverter()
         throws IOException
   {
      if(!)
      {
         setConverter();
      }
   }
   protected void setConverter()
         throws IOException
   {
      if( != null)
      {
          = .getCharacterEncoding();
      }
       = true;
      if( == null)
      {
          = ;
      }
       = (C2BConverter) .get();
      
      // The following has been updated to conform to jbossweb 2.1.
      if( == null)
      {
         if(!SecurityUtility.skipAccessControl())
         {
            try
            {
                = (C2BConverter) AccessController.doPrivileged(
                     new PrivilegedExceptionAction()
                     {
                        public Object run() throws IOException
                        {
                           return new C2BConverter();
                        }
                     }
               );
            }
            catch(PrivilegedActionException ex)
            {
               Exception e = ex.getException();
               if(e instanceof IOException)
               {
                  throw (IOExceptione;
               }
            }
         }
         else
         {
             = new C2BConverter();
         }
         .put();
      }
   }
   // --------------------  BufferedOutputStream compatibility


   
Real write - this buffer will be sent to the client
   public void flushBytes()
         throws IOException
   {
      .flushBuffer();
   }
   public int getBytesWritten()
   {
      return ;
   }
   public int getCharsWritten()
   {
      return ;
   }
   public int getContentWritten()
   {
      return  + ;
   }


   
True if this buffer hasn't been used ( since recycle() ) - i.e. no chars or bytes have been added to the buffer.
   public boolean isNew()
   {
      return ( == 0) && ( == 0);
   }
   public void setBufferSize(int size)
   {
      if(size > .getLimit())
      {// ??????
         .setLimit(size);
      }
   }
   public void reset()
   {
      //count=0;
      .recycle();
       = 0;
      .recycle();
       = 0;
       = false;
       = null;
       = ;
   }
   public int getBufferSize()
   {
      return .getLimit();
   }
New to GrepCode? Check out our FAQ X