Package com.fasterxml.aalto.util
Class TextBuilder
- java.lang.Object
-
- com.fasterxml.aalto.util.TextBuilder
-
public final class TextBuilder extends java.lang.Object
Class conceptually similar toStringBuilder
, but that allows for bit more efficient building, using segmented internal buffers, and direct access to these buffers.
-
-
Field Summary
Fields Modifier and Type Field Description private ReaderConfig
_config
private char[]
_currentSegment
private int
_currentSize
Number of characters in currently active (last) segmentprivate char[]
_decodeBuffer
private int
_decodeEnd
private int
_decodePtr
private boolean
_isIndentation
Marker to know if the contents currently stored were created using "indentation detection".private char[]
_resultArray
private int
_resultLen
Indicator for length of data with_resultArray
, iff the primary indicator (_currentSize) is invalid (-1).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
DEF_INITIAL_BUFFER_SIZE
Size of the first text segment buffer to allocate.(package private) static int
INT_SPACE
static int
MAX_INDENT_SPACES
static int
MAX_INDENT_TABS
(package private) static int
MAX_SEGMENT_LENGTH
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
(package private) static char[]
sNoChars
-
Constructor Summary
Constructors Modifier Constructor Description private
TextBuilder(ReaderConfig cfg)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private char[]
allocBuffer(int minNeeded)
void
append(char c)
void
append(char[] c, int start, int len)
void
append(java.lang.String str)
void
appendSurrogate(int surr)
private char[]
buildResultArray()
private int
calcNewSize(int latestSize)
char[]
contentsAsArray()
java.lang.String
contentsAsString()
int
contentsToArray(int srcStart, char[] dst, int dstStart, int len)
static TextBuilder
createRecyclableBuffer(ReaderConfig cfg)
int
decodeElements(org.codehaus.stax2.typed.TypedArrayDecoder tad, boolean reset)
Method called by the stream reader to decode space-separated tokens that are part of the current text event (contents of which are stored within this buffer), using given decoder.boolean
endsWith(java.lang.String str)
Method that can be used to check if the contents of the buffer end in specified String.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
fireSaxCharacterEvents(org.xml.sax.ContentHandler h)
This is a specialized "accessor" method, which is basically to fire SAX characters() events in an optimal way, based on which internal buffers are being usedvoid
fireSaxCommentEvent(org.xml.sax.ext.LexicalHandler h)
void
fireSaxSpaceEvents(org.xml.sax.ContentHandler h)
char[]
getBufferWithoutReset()
int
getCurrentLength()
char[]
getTextBuffer()
boolean
isAllWhitespace()
int
rawContentsTo(java.io.Writer w)
Method that will stream contents of this buffer into specified Writer.void
recycle(boolean force)
Method called to indicate that the underlying buffers should now be recycled if they haven't yet been recycled.void
resetForBinaryDecode(org.codehaus.stax2.typed.Base64Variant v, org.codehaus.stax2.ri.typed.CharArrayBase64Decoder dec, boolean firstChunk)
Method called to initialize given base64 decoder with data contained in this text buffer (for the current event).private void
resetForDecode()
void
resetWithChar(char c)
Method called to initialize the buffer with just a single charchar[]
resetWithEmpty()
Method called to clear out any content text buffer may have, and initializes and returns the first segment to add characters to.void
resetWithIndentation(int indCharCount, char indChar)
void
resetWithSurrogate(int c)
void
setCurrentLength(int len)
int
size()
java.lang.String
toString()
Note: calling this method may not be as efficient as callingcontentsAsString()
, since it is guaranteed that resulting String is NOT cached (to ensure we see no stale data)
-
-
-
Field Detail
-
sNoChars
static final char[] sNoChars
-
DEF_INITIAL_BUFFER_SIZE
static final 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. However, it's sensible to use something that often is big enough to contain typical segments.- See Also:
- Constant Field Values
-
MAX_SEGMENT_LENGTH
static final int MAX_SEGMENT_LENGTH
- See Also:
- Constant Field Values
-
INT_SPACE
static final int INT_SPACE
- See Also:
- Constant Field Values
-
_config
private final ReaderConfig _config
-
_segments
private java.util.ArrayList<char[]> _segments
List of segments prior to currently active segment.
-
_segmentSize
private int _segmentSize
Amount of characters in segments in_segments
-
_currentSegment
private char[] _currentSegment
-
_currentSize
private int _currentSize
Number of characters in currently active (last) segment
-
_resultString
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.
-
_resultArray
private char[] _resultArray
-
_resultLen
private int _resultLen
Indicator for length of data with_resultArray
, iff the primary indicator (_currentSize) is invalid (-1).
-
_decodeBuffer
private char[] _decodeBuffer
-
_decodePtr
private int _decodePtr
-
_decodeEnd
private int _decodeEnd
-
_isIndentation
private boolean _isIndentation
Marker to know if the contents currently stored were created using "indentation detection". If so, it's known to be all white space
-
MAX_INDENT_SPACES
public static final int MAX_INDENT_SPACES
- See Also:
- Constant Field Values
-
MAX_INDENT_TABS
public static final int MAX_INDENT_TABS
- See Also:
- Constant Field Values
-
sIndSpaces
private static final java.lang.String sIndSpaces
- See Also:
- Constant Field Values
-
sIndSpacesArray
private static final char[] sIndSpacesArray
-
sIndSpacesStrings
private static final java.lang.String[] sIndSpacesStrings
-
sIndTabs
private static final java.lang.String sIndTabs
- See Also:
- Constant Field Values
-
sIndTabsArray
private static final char[] sIndTabsArray
-
sIndTabsStrings
private static final java.lang.String[] sIndTabsStrings
-
-
Constructor Detail
-
TextBuilder
private TextBuilder(ReaderConfig cfg)
-
-
Method Detail
-
createRecyclableBuffer
public static TextBuilder createRecyclableBuffer(ReaderConfig cfg)
-
recycle
public void recycle(boolean force)
Method called to indicate that the underlying buffers should now be recycled if they haven't yet been recycled. Although caller can still use this text buffer, it is not advisable to call this method if that is likely, since next time a buffer is needed, buffers need to reallocated. Note: calling this method automatically also clears contents of the buffer.
-
resetWithEmpty
public char[] resetWithEmpty()
Method called to clear out any content text buffer may have, and initializes and returns the first segment to add characters to.
-
resetWithIndentation
public void resetWithIndentation(int indCharCount, char indChar)
-
resetWithChar
public void resetWithChar(char c)
Method called to initialize the buffer with just a single char
-
resetWithSurrogate
public void resetWithSurrogate(int c)
-
getBufferWithoutReset
public char[] getBufferWithoutReset()
-
size
public int size()
- Returns:
- Number of characters currently stored by this collector
-
getTextBuffer
public char[] getTextBuffer()
-
contentsAsString
public java.lang.String contentsAsString()
-
contentsAsArray
public char[] contentsAsArray()
-
contentsToArray
public int contentsToArray(int srcStart, char[] dst, int dstStart, int len)
-
rawContentsTo
public int rawContentsTo(java.io.Writer w) throws java.io.IOException
Method that will stream contents of this buffer into specified Writer.- Throws:
java.io.IOException
-
isAllWhitespace
public boolean isAllWhitespace()
-
endsWith
public boolean endsWith(java.lang.String str)
Method that can be used to check if the contents of the buffer end in specified String.- Returns:
- True if the textual content buffer contains ends with the specified String; false otherwise
-
equalsString
public 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. Based on this, it is optimized for common simple cases where there is only one single character segment to use; fallback for other cases is to create such segment.
-
fireSaxCharacterEvents
public void fireSaxCharacterEvents(org.xml.sax.ContentHandler h) throws org.xml.sax.SAXException
This is a specialized "accessor" method, which is basically to fire SAX characters() events in an optimal way, based on which internal buffers are being used- Throws:
org.xml.sax.SAXException
-
fireSaxSpaceEvents
public void fireSaxSpaceEvents(org.xml.sax.ContentHandler h) throws org.xml.sax.SAXException
- Throws:
org.xml.sax.SAXException
-
fireSaxCommentEvent
public void fireSaxCommentEvent(org.xml.sax.ext.LexicalHandler h) throws org.xml.sax.SAXException
- Throws:
org.xml.sax.SAXException
-
append
public void append(char c)
-
appendSurrogate
public void appendSurrogate(int surr)
-
append
public void append(char[] c, int start, int len)
-
append
public void append(java.lang.String str)
-
getCurrentLength
public int getCurrentLength()
-
setCurrentLength
public void setCurrentLength(int len)
-
finishCurrentSegment
public char[] finishCurrentSegment()
-
calcNewSize
private int calcNewSize(int latestSize)
-
decodeElements
public int decodeElements(org.codehaus.stax2.typed.TypedArrayDecoder tad, boolean reset) throws org.codehaus.stax2.typed.TypedXMLStreamException
Method called by the stream reader to decode space-separated tokens that are part of the current text event (contents of which are stored within this buffer), using given decoder.- Throws:
org.codehaus.stax2.typed.TypedXMLStreamException
-
resetForBinaryDecode
public void resetForBinaryDecode(org.codehaus.stax2.typed.Base64Variant v, org.codehaus.stax2.ri.typed.CharArrayBase64Decoder dec, boolean firstChunk)
Method called to initialize given base64 decoder with data contained in this text buffer (for the current event).
-
resetForDecode
private final void resetForDecode()
-
toString
public java.lang.String toString()
Note: calling this method may not be as efficient as callingcontentsAsString()
, since it is guaranteed that resulting String is NOT cached (to ensure we see no stale data)- Overrides:
toString
in classjava.lang.Object
-
allocBuffer
private final char[] allocBuffer(int minNeeded)
-
expand
private void expand(int roomNeeded)
Method called when current segment is full, to allocate new segment.
-
buildResultArray
private char[] buildResultArray()
-
-