public final class TextBuffer
extends java.lang.Object
StringBuilder
, with
following differences:
Over time more and more cruft has accumulated here, mostly to support efficient access to collected text. Since access is easiest to do efficiently using callbacks, this class now needs to known interfaces of SAX classes and validators.
Notes about usage: for debugging purposes, it's suggested to use
toString()
method, as opposed to
contentsAsArray()
or contentsAsString()
. Internally
resulting code paths may or may not be different, WRT caching.
Modifier and Type | Class and Description |
---|---|
private static class |
TextBuffer.BufferReader |
Modifier and Type | Field and Description |
---|---|
(package private) static int |
DEF_INITIAL_BUFFER_SIZE
Size of the first text segment buffer to allocate; need not contain
the biggest segment, since new ones will get allocated as needed.
|
(package private) static int |
INT_SPACE |
static int |
MAX_INDENT_SPACES |
static int |
MAX_INDENT_TABS |
(package private) static int |
MAX_SEGMENT_LENGTH
We will also restrict maximum length of individual segments
to allocate (not including cases where we must return a single
segment).
|
private ReaderConfig |
mConfig |
private char[] |
mCurrentSegment |
private int |
mCurrentSize
Number of characters in currently active (last) segment
|
private boolean |
mHasSegments |
private char[] |
mInputBuffer
Shared input buffer; stored here in case some input can be returned
as is, without being copied to collector's own buffers.
|
private int |
mInputLen
When using shared buffer, offset after the last character in
shared buffer
|
private int |
mInputStart
Character offset of first char in input buffer; -1 to indicate
that input buffer currently does not contain any useful char data
|
private char[] |
mResultArray |
private java.lang.String |
mResultString
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[]> |
mSegments
List of segments prior to currently active segment.
|
private int |
mSegmentSize
Amount of characters in segments in
mSegments |
private static java.lang.String |
sIndSpaces |
private static char[] |
sIndSpacesArray |
private static java.lang.String[] |
sIndSpacesStrings |
private static java.lang.String |
sIndTabs |
private static char[] |
sIndTabsArray |
private static java.lang.String[] |
sIndTabsStrings |
Modifier | Constructor and Description |
---|---|
private |
TextBuffer(ReaderConfig cfg) |
Modifier and Type | Method and Description |
---|---|
private char[] |
allocBuffer(int needed) |
void |
append(char c) |
void |
append(char[] c,
int start,
int len) |
void |
append(java.lang.String str) |
private char[] |
buildResultArray() |
private int |
calcNewSize(int latestSize)
Method used to determine size of the next segment to
allocate to contain textual content.
|
private void |
clearSegments() |
char[] |
contentsAsArray() |
java.lang.String |
contentsAsString() |
java.lang.StringBuilder |
contentsAsStringBuilder(int extraSpace)
Similar to
contentsAsString() , but constructs a StringBuilder
for further appends. |
int |
contentsToArray(int srcStart,
char[] dst,
int dstStart,
int len) |
void |
contentsToStringBuilder(java.lang.StringBuilder sb) |
static TextBuffer |
createRecyclableBuffer(ReaderConfig cfg) |
static TextBuffer |
createTemporaryBuffer() |
void |
decode(org.codehaus.stax2.typed.TypedValueDecoder tvd)
Generic pass-through method which call given decoder
with accumulated data
|
int |
decodeElements(org.codehaus.stax2.typed.TypedArrayDecoder tad,
InputProblemReporter rep)
Pass-through decode method called to find find the next token,
decode it, and repeat the process as long as there are more
tokens and the array decoder accepts more entries.
|
boolean |
endsWith(java.lang.String str)
Method that can be used to check if the contents of the buffer end
in specified String.
|
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.
|
boolean |
equalsString(java.lang.String str)
Note: it is assumed that this method is not used often enough to
be a bottleneck, or for long segments.
|
private void |
expand(int roomNeeded)
Method called when current segment is full, to allocate new
segment.
|
char[] |
finishCurrentSegment() |
void |
fireDtdCommentEvent(DTDEventListener l) |
void |
fireSaxCharacterEvents(org.xml.sax.ContentHandler h) |
void |
fireSaxCommentEvent(org.xml.sax.ext.LexicalHandler h) |
void |
fireSaxSpaceEvents(org.xml.sax.ContentHandler h) |
char[] |
getCurrentSegment() |
int |
getCurrentSegmentSize() |
char[] |
getTextBuffer() |
int |
getTextStart() |
void |
initBinaryChunks(org.codehaus.stax2.typed.Base64Variant v,
org.codehaus.stax2.ri.typed.CharArrayBase64Decoder dec,
boolean firstChunk)
Method that needs to be called to configure given base64 decoder
with textual contents collected by this buffer.
|
boolean |
isAllWhitespace() |
int |
rawContentsTo(java.io.Writer w)
Method that will stream contents of this buffer into specified
Writer.
|
java.io.Reader |
rawContentsViaReader() |
void |
recycle(boolean force)
Method called to indicate that the underlying buffers should now
be recycled if they haven't yet been recycled.
|
void |
resetInitialized()
Method called to make sure there is a non-shared segment to use, without
appending any content yet.
|
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 |
resetWithEmptyString()
Similar to
resetWithEmpty() , but actively marks current
text content to be empty string (whereas former method leaves
content as undefined). |
void |
resetWithIndentation(int indCharCount,
char indChar) |
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 |
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. |
void |
unshare(int needExtra)
Method called if/when we need to append content when we have been
initialized to use shared buffer.
|
void |
validateText(org.codehaus.stax2.validation.XMLValidator vld,
boolean lastSegment) |
static final int DEF_INITIAL_BUFFER_SIZE
static final int MAX_SEGMENT_LENGTH
static final int INT_SPACE
private final ReaderConfig mConfig
private char[] mInputBuffer
private int mInputStart
private int mInputLen
private boolean mHasSegments
private java.util.ArrayList<char[]> mSegments
private int mSegmentSize
mSegments
private char[] mCurrentSegment
private int mCurrentSize
private java.lang.String mResultString
private char[] mResultArray
public static final int MAX_INDENT_SPACES
public static final int MAX_INDENT_TABS
private static final java.lang.String sIndSpaces
private static final char[] sIndSpacesArray
private static final java.lang.String[] sIndSpacesStrings
private static final java.lang.String sIndTabs
private static final char[] sIndTabsArray
private static final java.lang.String[] sIndTabsStrings
private TextBuffer(ReaderConfig cfg)
public static TextBuffer createRecyclableBuffer(ReaderConfig cfg)
public static TextBuffer createTemporaryBuffer()
public void recycle(boolean force)
public void resetWithEmpty()
public void resetWithEmptyString()
resetWithEmpty()
, but actively marks current
text content to be empty string (whereas former method leaves
content as undefined).public void resetWithShared(char[] buf, int start, int len)
public void resetWithCopy(char[] buf, int start, int len)
public void resetInitialized()
private final char[] allocBuffer(int needed)
private final void clearSegments()
public void resetWithIndentation(int indCharCount, char indChar)
public int size()
public int getTextStart()
public char[] getTextBuffer()
public void decode(org.codehaus.stax2.typed.TypedValueDecoder tvd) throws java.lang.IllegalArgumentException
java.lang.IllegalArgumentException
public int decodeElements(org.codehaus.stax2.typed.TypedArrayDecoder tad, InputProblemReporter rep) throws org.codehaus.stax2.typed.TypedXMLStreamException
org.codehaus.stax2.typed.TypedXMLStreamException
public void initBinaryChunks(org.codehaus.stax2.typed.Base64Variant v, org.codehaus.stax2.ri.typed.CharArrayBase64Decoder dec, boolean firstChunk)
dec
- Decoder that will need datafirstChunk
- Whether this is the first segment fed or not;
if it is, state needs to be fullt reset; if not, only partially.public java.lang.String contentsAsString()
public java.lang.StringBuilder contentsAsStringBuilder(int extraSpace)
contentsAsString()
, but constructs a StringBuilder
for further appends.extraSpace
- Number of extra characters to preserve in StringBuilder
beyond space immediately needed to hold the contentspublic void contentsToStringBuilder(java.lang.StringBuilder sb)
public char[] contentsAsArray()
public int contentsToArray(int srcStart, char[] dst, int dstStart, int len)
public int rawContentsTo(java.io.Writer w) throws java.io.IOException
java.io.IOException
public java.io.Reader rawContentsViaReader() throws java.io.IOException
java.io.IOException
public boolean isAllWhitespace()
public boolean endsWith(java.lang.String str)
public boolean equalsString(java.lang.String str)
public void fireSaxCharacterEvents(org.xml.sax.ContentHandler h) throws org.xml.sax.SAXException
org.xml.sax.SAXException
public void fireSaxSpaceEvents(org.xml.sax.ContentHandler h) throws org.xml.sax.SAXException
org.xml.sax.SAXException
public void fireSaxCommentEvent(org.xml.sax.ext.LexicalHandler h) throws org.xml.sax.SAXException
org.xml.sax.SAXException
public void fireDtdCommentEvent(DTDEventListener l)
public void validateText(org.codehaus.stax2.validation.XMLValidator vld, boolean lastSegment) throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException
public void ensureNotShared()
public void append(char c)
public void append(char[] c, int start, int len)
public void append(java.lang.String str)
public char[] getCurrentSegment()
public int getCurrentSegmentSize()
public void setCurrentLength(int len)
public char[] finishCurrentSegment()
private int calcNewSize(int latestSize)
public java.lang.String toString()
contentsAsString()
, since it's not guaranteed that resulting
String is cached.toString
in class java.lang.Object
public void unshare(int needExtra)
private void expand(int roomNeeded)
roomNeeded
- Number of characters that the resulting
new buffer must haveprivate char[] buildResultArray()