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
   *
   *      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.apache.coyote.http11;
 
 
 
Output buffer.

Author(s):
Remy Maucherat
 
 public class InternalOutputBuffer 
     implements OutputBufferByteChunk.ByteOutputChannel {
 
     // -------------------------------------------------------------- Constants
 
 
     // ----------------------------------------------------------- Constructors
 

    
Alternate constructor.
 
     public InternalOutputBuffer(Response responseint headerBufferSize) {
 
         this. = response;
         
          = response.getMimeHeaders();
 
          = new byte[headerBufferSize];
 
 
          = new OutputFilter[0];
          = new OutputFilter[0];
          = -1;
 
          = new ByteChunk();
         .setByteOutputChannel(this);
 
          = false;
          = false;
 
         // Cause loading of constants
         boolean res = ....;
 
     }
 
 
     // ----------------------------------------------------- Instance Variables
 

    
Associated Coyote response.
 
     protected Response response;


    
Headers of the associated request.
 
     protected MimeHeaders headers;


    
Committed flag.
 
     protected boolean committed;


    
Finished flag.
    protected boolean finished;


    
The buffer used for header composition.
    protected byte[] buf;


    
Position in the buffer.
    protected int pos;


    
Underlying output stream.
    protected OutputStream outputStream;


    
Underlying output buffer.
    protected OutputBuffer outputStreamOutputBuffer;


    
Filter library. Note: Filter[0] is always the "chunked" filter.
    protected OutputFilter[] filterLibrary;


    
Active filter (which is actually the top of the pipeline).
    protected OutputFilter[] activeFilters;


    
Index of the last active filter.
    protected int lastActiveFilter;


    
Socket buffer.
    protected ByteChunk socketBuffer;


    
Socket buffer (extra buffering to reduce number of packets sent).
    protected boolean useSocketBuffer = false;
    // ------------------------------------------------------------- Properties


    
Set the underlying socket output stream.
    public void setOutputStream(OutputStream outputStream) {
        // FIXME: Check for null ?
        this. = outputStream;
    }


    
Get the underlying socket output stream.
    public OutputStream getOutputStream() {
        return ;
    }


    
Set the socket buffer size.
    public void setSocketBuffer(int socketBufferSize) {
        if (socketBufferSize > 500) {
             = true;
            .allocate(socketBufferSizesocketBufferSize);
        } else {
             = false;
        }
    }


    
Add an output filter to the filter library.
    public void addFilter(OutputFilter filter) {
        OutputFilter[] newFilterLibrary = 
            new OutputFilter[. + 1];
        for (int i = 0; i < .i++) {
            newFilterLibrary[i] = [i];
        }
        newFilterLibrary[.] = filter;
         = newFilterLibrary;
    }


    
Get filters.
    public OutputFilter[] getFilters() {
        return ;
    }


    
Clear filters.
    public void clearFilters() {
         = new OutputFilter[0];
         = -1;
    }


    
Add an output filter to the filter library.
    public void addActiveFilter(OutputFilter filter) {
        if ( == -1) {
            filter.setBuffer();
        } else {
            for (int i = 0; i <= i++) {
                if ([i] == filter)
                    return;
            }
            filter.setBuffer([]);
        }
        [++] = filter;
        filter.setResponse();
    }
    // --------------------------------------------------------- Public Methods


    
Flush the response.

Throws:
java.io.IOException an undelying I/O error occured
    public void flush()
        throws IOException {
        if (!) {
            // Send the connector a request for commit. The connector should
            // then validate the headers, send them (using sendHeader) and 
            // set the filters accordingly.
            .action(.null);
        }
        // Flush the current buffer
        if () {
            .flushBuffer();
        }
    }


    
Recycle the output buffer. This should be called when closing the connection.
    public void recycle() {
        // Recycle Request object
        .recycle();
        .recycle();
         = null;
         = 0;
         = -1;
         = false;
         = false;
    }


    
End processing of current HTTP request. Note: All bytes of the current request should have been already consumed. This method only resets all the pointers so that we are ready to parse the next HTTP request.
    public void nextRequest() {
        // Recycle Request object
        .recycle();
        .recycle();
        // Recycle filters
        for (int i = 0; i <= i++) {
            [i].recycle();
        }
        // Reset pointers
         = 0;
         = -1;
         = false;
         = false;
    }


    
End request.

Throws:
java.io.IOException an undelying I/O error occured
    public void endRequest()
        throws IOException {
        if (!) {
            // Send the connector a request for commit. The connector should
            // then validate the headers, send them (using sendHeader) and 
            // set the filters accordingly.
            .action(.null);
        }
        if ()
            return;
        if ( != -1)
            [].end();
        if () {
            .flushBuffer();
        }
         = true;
    }
    // ------------------------------------------------ HTTP/1.1 Output Methods


    
Send an acknoledgement.
    public void sendAck()
        throws IOException {
        if (!)
            .write(.);
    }


    
Send the response status line.
    public void sendStatus() {
        // Write protocol name
        write(.);
        [++] = .;
        // Write status code
        int status = .getStatus();
        switch (status) {
        case 200:
            write(.);
            break;
        case 400:
            write(.);
            break;
        case 404:
            write(.);
            break;
        default:
            write(status);
        }
        [++] = .;
        // Write message
        String message = null;
            message = .getMessage();
        }
        if (message == null) {
            write(getMessage(status));
        } else {
            write(message.replace('\n'' ').replace('\r'' '));
        }
        // End the response status line
           AccessController.doPrivileged(
                new PrivilegedAction(){
                    public Object run(){
                        [++] = .;
                        [++] = .;
                        return null;
                    }
                }
           );
        } else {
            [++] = .;
            [++] = .;
        }
    }
    private String getMessage(final int message){
           return (String)AccessController.doPrivileged(
                new PrivilegedAction(){
                    public Object run(){
                        return HttpMessages.getMessage(message); 
                    }
                }
           );
        } else {
            return HttpMessages.getMessage(message);
        }
    }

    
Send a header.

Parameters:
name Header name
value Header value
    public void sendHeader(MessageBytes nameMessageBytes value) {
        if (name.getLength() > 0 && !value.isNull()) {
            write(name);
            [++] = .;
            [++] = .;
            write(value);
            [++] = .;
            [++] = .;
        }
    }


    
Send a header.

Parameters:
name Header name
value Header value
    public void sendHeader(ByteChunk nameByteChunk value) {
        write(name);
        [++] = .;
        [++] = .;
        write(value);
        [++] = .;
        [++] = .;
    }


    
Send a header.

Parameters:
name Header name
value Header value
    public void sendHeader(String nameString value) {
        write(name);
        [++] = .;
        [++] = .;
        write(value);
        [++] = .;
        [++] = .;
    }


    
End the header block.
    public void endHeaders() {
        [++] = .;
        [++] = .;
    }
    // --------------------------------------------------- OutputBuffer Methods


    
Write the contents of a byte chunk.

Parameters:
chunk byte chunk
Returns:
number of bytes written
Throws:
java.io.IOException an undelying I/O error occured
    public int doWrite(ByteChunk chunkResponse res
        throws IOException {
        if (!) {
            // Send the connector a request for commit. The connector should
            // then validate the headers, send them (using sendHeaders) and 
            // set the filters accordingly.
            .action(.null);
        }
        if ( == -1)
            return .doWrite(chunkres);
        else
            return [].doWrite(chunkres);
    }
    // ------------------------------------------------------ Protected Methods


    
Commit the response.

Throws:
java.io.IOException an undelying I/O error occured
    protected void commit()
        throws IOException {
        // The response is now committed
         = true;
        .setCommitted(true);
        if ( > 0) {
            // Sending the response header buffer
            if () {
                .append(, 0, );
            } else {
                .write(, 0, );
            }
        }
    }


    
This method will write the contents of the specyfied message bytes buffer to the output stream, without filtering. This method is meant to be used to write the response header.

Parameters:
mb data to be written
    protected void write(MessageBytes mb) {
        if (mb.getType() == .) {
            ByteChunk bc = mb.getByteChunk();
            write(bc);
        } else if (mb.getType() == .) {
            CharChunk cc = mb.getCharChunk();
            write(cc);
        } else {
            write(mb.toString());
        }
    }


    
This method will write the contents of the specyfied message bytes buffer to the output stream, without filtering. This method is meant to be used to write the response header.

Parameters:
bc data to be written
    protected void write(ByteChunk bc) {
        // Writing the byte chunk to the output buffer
        int length = bc.getLength();
        System.arraycopy(bc.getBytes(), bc.getStart(), length);
         =  + length;
    }


    
This method will write the contents of the specyfied char buffer to the output stream, without filtering. This method is meant to be used to write the response header.

Parameters:
cc data to be written
    protected void write(CharChunk cc) {
        int start = cc.getStart();
        int end = cc.getEnd();
        char[] cbuf = cc.getBuffer();
        for (int i = starti < endi++) {
            char c = cbuf[i];
            // Note:  This is clearly incorrect for many strings,
            // but is the only consistent approach within the current
            // servlet framework.  It must suffice until servlet output
            // streams properly encode their output.
            if (((c <= 31) && (c != 9)) || c == 127 || c > 255) {
                c = ' ';
            }
            [++] = (bytec;
        }
    }


    
This method will write the contents of the specyfied byte buffer to the output stream, without filtering. This method is meant to be used to write the response header.

Parameters:
b data to be written
    public void write(byte[] b) {
        // Writing the byte chunk to the output buffer
        System.arraycopy(b, 0, b.length);
         =  + b.length;
    }


    
This method will write the contents of the specyfied String to the output stream, without filtering. This method is meant to be used to write the response header.

Parameters:
s data to be written
    protected void write(String s) {
        if (s == null)
            return;
        // From the Tomcat 3.3 HTTP/1.0 connector
        int len = s.length();
        for (int i = 0; i < leni++) {
            char c = s.charAt (i);
            // Note:  This is clearly incorrect for many strings,
            // but is the only consistent approach within the current
            // servlet framework.  It must suffice until servlet output
            // streams properly encode their output.
            if (((c <= 31) && (c != 9)) || c == 127 || c > 255) {
                c = ' ';
            }
            [++] = (bytec;
        }
    }


    
This method will print the specified integer to the output stream, without filtering. This method is meant to be used to write the response header.

Parameters:
i data to be written
    protected void write(int i) {
        write(String.valueOf(i));
    }


    
Callback to write data from the buffer.
    public void realWriteBytes(byte cbuf[], int offint len)
        throws IOException {
        if (len > 0) {
            .write(cbufofflen);
        }
    }
    // ----------------------------------- OutputStreamOutputBuffer Inner Class


    
This class is an output buffer which will write data to an output stream.
    protected class OutputStreamOutputBuffer 
        implements OutputBuffer {


        
Write chunk.
        public int doWrite(ByteChunk chunkResponse res
            throws IOException {
            int length = chunk.getLength();
            if () {
                .append(chunk.getBuffer(), chunk.getStart(), 
                                    length);
            } else {
                .write(chunk.getBuffer(), chunk.getStart(), 
                                   length);
            }
            return length;
        }
    }
New to GrepCode? Check out our FAQ X