Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ************************************************************************
  #
  #  DivConq
  #
  #  http://divconq.com/
  #
  #  Copyright:
  #    Copyright 2014 eTimeline, LLC. All rights reserved.
  #
 #  License:
 #    See the license.txt file in the project's top-level directory for details.
 #
 #  Authors:
 #    * Andy White
 #
 ************************************************************************ */
 
 /*
  * ParseBuffer.java February 2001
  *
  * Copyright (C) 2001, Niall Gallagher <niallg@users.sf.net>
  *
  * 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 divconq.www.util.parse;

This is primarily used to replace the StringBuffer class, as a way for the Parser to store the char's for a specific region within the parse data that constitutes a desired value. The methods are not synchronized so it enables the char's to be taken quicker than the StringBuffer class.

Author(s):
Niall Gallagher
 
 public class ParseBuffer {      

   
This is used to quicken toString.
 
    protected String cache;

   
The char's this buffer accumulated.
 
    protected char[] buf;

   
This is the number of char's stored.
 
    protected int count;
   
   
Constructor for ParseBuffer. The default ParseBuffer stores 16 char's before a resize is needed to accommodate extra characters.
 
    public ParseBuffer(){
       this(16);
    }
   
   
This creates a ParseBuffer with a specific default size. The buffer will be created the with the length specified. The ParseBuffer can grow to accommodate a collection of char's larger the the size specified.

Parameters:
size initial size of this ParseBuffer
 
    public ParseBuffer(int size){
       this. = new char[size];
    }
   
   
This will add a char to the end of the buffer. The buffer will not overflow with repeated uses of the append, it uses an ensureCapacity method which will allow the buffer to dynamically grow in size to accommodate more char's.

Parameters:
c the char to be appended
 
    public void append(char c){
       ensureCapacity(+ 1);
       [++] = c;
   }

   
This will add a String to the end of the buffer. The buffer will not overflow with repeated uses of the append, it uses an ensureCapacity method which will allow the buffer to dynamically grow in size to accommodate large String objects.

Parameters:
text the String to be appended to this
  
   public void append(String text){
      ensureCapacity(text.length());
      text.getChars(0,text.length(),,);
       += text.length();
   }
   
   
This will reset the buffer in such a way that the buffer is cleared of all contents and then has the given string appended. This is used when a value is to be set into the buffer value. See the append(String) method for reference.

Parameters:
text this is the text that is to be appended to this
   public void reset(String text) {      
      clear();      
      append(text);
   }

   
This will add a ParseBuffer to the end of this. The buffer will not overflow with repeated uses of the append, it uses an ensureCapacity method which will allow the buffer to dynamically grow in size to accommodate large ParseBuffer objects.

Parameters:
text the ParseBuffer to be appended
  
   public void append(ParseBuffer text){
      append(text.buf, 0, text.count);           
   }
   
   
This will reset the buffer in such a way that the buffer is cleared of all contents and then has the given string appended. This is used when a value is to be set into the buffer value. See the append(ParseBuffer) method for reference.

Parameters:
text this is the text that is to be appended to this
   public void reset(ParseBuffer text) {      
      clear();      
      append(text);
   }
   
This will add a char to the end of the buffer. The buffer will not overflow with repeated uses of the append, it uses an ensureCapacity method which will allow the buffer to dynamically grow in size to accommodate large char arrays.

Parameters:
c the char array to be appended to this
off the read offset for the array
len the number of char's to add
   
   public void append(char[] cint offint len){
      ensureCapacity(len);
      System.arraycopy(c,off,,,len);
      +=len;
   }
   
   
This will add a String to the end of the buffer. The buffer will not overflow with repeated uses of the append, it uses an ensureCapacity method which will allow the buffer to dynamically grow in size to accommodate large String objects.

Parameters:
str the String to be appended to this
off the read offset for the String
len the number of char's to add
   
   public void append(String strint offint len){
      ensureCapacity(len);
      str.getChars(off,len,,);  
       += len;
   }


   
This will add a ParseBuffer to the end of this. The buffer will not overflow with repeated uses of the append, it uses an ensureCapacity method which will allow the buffer to dynamically grow in size to accommodate large ParseBuffer objects.

Parameters:
text the ParseBuffer to be appended
off the read offset for the ParseBuffer
len the number of char's to add
  
   public void append(ParseBuffer textint offint len){
      append(text.bufofflen);           
   }   
   
   
This ensure that there is enough space in the buffer to allow for more char's to be added. If the buffer is already larger than min then the buffer will not be expanded at all.

Parameters:
min the minimum size needed
     
   protected void ensureCapacity(int min) {
      if(. < min) {
         int size = . * 2;
         int max = Math.max(minsize);
         char[] temp = new char[max];         
         System.arraycopy(, 0, temp, 0, ); 
          = temp;
      }
   }  
   
   
This will empty the ParseBuffer so that the toString parameter will return null. This is used so that the same ParseBuffer can be recycled for different tokens.
   public void clear(){
       = null;
       = 0;
   }
  
   
This will return the number of bytes that have been appended to the ParseBuffer. This will return zero after the clear method has been invoked.

Returns:
the number of char's within the buffer
   public int length(){
      return ;
   }

   
This will return the characters that have been appended to the ParseBuffer as a String object. If the String object has been created before then a cached String object will be returned. This method will return null after clear is invoked.

Returns:
the char's appended as a String
   public String toString(){
      if( <= 0) {
         return null;
      }
      if( != null) {
         return ;
      }
       = new String(,0,);
      return ;
   }
}   
New to GrepCode? Check out our FAQ X