net.sf.saxon.om

Class FastStringBuffer

Implemented Interfaces:
CharSequence, Serializable

public final class FastStringBuffer
extends java.lang.Object
implements CharSequence, Serializable

A simple implementation of a class similar to StringBuffer. Unlike StringBuffer it is not synchronized. It also offers the capability to remove unused space. (This class could possibly be replaced by StringBuilder in JDK 1.5, but using our own class gives more control.)

Constructor Summary

FastStringBuffer(CharSequence cs)
Create a FastStringBuffer with initial content
FastStringBuffer(int initialSize)
Create a FastStringBuffer with a given initial capacity

Method Summary

void
append(CharSequence s)
Append the contents of a general CharSequence to the buffer
void
append(String s)
Append the contents of a String to the buffer
void
append(StringBuffer s)
Append the contents of a StringBuffer to the buffer
void
append(char ch)
Append a character to the buffer
void
append(char[] srcArray)
Append the entire contents of a character array to the buffer
void
append(char[] srcArray, int start, int length)
Append the contents of a character array to the buffer
void
append(FastStringBuffer s)
Append the contents of a FastStringBuffer to the buffer
void
append(CharSlice s)
Append the contents of a CharSlice to the buffer
void
appendWideChar(int ch)
Append a wide character to the buffer (as a surrogate pair if necessary)
char
charAt(int index)
Returns the char value at the specified index.
CharSequence
condense()
Remove surplus space from the array.
static String
diagnosticPrint(CharSequence in)
Diagnostic print of the contents of a CharSequence.
void
ensureCapacity(int extra)
Expand the character array if necessary to ensure capacity for appended data
char[]
getCharArray()
Get a char[] array containing the characters.
void
getChars(int srcBegin, int srcEnd, dst[] , int dstBegin)
Copies characters from this FastStringBuffer into the destination character array.
int
indexOf(char ch)
Get the index of the first character equal to a given value
void
insertCharAt(int index, char ch)
Insert a character at a particular offset
int
length()
Returns the length of this character sequence.
void
prepend(char ch)
Insert a given character at the start of the buffer
void
prependRepeated(char ch, int repeat)
Insert a given character N times at the start of the buffer
void
prependWideChar(int ch)
Prepend a wide character to the buffer (as a surrogate pair if necessary)
void
removeCharAt(int index)
Remove a character at a particular offset
void
setCharAt(int index, char ch)
Set the character at a particular offset
void
setLength(int length)
Set the length.
CharSequence
subSequence(int start, int end)
Returns a new CharSequence that is a subsequence of this sequence.
String
toString()
Convert contents of the FastStringBuffer to a string
void
write(Writer writer)
Write the value to a writer

Constructor Details

FastStringBuffer

public FastStringBuffer(CharSequence cs)
Create a FastStringBuffer with initial content
Parameters:
cs - the initial content. The buffer is created with just enough capacity for this content (it will be expanded if more content is added later).

FastStringBuffer

public FastStringBuffer(int initialSize)
Create a FastStringBuffer with a given initial capacity
Parameters:
initialSize - the initial capacity

Method Details

append

public void append(CharSequence s)
Append the contents of a general CharSequence to the buffer
Parameters:
s - the CharSequence to be appended

append

public void append(String s)
Append the contents of a String to the buffer
Parameters:
s - the String to be appended

append

public void append(StringBuffer s)
Append the contents of a StringBuffer to the buffer
Parameters:
s - the StringBuffer to be appended

append

public void append(char ch)
Append a character to the buffer
Parameters:
ch - the character to be added

append

public void append(char[] srcArray)
Append the entire contents of a character array to the buffer
Parameters:
srcArray - the array whose contents are to be added

append

public void append(char[] srcArray,
                   int start,
                   int length)
Append the contents of a character array to the buffer
Parameters:
srcArray - the array whose contents are to be added
start - the offset of the first character in the array to be copied
length - the number of characters to be copied

append

public void append(FastStringBuffer s)
Append the contents of a FastStringBuffer to the buffer
Parameters:
s - the FastStringBuffer to be appended

append

public void append(CharSlice s)
Append the contents of a CharSlice to the buffer
Parameters:
s - the String to be appended

appendWideChar

public void appendWideChar(int ch)
Append a wide character to the buffer (as a surrogate pair if necessary)
Parameters:
ch - the character, as a 32-bit Unicode codepoint

charAt

public char charAt(int index)
Parameters:
index - the index of the char value to be returned
Returns:
the specified char value

condense

public CharSequence condense()
Remove surplus space from the array. This doesn't reduce the array to the minimum possible size; it only reclaims space if it seems worth doing. Specifically, it contracts the array if the amount of wasted space is more than 256 characters, or more than half the allocated size and more than 20 chars.
Returns:
the buffer after removing unused space

diagnosticPrint

public static String diagnosticPrint(CharSequence in)
Diagnostic print of the contents of a CharSequence. Ordinary printable ASCII characters are displayed as themselves; anything else is displayed as a \\uNNNN escape sequence
Parameters:
in - the CharSequence whose contents are to be displayed.
Returns:
the diagnostic output

ensureCapacity

public void ensureCapacity(int extra)
Expand the character array if necessary to ensure capacity for appended data
Parameters:
extra - the amount of additional capacity needed, in characters

getCharArray

public char[] getCharArray()
Get a char[] array containing the characters. The caller should not modify the array.
Returns:
a char[] array containing the characters

getChars

public void getChars(int srcBegin,
                     int srcEnd,
                     dst[] ,
                     int dstBegin)
Copies characters from this FastStringBuffer into the destination character array.

The first character to be copied is at index srcBegin; the last character to be copied is at index srcEnd-1 (thus the total number of characters to be copied is srcEnd-srcBegin). The characters are copied into the subarray of dst starting at index dstBegin and ending at index:

     dstbegin + (srcEnd-srcBegin) - 1
 
Parameters:
srcBegin - index of the first character in the string to copy.
srcEnd - index after the last character in the string to copy.
dstBegin - the start offset in the destination array.

indexOf

public int indexOf(char ch)
Get the index of the first character equal to a given value
Parameters:
ch - the character to search for
Returns:
the position of the first occurrence, or -1 if not found

insertCharAt

public void insertCharAt(int index,
                         char ch)
Insert a character at a particular offset
Parameters:
index - the index of the character to be set
ch - the new character to insert at that location

length

public int length()
Returns the length of this character sequence. The length is the number of 16-bit chars in the sequence.
Returns:
the number of chars in this sequence

prepend

public void prepend(char ch)
Insert a given character at the start of the buffer
Parameters:
ch - the character to insert

prependRepeated

public void prependRepeated(char ch,
                            int repeat)
Insert a given character N times at the start of the buffer
Parameters:
ch - the character to insert
repeat - the number of occurrences required. Supplying 0 or a negative number is OK, and is treated as a no-op.

prependWideChar

public void prependWideChar(int ch)
Prepend a wide character to the buffer (as a surrogate pair if necessary)
Parameters:
ch - the character, as a 32-bit Unicode codepoint

removeCharAt

public void removeCharAt(int index)
Remove a character at a particular offset
Parameters:
index - the index of the character to be set

setCharAt

public void setCharAt(int index,
                      char ch)
Set the character at a particular offset
Parameters:
index - the index of the character to be set
ch - the new character to overwrite the existing character at that location

setLength

public void setLength(int length)
Set the length. If this exceeds the current length, this method is a no-op. If this is less than the current length, characters beyond the specified point are deleted.
Parameters:
length - the new length

subSequence

public CharSequence subSequence(int start,
                                int end)
Returns a new CharSequence that is a subsequence of this sequence. The subsequence starts with the char value at the specified index and ends with the char value at index end - 1. The length (in chars) of the returned sequence is end - start, so if start == end then an empty sequence is returned.
Parameters:
start - the start index, inclusive
end - the end index, exclusive
Returns:
the specified subsequence

toString

public String toString()
Convert contents of the FastStringBuffer to a string

write

public void write(Writer writer)
            throws java.io.IOException
Write the value to a writer
Parameters:
writer - the writer to which the content is to be written