public final class TextBuffer
extends java.lang.Object
StringBuffer, with
following differences:
| Modifier and Type | Field and Description |
|---|---|
private BufferRecycler |
_allocator |
private char[] |
_currentSegment |
private int |
_currentSize
Number of characters in currently active (last) segment
|
private boolean |
_hasSegments
Flag that indicates whether _seqments is non-empty
|
private char[] |
_inputBuffer
Shared input buffer; stored here in case some input can be returned
as is, without being copied to collector's own buffers.
|
private int |
_inputLen |
private int |
_inputStart
Character offset of first char in input buffer; -1 to indicate
that input buffer currently does not contain any useful char data
|
private char[] |
_resultArray |
private java.lang.String |
_resultString
String that will be constructed when the whole contents are
needed; will be temporarily stored in case asked for again.
|
private java.util.ArrayList<char[]> |
_segments
List of segments prior to currently active segment.
|
private int |
_segmentSize
Amount of characters in segments in
_segments |
(package private) static int |
MAX_SEGMENT_LEN
Let's limit maximum segment length to something sensible
like 256k
|
(package private) static int |
MIN_SEGMENT_LEN
Let's start with sizable but not huge buffer, will grow as necessary
|
(package private) static char[] |
NO_CHARS |
| Constructor and Description |
|---|
TextBuffer(BufferRecycler allocator) |
| Modifier and Type | Method and Description |
|---|---|
void |
append(char c) |
void |
append(char[] c,
int start,
int len) |
void |
append(java.lang.String str,
int offset,
int len) |
private char[] |
buf(int needed)
Helper method used to find a buffer to use, ideally one
recycled earlier.
|
private char[] |
carr(int len) |
private void |
clearSegments() |
char[] |
contentsAsArray() |
java.math.BigDecimal |
contentsAsDecimal()
Convenience method for converting contents of the buffer
into a
BigDecimal. |
double |
contentsAsDouble()
Convenience method for converting contents of the buffer
into a Double value.
|
java.lang.String |
contentsAsString() |
char[] |
emptyAndGetCurrentSegment() |
void |
ensureNotShared()
Method called to make sure that buffer is not using shared input
buffer; if it is, it will copy such contents to private buffer.
|
private void |
expand(int minNewSegmentSize)
Method called when current segment is full, to allocate new
segment.
|
char[] |
expandCurrentSegment()
Method called to expand size of the current segment, to
accommodate for more contiguous content.
|
char[] |
expandCurrentSegment(int minSize)
Method called to expand size of the current segment, to
accommodate for more contiguous content.
|
char[] |
finishCurrentSegment() |
char[] |
getCurrentSegment() |
int |
getCurrentSegmentSize() |
char[] |
getTextBuffer()
Accessor that may be used to get the contents of this buffer in a single
char array regardless of whether they were collected in a segmented
fashion or not. |
int |
getTextOffset() |
boolean |
hasTextAsCharacters()
Method that can be used to check whether textual contents can
be efficiently accessed using
getTextBuffer(). |
void |
releaseBuffers()
Method called to indicate that the underlying buffers should now
be recycled if they haven't yet been recycled.
|
void |
resetWithCopy(char[] buf,
int start,
int len) |
void |
resetWithEmpty()
Method called to clear out any content text buffer may have, and
initializes buffer to use non-shared data.
|
void |
resetWithShared(char[] buf,
int start,
int len)
Method called to initialize the buffer with a shared copy of data;
this means that buffer will just have pointers to actual data.
|
void |
resetWithString(java.lang.String value) |
private char[] |
resultArray() |
java.lang.String |
setCurrentAndReturn(int len) |
void |
setCurrentLength(int len) |
int |
size() |
java.lang.String |
toString()
Note: calling this method may not be as efficient as calling
contentsAsString(), since it's not guaranteed that resulting
String is cached. |
private void |
unshare(int needExtra)
Method called if/when we need to append content when we have been
initialized to use shared buffer.
|
static final char[] NO_CHARS
static final int MIN_SEGMENT_LEN
static final int MAX_SEGMENT_LEN
private final BufferRecycler _allocator
private char[] _inputBuffer
private int _inputStart
private int _inputLen
private java.util.ArrayList<char[]> _segments
private boolean _hasSegments
private int _segmentSize
_segmentsprivate char[] _currentSegment
private int _currentSize
private java.lang.String _resultString
private char[] _resultArray
public TextBuffer(BufferRecycler allocator)
public void releaseBuffers()
public void resetWithEmpty()
public void resetWithShared(char[] buf,
int start,
int len)
public void resetWithCopy(char[] buf,
int start,
int len)
public void resetWithString(java.lang.String value)
private char[] buf(int needed)
private void clearSegments()
public int size()
public int getTextOffset()
public boolean hasTextAsCharacters()
getTextBuffer().public char[] getTextBuffer()
char array regardless of whether they were collected in a segmented
fashion or not.public java.lang.String contentsAsString()
public char[] contentsAsArray()
public java.math.BigDecimal contentsAsDecimal()
throws java.lang.NumberFormatException
BigDecimal.java.lang.NumberFormatExceptionpublic double contentsAsDouble()
throws java.lang.NumberFormatException
java.lang.NumberFormatExceptionpublic void ensureNotShared()
public void append(char c)
public void append(char[] c,
int start,
int len)
public void append(java.lang.String str,
int offset,
int len)
public char[] getCurrentSegment()
public char[] emptyAndGetCurrentSegment()
public int getCurrentSegmentSize()
public void setCurrentLength(int len)
public java.lang.String setCurrentAndReturn(int len)
public char[] finishCurrentSegment()
public char[] expandCurrentSegment()
public char[] expandCurrentSegment(int minSize)
minSize - Required minimum strength of the current segmentpublic java.lang.String toString()
contentsAsString(), since it's not guaranteed that resulting
String is cached.toString in class java.lang.Objectprivate void unshare(int needExtra)
private void expand(int minNewSegmentSize)
private char[] resultArray()
private char[] carr(int len)