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.catalina.connector;
 
 import static org.jboss.web.CatalinaMessages.MESSAGES;
 
 import java.io.Reader;
 
The buffer used by Tomcat request. This is a derivative of the Tomcat 3.3 OutputBuffer, adapted to handle input instead of output. This allows complete recycling of the facade objects (the ServletInputStream and the BufferedReader).

Author(s):
Remy Maucherat
 
 public class InputBuffer extends Reader
 
 
     // -------------------------------------------------------------- Constants
 
 
     public static final String DEFAULT_ENCODING = 
     public static final int DEFAULT_BUFFER_SIZE = 8*1024;
 
     // The buffer can be used for byte[] and char[] reading
     // ( this is needed to support ServletInputStream and BufferedReader )
     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;


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


    
Flag which indicates if the end of stream has been reached.
 
     private boolean eof = false;


    
Encoding to use.
    private String enc;


    
Encoder is set.
    private boolean gotEnc = false;


    
List of encoders.
    protected HashMap<StringB2CConverterencoders = 
        new HashMap<StringB2CConverter>();


    
Current byte to char converter.
    protected B2CConverter conv;


    
Associated Coyote request.
    private Request coyoteRequest;


    
Associated request.
    private org.apache.catalina.connector.Request request;


    
Buffer position.
    private int markPos = -1;


    
Buffer size.
    private int size = -1;


    
Read listener.
    private ReadListener readListener = null;
    // ----------------------------------------------------------- Constructors


    
Default constructor. Allocate the buffer with the default buffer size.
    public InputBuffer(org.apache.catalina.connector.Request request) {
        this(request);
    }


    
Alternate constructor which allows specifying the initial buffer size.

Parameters:
size Buffer size to use
    public InputBuffer(org.apache.catalina.connector.Request requestint size) {
        this. = request;
        this. = size;
         = new ByteChunk(size);
        .setLimit(size);
        .setByteInputChannel(this);
         = new CharChunk(size);
        .setLimit(size);
        .setOptimizedWrite(false);
        .setCharInputChannel(this);
    }
    // ------------------------------------------------------------- Properties


    
Associated Coyote request.

Parameters:
coyoteRequest Associated Coyote request
    public void setRequest(Request coyoteRequest) {
	this. = coyoteRequest;
    }


    
Get associated Coyote request.

Returns:
the associated Coyote request
    public Request getRequest() {
        return this.;
    }
    // --------------------------------------------------------- Public Methods


    
Recycle the output buffer.
    public void recycle() {
        
         = ;
        
        // If usage of mark made the buffer too big, reallocate it
        if (.getChars().length > ) {
             = new CharChunk();
            .setLimit();
            .setOptimizedWrite(false);
            .setCharInputChannel(this);
        } else {
            .recycle();
        }
         = -1;
        .recycle(); 
         = false;
         = false;
        
        if ( != null) {
            .recycle();
        }
        
         = false;
         = null;
         = null;
    }


    
Clear cached encoders (to save memory for event requests).
    public void clearEncoders() {
        .clear();
    }
    
    
    
Close the input buffer.

Throws:
java.io.IOException An underlying IOException occurred
    public void close()
        throws IOException {
        if (.getUpgradeHandler() != null && .isEventMode()) {
            .getEvent().close();
        } else {
             = true;
        }
    }


    
Returns if the request is closed.
    public boolean isClosed() {
        return ;
    }


    
Returns if the eof has been reached.
    public boolean isEof() {
        return ;
    }


    
Reset the eof state.
    public void resetEof() {
         = false;
    }
    public int available() {
        if ( || ) {
            return -1;
        }
        int available = 0;
        if ( != ) {
            available = .getLength();
            if (.isEventMode() && available == 0) {
                try {
                    .action(.null);
                    available = realReadBytes(null, 0, 0);
                } catch (IOException e) {
                    // Ignore, will return 0, and another error
                    // will occur elsewhere
                }
            }
        } else {
            available = .getLength();
            if (.isEventMode() && available == 0) {
                try {
                    .action(.null);
                    available = realReadChars(null, 0, .getBuffer().length);
                } catch (IOException e) {
                    // Ignore, will return 0, and another error
                    // will occur elsewhere
                }
            }
        }
        return available;
    }
    public int getAvailable() {
        if ( || ) {
            return -1;
        }
        int available = 0;
        if ( != ) {
            available = .getLength();
        } else {
            available = .getLength();
        }
        return available;
    }
    // ------------------------------------------------- Bytes Handling Methods


    
Reads new bytes in the byte chunk.

Parameters:
cbuf Byte buffer to be written to the response
off Offset
len Length
Throws:
java.io.IOException An underlying IOException occurred
    public int realReadBytes(byte cbuf[], int offint len)
	throws IOException {
        if ()
            return -1;
        if ( == null)
            return -1;
        if( == )
             = ;
        try {
            int n = .doRead();
            if (n < 0) {
                 = true;
            }
            return n;
        } catch (IOException e) {
            // An IOException on a read is almost always due to
            // the remote client aborting the request or a timeout occurring.
            // Wrap this so that it can be handled better by the error 
            // dispatcher.
            throw new ClientAbortException(e);
        }
    }
    public int readByte()
        throws IOException {
        if ()
            throw .streamClosed();
        return .substract();
    }
    public int read(byte[] bint offint len)
        throws IOException {
        if ()
            throw .streamClosed();
        return .substract(bofflen);
    }
    // ------------------------------------------------- Chars Handling Methods
    public void setEncoding(String s) {
         = s;
    }
    public int realReadChars(char cbuf[], int offint len)
        throws IOException {
        if (!)
            setConverter();
        if (.getLength() <= 0) {
            if (realReadBytes(.getBytes(), 0, .getBytes().length) < 0) {
                return -1;
            }
        }
        if ( == -1) {
            .setOffset(0);
            .setEnd(0);
        } else {
            // Make sure there's enough space in the worst case
            .makeSpace(.getLength());
            if ((.getBuffer().length - .getEnd()) == 0) {
                // We went over the limit
                .setOffset(0);
                .setEnd(0);
                 = -1;
            }
        }
         = ;
        .convert();
        return .getLength();
    }
    public int read()
        throws IOException {
        if ()
            throw .streamClosed();
        return .substract();
    }
    public int read(char[] cbuf)
        throws IOException {
        return read(cbuf, 0, cbuf.length);
    }
    public int read(char[] cbufint offint len)
        throws IOException {
        if ()
            throw .streamClosed();
        return .substract(cbufofflen);
    }
    public long skip(long n)
        throws IOException {
        if ()
            throw .streamClosed();
        if (n < 0) {
            throw new IllegalArgumentException();
        }
        long nRead = 0;
        while (nRead < n) {
            if (.getLength() >= n) {
                .setOffset(.getStart() + (intn);
                nRead = n;
            } else {
                nRead += .getLength();
                .setOffset(.getEnd());
                int toRead = 0;
                if (.getChars().length < (n - nRead)) {
                    toRead = .getChars().length;
                } else {
                    toRead = (int) (n - nRead);
                }
                int nb = realReadChars(.getChars(), 0, toRead);
                if (nb < 0)
                    break;
            }
        }
        return nRead;
    }
    public boolean ready()
        throws IOException {
        if ()
            throw .streamClosed();
        return (available() > 0);
    }
    public boolean markSupported() {
        return true;
    }
    public void mark(int readAheadLimit)
        throws IOException {
        if ()
            throw .streamClosed();
        if (.getLength() <= 0) {
            .setOffset(0);
            .setEnd(0);
        } else {
            if ((.getBuffer().length > (2 * )) 
                && (.getLength()) < (.getStart())) {
                System.arraycopy(.getBuffer(), .getStart(), 
                                 .getBuffer(), 0, .getLength());
                .setEnd(.getLength());
                .setOffset(0);
            }
        }
        if (.getStart() + readAheadLimit > .getLimit()) {
            .setLimit(.getStart() + readAheadLimit);
        }
         = .getStart();
    }
    public void reset()
        throws IOException {
        if ()
            throw .streamClosed();
        if ( == ) {
            if ( < 0) {
                throw new IOException();
            } else {
                .setOffset();
            }
        } else {
            .recycle();
        }
    }
    public void checkConverter() 
        throws IOException {
        if (!)
            setConverter();
    }
    protected void setConverter()
        throws IOException {
        if ( != null)
             = .getCharacterEncoding();
         = true;
         = ( == null) ?  : .toUpperCase(.);
         = .get();
        if ( == null) {
            if (SecurityUtil.isPackageProtectionEnabled()) {
                try {
                     = (B2CConverter) AccessController
                            .doPrivileged(new PrivilegedExceptionAction<B2CConverter>() {
                                public B2CConverter run() throws IOException {
                                    return new B2CConverter();
                                }
                            });
                } catch (PrivilegedActionException ex) {
                    Exception e = ex.getException();
                    if (e instanceof IOException)
                        throw (IOExceptione;
                }
            } else {
                 = new B2CConverter();
            }
            .put();
        }
    }
    public ReadListener getReadListener() {
        return ;
    }
    public void setReadListener(ReadListener readListener) {
        if (this. != null) {
            throw .readListenerAlreadySet();
        }
        if (readListener == null) {
            throw .nullListener();
        }
        if (!.isEventMode()) {
            throw .cannotSetListenerWithoutUpgradeOrAsync();
        }
        this. = readListener;
                (.getUpgradeHandler() != null) ? readListener : null);
    }
New to GrepCode? Check out our FAQ X