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.jasper.runtime;
 
 import static org.jboss.web.JasperMessages.MESSAGES;
 
 import java.io.Writer;
An efficient character buffer class. This class is not thread-safe, so its clients will have to take care of any necessary thread issues.

Author(s):
Brian Remmington
 
 public class CharBuffer {
     private static final int DEFAULT_INITIAL_CAPACITY = 512;

    
List of all fully populated character arrays.
 
     private LinkedList bufList;
     private int listSize = 0;

    
The character array that is currently being filled
 
     private char[] currentBuf;

    
The index of the next character to write in 'currentBuf'.
 
     private int index;

    
The minimum number of characters by which the buffer should grow when 'currentBuf' becomes full.
 
     private int minimumGrowth;


    
Create a new character buffer, specifying its initial capacity. If this buffer ever has to grow then it will grow by at least the same as its initial capacity again

Parameters:
initialCapacity
 
     public CharBuffer(int initialCapacity) {
         this(initialCapacity, 0);
     }

    
Create a new character buffer, specifying its initial capacity and minimum growth.

Parameters:
initialCapacity The number of characters initially provisioned for.
minimumGrowth The smallest number of characters by which the buffer will grow. If zero is specified then the value of the initial capacity will be used.
 
     public CharBuffer(int initialCapacityint minimumGrowth) {
         if (initialCapacity == 0) {
             initialCapacity = ;
         }
         if (minimumGrowth == 0) {
             minimumGrowth = initialCapacity;
         }
         this. = new LinkedList();
         this. = new char[initialCapacity];
         this. = 0;
         this. = minimumGrowth;
         this. = 0;
     }

    
Add the supplied character to this buffer, growing the buffer if necessary.

Parameters:
character
 
     public void buffer(char character) {
         if (this.getCapacity() == 0) {
             this.grow();
         }
         [++] = character;
     }

    
Add a substring of the supplied string to this buffer, growing the buffer if necessary.

Parameters:
text The original String that is to be added to the buffer.
start The index of the starting character to add to the buffer (zero-based).
length The number of characters to add to the buffer.
Throws:
java.lang.IllegalArgumentException If text is null, start points beyond end of text, or start + length goes beyond end of text.
    public void buffer(String textint startint lengththrows IllegalArgumentException {
        //This method looks very similar to the overloaded version of it, but I can't see a good way of
        //refactoring without introducing at least one extra char[] allocation and arraycopy per write.
        //Ideas welcome.
        if (length <= 0) {
            return;
        }
        if (text == null) {
            throw .nullCharBufferTextArgument();
        }
        if (start > text.length()) {
            throw .invalidCharBufferStartPosition();
        }
        if ((start + length) > text.length()) {
            throw .invalidCharBufferLength();
        }
        //If length of string to add is greater than will fit in current char array then add what will fit
        //and then bolt the rest on.
        int charsToCopy = 0;
        while (length != 0) {
            charsToCopy = this.getCapacity();
            if (charsToCopy > length) {
                charsToCopy = length;
            }
            if (charsToCopy > 0) {
                //Add as many characters as will fit in currentBuf, updating the indexes as necessary.
                text.getChars(startstart + charsToCopy);
                start += charsToCopy;
                length -= charsToCopy;
                this. += charsToCopy;
            }
            //If there are still some characters to write then grow the buffer by enough to take the 
            //remainder and loop.
            if (length != 0) {
                this.grow(length);
            }
        }
    }

    
Add a section of the supplied character array to this buffer, growing the buffer if necessary.

Parameters:
characters The character array that is to be added to the buffer.
start The index of the starting character to add to the buffer (zero-based).
length The number of characters to add to the buffer.
Throws:
java.lang.IllegalArgumentException If array is null, start points beyond end of array, or start + length goes beyond end of array.
    public void buffer(char[] charactersint startint lengththrows IllegalArgumentException {
        //This method looks very similar to the overloaded version of it, but I can't see a good way of
        //refactoring without introducing at least one extra char[] allocation and arraycopy per write.
        //Ideas welcome.
        if (length <= 0) {
            return;
        }
        if (characters == null) {
            throw .nullCharBufferCharactersArgument();
        }
        if (start > characters.length) {
            throw .invalidCharBufferStartPosition();
        }
        if ((start + length) > characters.length) {
            throw .invalidCharBufferLength();
        }
        //If length of string to add is greater than will fit in current char array then add what will fit
        //and then bolt the rest on.
        int charsToCopy = 0;
        while (length != 0) {
            charsToCopy = this.getCapacity();
            if (charsToCopy > length) {
                charsToCopy = length;
            }
            if (charsToCopy > 0) {
                //Add as many characters as will fit in currentBuf, updating the indexes as necessary.
                System.arraycopy(charactersstartcharsToCopy);
                start += charsToCopy;
                length -= charsToCopy;
                this. += charsToCopy;
            }
            //If there are still some characters to write then grow the buffer by enough to take the 
            //remainder and loop.
            if (length != 0) {
                this.grow(length);
            }
        }
    }

    
Render this buffer as a character array.

Returns:
    public char[] toArray() {
        char[] result = new char[size()];
        int offset = 0;
        for (Iterator iter = this..iterator(); iter.hasNext();) {
            char[] curBuf = (char[]) iter.next();
            System.arraycopy(curBuf, 0, resultoffsetcurBuf.length);
            offset += curBuf.length;
        }
        System.arraycopy(this., 0, resultoffset);
        return result;
    }

    
Render this buffer as a String.
    public String toString()
    {
        StringBuilder sb = new StringBuilder(size());
        for (Iterator iter = this..iterator(); iter.hasNext();) {
            char[] curBuf = (char[]) iter.next();
            sb.append(curBuf);
        }
        sb.append(, 0, );
        return sb.toString();
    }

    
Find the current capacity of this buffer.

Returns:
The capacity of this buffer. This is the number of characters that can be added to this buffer without it having to grow.
    public int getCapacity() {
        return this..length - this.;
    }

    
Obtain the current size of this buffer.

Returns:
The number of characters that are currently in this buffer. Equates to the size of array that would be returned by toArray() or the length of String returned by toString().
    public int size() {
        return (this. + );
    }

    
Clear this buffer. Upon return, a call to size() will return zero.
    public void clear() {
        this..clear();
        this. = 0;
        this. = 0;
    }

    
Write the content of this buffer out to the supplied Writer object. This will not flush the writer before returning.

Parameters:
writer The writer in to which the buffer should be written.
Throws:
java.io.IOException If any error occurs while writing the buffer into the stream.
java.lang.IllegalArgumentException If 'writer' is null.
    public void writeOut(Writer writerthrows IOExceptionIllegalArgumentException {
        if (writer == null) {
            throw .nullCharBufferWriterArgument();
        }
        for (Iterator iter = this..iterator(); iter.hasNext();) {
            char[] curBuf = (char[]) iter.next();
            writer.write(curBuf);
        }
        writer.write(, 0, );
    }
    private void grow() {
        this.grow(this.);
    }
    private void grow(int requiredChars) {
        if (requiredChars < this.) {
            requiredChars = this.;
        }
        this..add();
        this. += .;
         = new char[requiredChars];
         = 0;
    }
New to GrepCode? Check out our FAQ X