org.apache.commons.csv

Class CharBuffer


public class CharBuffer
extends java.lang.Object

A simple StringBuffer replacement that aims to reduce copying as much as possible. The buffer grows as necessary. This class is not thread safe.
Author:
Ortwin Gl?ck

Field Summary

private char[]
c
private int
length
Actually used number of characters in the array.

Constructor Summary

CharBuffer()
Creates a new CharBuffer with an initial capacity of 32 characters.
CharBuffer(int length)
Creates a new CharBuffer with an initial capacity of length characters.

Method Summary

void
append(String s)
Appends s to the end of this CharBuffer.
void
append(StringBuffer sb)
Appends sb to the end of this CharBuffer.
void
append(char data)
Appends a single character to the end of this CharBuffer.
void
append(char[] data)
Appends data to the end of this CharBuffer.
void
append(CharBuffer cb)
Appends the contents of cb to the end of this CharBuffer.
int
capacity()
Returns the current capacity of the buffer.
void
clear()
Empties the buffer.
char[]
getCharacters()
Returns the contents of the buffer as a char[].
int
length()
Returns the number of characters in the buffer.
void
provideCapacity(int capacity)
Copies the data into a new array of at least capacity size.
void
shrink()
Shrinks the capacity of the buffer to the current length if necessary.
String
toString()
Converts the contents of the buffer into a StringBuffer.
StringBuffer
toStringBuffer()
Converts the contents of the buffer into a StringBuffer.

Field Details

c

private char[] c

length

private int length
Actually used number of characters in the array. It is also the index at which a new character will be inserted into c.

Constructor Details

CharBuffer

public CharBuffer()
Creates a new CharBuffer with an initial capacity of 32 characters.

CharBuffer

public CharBuffer(int length)
Creates a new CharBuffer with an initial capacity of length characters.

Method Details

append

public void append(String s)
Appends s to the end of this CharBuffer. This method involves copying the new data once!
Parameters:
s - the String to append or null

append

public void append(StringBuffer sb)
Appends sb to the end of this CharBuffer. This method involves copying the new data once!
Parameters:
sb - the StringBuffer to append or null

append

public void append(char data)
Appends a single character to the end of this CharBuffer. This method involves copying the new data once!
Parameters:
data - the char to append

append

public void append(char[] data)
Appends data to the end of this CharBuffer. This method involves copying the new data once!
Parameters:
data - the char[] to append or null

append

public void append(CharBuffer cb)
Appends the contents of cb to the end of this CharBuffer.
Parameters:
cb - the CharBuffer to append or null

capacity

public int capacity()
Returns the current capacity of the buffer.
Returns:
the maximum number of characters that can be stored in this buffer without resizing it.

clear

public void clear()
Empties the buffer. The capacity still remains the same, so no memory is freed.

getCharacters

public char[] getCharacters()
Returns the contents of the buffer as a char[]. The returned array may be the internal array of the buffer, so the caller must take care when modifying it. This method allows to avoid copying if the caller knows the exact capacity before.
Returns:

length

public int length()
Returns the number of characters in the buffer.
Returns:
the number of characters

provideCapacity

public void provideCapacity(int capacity)
Copies the data into a new array of at least capacity size.
Parameters:
capacity -

shrink

public void shrink()
Shrinks the capacity of the buffer to the current length if necessary. This method involves copying the data once!

toString

public String toString()
Converts the contents of the buffer into a StringBuffer. This method involves copying the new data once!
Returns:

toStringBuffer

public StringBuffer toStringBuffer()
Converts the contents of the buffer into a StringBuffer. This method involves copying the new data once!
Returns: