com.claritysys.util
Class FixedWidthStringBuffer

java.lang.Object
  extended by com.claritysys.util.FixedWidthStringBuffer

public final class FixedWidthStringBuffer
extends java.lang.Object

A StringBuffer which provides for appending fixed-width fields. The underlying StringBuffer can be accessed directly via getStringBuffer () if desired.

If you look into the mechanics of the java.lang.String and java.lang.StringBuffer class, you will understand why it is better to create a new one of these each time you need it as opposed to performing a StringBuffer.setLength (0), which forces re-allocation of the char[].

 FixedWidthStringBuffer buf = new FixedWidthStringBuffer (100);
 buf.append (lastName, 25);
 buf.append (firstName, 25);
 System.out.println (buf.toString ());
 buf = new FixedWidthStringBuffer (100);
 ...
 

Version:
$Revision: 2348 $
See Also:
String, StringBuffer

Constructor Summary
FixedWidthStringBuffer()
          Create a new FixedWidthStringBuffer with an initial size of 64 characters.
FixedWidthStringBuffer(int initialSize)
          Create a new FixedWidthStringBuffer with an initial size of 64 characters.
 
Method Summary
 void append(char ch)
          Several accessor methods which delegate directly to the StringBuffer.
 void append(java.lang.String s)
          Several accessor methods which delegate directly to the StringBuffer.
 void append(java.lang.String s, int width)
          Append the given String to the buffer, ensuring it occupies the given width.
 char charAt(int pos)
          Several accessor methods which delegate directly to the StringBuffer.
 java.lang.StringBuffer getStringBuffer()
          Retrive the underlying StringBuffer.
 int length()
          Several accessor methods which delegate directly to the StringBuffer.
 void setCharAt(int pos, char ch)
          Set the character at the specified position to ch.
 java.lang.String toString()
          Return the string from the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FixedWidthStringBuffer

public FixedWidthStringBuffer()
Create a new FixedWidthStringBuffer with an initial size of 64 characters.


FixedWidthStringBuffer

public FixedWidthStringBuffer(int initialSize)
Create a new FixedWidthStringBuffer with an initial size of 64 characters.

Parameters:
initialSize - The initial size of the underlying StringBuffer.
Method Detail

append

public void append(java.lang.String s,
                   int width)
Append the given String to the buffer, ensuring it occupies the given width. If longer, truncate. If shorter, pad with spaces.

Parameters:
s - The String to append.
width - The field width.

getStringBuffer

public java.lang.StringBuffer getStringBuffer()
Retrive the underlying StringBuffer.

Returns:
The StringBuffer.

append

public void append(java.lang.String s)
Several accessor methods which delegate directly to the StringBuffer.

Parameters:
s - the string to append.

append

public void append(char ch)
Several accessor methods which delegate directly to the StringBuffer.

Parameters:
ch - the char to append.

length

public int length()
Several accessor methods which delegate directly to the StringBuffer.

Returns:
the length of the stringbuffer.

charAt

public char charAt(int pos)
Several accessor methods which delegate directly to the StringBuffer.

Parameters:
pos -
Returns:
the charAt(pos).

setCharAt

public void setCharAt(int pos,
                      char ch)
Set the character at the specified position to ch.

Parameters:
pos - The position in the buffer at which to place the character.
ch - The character to place in the buffer.

toString

public java.lang.String toString()
Return the string from the buffer. Note that since this is calling toString() on the StringBuffer, any subsequent append() calls will force a new char[] to be allocated. See the JDK source code for details.

Overrides:
toString in class java.lang.Object
Returns:
the string.
See Also:
String, StringBuffer


Copyright ? 2002 Clarity Systems Group, LLC. All Rights Reserved.