Top Description Inners Fields Constructors Methods
com.ctc.wstx.util

public final Class TextBuffer

extends Object
Class Inheritance
Imports
java.io.*, java.util.ArrayList, javax.xml.stream.Location, .XMLStreamException, org.xml.sax.ContentHandler, .SAXException, org.xml.sax.ext.LexicalHandler, org.codehaus.stax2.typed.Base64Variant, .TypedArrayDecoder, .TypedValueDecoder, .TypedXMLStreamException, org.codehaus.stax2.validation.XMLValidator, org.codehaus.stax2.ri.typed.CharArrayBase64Decoder, com.ctc.wstx.api.ReaderConfig, com.ctc.wstx.dtd.DTDEventListener, com.ctc.wstx.sr.InputProblemReporter

TextBuffer is a class similar to 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.

Author
Tatu Saloranta

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
pack-priv 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.

pack-priv static final int
public static final int
public static final int
pack-priv static final 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 final ReaderConfig
private char[]
private int
mCurrentSize

Number of characters in currently active (last) segment

private boolean
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[]
private String
mResultString

String that will be constructed when the whole contents are needed; will be temporarily stored in case asked for again.

private ArrayList<char[]>
mSegments

List of segments prior to currently active segment.

private int
mSegmentSize

Amount of characters in segments in mSegments

private static final String
private static final char[]
private static final String[]
private static final String
private static final char[]
private static final String[]

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private final char[]
allocBuffer(int needed)

public void
append(char c)

public void
append(char[] c, int start, int len)

public void
private char[]
private int
calcNewSize(int latestSize)

Method used to determine size of the next segment to allocate to contain textual content.

private final void
public char[]
public String
public StringBuilder
contentsAsStringBuilder(int
Number of extra characters to preserve in StringBuilder beyond space immediately needed to hold the contents
extraSpace
)

Similar to contentsAsString, but constructs a StringBuilder for further appends.

public int
contentsToArray(int srcStart, char[] dst, int dstStart, int len)

public void
public static TextBuffer
public static TextBuffer
public void
decode(TypedValueDecoder tvd)

Generic pass-through method which call given decoder with accumulated data

public int

Returns:

Number of tokens decoded; 0 means that no (more) tokens were found from this buffer.
decodeElements
(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.

public 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.

public boolean
equalsString(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
Number of characters that the resulting new buffer must have
roomNeeded
)

Method called when current segment is full, to allocate new segment.

public char[]
public void
public void
public void
public void
public char[]
public int
public char[]
public int
public void
initBinaryChunks(Base64Variant v, CharArrayBase64Decoder
Decoder that will need data
dec
,
boolean
Whether this is the first segment fed or not; if it is, state needs to be fullt reset; if not, only partially.
firstChunk
)

Method that needs to be called to configure given base64 decoder with textual contents collected by this buffer.

public boolean
public int
rawContentsTo(Writer w)

Method that will stream contents of this buffer into specified Writer.

public Reader
rawContentsViaReader()
Deprecated

public void
recycle(boolean force)

Method called to indicate that the underlying buffers should now be recycled if they haven't yet been recycled.

public void
resetInitialized()

Method called to make sure there is a non-shared segment to use, without appending any content yet.

public void
resetWithCopy(char[] buf, int start, int len)

public void
resetWithEmpty()

Method called to clear out any content text buffer may have, and initializes buffer to use non-shared data.

public void
resetWithEmptyString()

Similar to resetWithEmpty, but actively marks current text content to be empty string (whereas former method leaves content as undefined).

public void
resetWithIndentation(int indCharCount, char indChar)

public 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.

public void
public int

Returns:

Number of characters currently stored by this collector
size
()

public String
toString()

Overrides java.lang.Object.toString.

Note

calling this method may not be as efficient as calling contentsAsString, since it's not guaranteed that resulting String is cached.

public void
unshare(int needExtra)

Method called if/when we need to append content when we have been initialized to use shared buffer.

public void
validateText(XMLValidator vld, boolean lastSegment)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

DEF_INITIAL_BUFFER_SIZEback to summary
pack-priv 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 segments.

INT_SPACEback to summary
pack-priv static final int INT_SPACE
MAX_INDENT_SPACESback to summary
public static final int MAX_INDENT_SPACES
MAX_INDENT_TABSback to summary
public static final int MAX_INDENT_TABS
MAX_SEGMENT_LENGTHback to summary
pack-priv static final 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). Value is somewhat arbitrary, let's use it so that memory used is no more than 1/2 megabytes.

mConfigback to summary
private final ReaderConfig mConfig
mCurrentSegmentback to summary
private char[] mCurrentSegment
mCurrentSizeback to summary
private int mCurrentSize

Number of characters in currently active (last) segment

mHasSegmentsback to summary
private boolean mHasSegments
mInputBufferback to summary
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. Note that this is read-only for this Objet.

mInputLenback to summary
private int mInputLen

When using shared buffer, offset after the last character in shared buffer

mInputStartback to summary
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

mResultArrayback to summary
private char[] mResultArray
mResultStringback to summary
private String mResultString

String that will be constructed when the whole contents are needed; will be temporarily stored in case asked for again.

mSegmentsback to summary
private ArrayList<char[]> mSegments

List of segments prior to currently active segment.

mSegmentSizeback to summary
private int mSegmentSize

Amount of characters in segments in mSegments

sIndSpacesback to summary
private static final String sIndSpaces
sIndSpacesArrayback to summary
private static final char[] sIndSpacesArray
sIndSpacesStringsback to summary
private static final String[] sIndSpacesStrings
sIndTabsback to summary
private static final String sIndTabs
sIndTabsArrayback to summary
private static final char[] sIndTabsArray
sIndTabsStringsback to summary
private static final String[] sIndTabsStrings

Constructor Detail

TextBufferback to summary
private TextBuffer(ReaderConfig cfg)

Method Detail

allocBufferback to summary
private final char[] allocBuffer(int needed)
appendback to summary
public void append(char c)
appendback to summary
public void append(char[] c, int start, int len)
appendback to summary
public void append(String str)
buildResultArrayback to summary
private char[] buildResultArray()
calcNewSizeback to summary
private int calcNewSize(int latestSize)

Method used to determine size of the next segment to allocate to contain textual content.

clearSegmentsback to summary
private final void clearSegments()
contentsAsArrayback to summary
public char[] contentsAsArray()
contentsAsStringback to summary
public String contentsAsString()
contentsAsStringBuilderback to summary
public StringBuilder contentsAsStringBuilder(int extraSpace)

Similar to contentsAsString, but constructs a StringBuilder for further appends.

Parameters
extraSpace:int

Number of extra characters to preserve in StringBuilder beyond space immediately needed to hold the contents

contentsToArrayback to summary
public int contentsToArray(int srcStart, char[] dst, int dstStart, int len)
contentsToStringBuilderback to summary
public void contentsToStringBuilder(StringBuilder sb)
createRecyclableBufferback to summary
public static TextBuffer createRecyclableBuffer(ReaderConfig cfg)
createTemporaryBufferback to summary
public static TextBuffer createTemporaryBuffer()
decodeback to summary
public void decode(TypedValueDecoder tvd) throws IllegalArgumentException

Generic pass-through method which call given decoder with accumulated data

decodeElementsback to summary
public int decodeElements(TypedArrayDecoder tad, InputProblemReporter rep) throws TypedXMLStreamException

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. All tokens processed will be "consumed", such that they will not be visible via buffer.

Returns:int

Number of tokens decoded; 0 means that no (more) tokens were found from this buffer.

ensureNotSharedback to summary
public 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.

equalsStringback to summary
public boolean equalsString(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.

expandback to summary
private void expand(int roomNeeded)

Method called when current segment is full, to allocate new segment.

Parameters
roomNeeded:int

Number of characters that the resulting new buffer must have

finishCurrentSegmentback to summary
public char[] finishCurrentSegment()
fireDtdCommentEventback to summary
public void fireDtdCommentEvent(DTDEventListener l)
fireSaxCharacterEventsback to summary
public void fireSaxCharacterEvents(ContentHandler h) throws SAXException
fireSaxCommentEventback to summary
public void fireSaxCommentEvent(LexicalHandler h) throws SAXException
fireSaxSpaceEventsback to summary
public void fireSaxSpaceEvents(ContentHandler h) throws SAXException
getCurrentSegmentback to summary
public char[] getCurrentSegment()
getCurrentSegmentSizeback to summary
public int getCurrentSegmentSize()
getTextBufferback to summary
public char[] getTextBuffer()
getTextStartback to summary
public int getTextStart()
initBinaryChunksback to summary
public void initBinaryChunks(Base64Variant v, CharArrayBase64Decoder dec, boolean firstChunk)

Method that needs to be called to configure given base64 decoder with textual contents collected by this buffer.

Parameters
dec:CharArrayBase64Decoder

Decoder that will need data

firstChunk:boolean

Whether this is the first segment fed or not; if it is, state needs to be fullt reset; if not, only partially.

isAllWhitespaceback to summary
public boolean isAllWhitespace()
rawContentsToback to summary
public int rawContentsTo(Writer w) throws IOException

Method that will stream contents of this buffer into specified Writer.

rawContentsViaReaderback to summary
public Reader rawContentsViaReader() throws IOException

Deprecated

Annotations
@Deprecated
recycleback to summary
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.

resetInitializedback to summary
public void resetInitialized()

Method called to make sure there is a non-shared segment to use, without appending any content yet.

resetWithCopyback to summary
public void resetWithCopy(char[] buf, int start, int len)
resetWithEmptyback to summary
public void resetWithEmpty()

Method called to clear out any content text buffer may have, and initializes buffer to use non-shared data.

resetWithEmptyStringback to summary
public void resetWithEmptyString()

Similar to resetWithEmpty, but actively marks current text content to be empty string (whereas former method leaves content as undefined).

resetWithIndentationback to summary
public void resetWithIndentation(int indCharCount, char indChar)
resetWithSharedback to summary
public 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. It also means that if anything is to be appended to the buffer, it will first have to unshare it (make a local copy).

setCurrentLengthback to summary
public void setCurrentLength(int len)
sizeback to summary
public int size()
Returns:int

Number of characters currently stored by this collector

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Note

calling this method may not be as efficient as calling contentsAsString, since it's not guaranteed that resulting String is cached.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object

Annotations
@Override
unshareback to summary
public void unshare(int needExtra)

Method called if/when we need to append content when we have been initialized to use shared buffer.

validateTextback to summary
public void validateText(XMLValidator vld, boolean lastSegment) throws XMLStreamException
com.ctc.wstx.util back to summary

private final Class TextBuffer.BufferReader

extends Reader
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final int
pack-priv int
pack-priv char[]
pack-priv int
pack-priv int
pack-priv ArrayList<char[]>
Inherited from java.io.Reader:
lock

Constructor Summary

AccessConstructor and Description
public
BufferReader(ArrayList<char[]> segs, char[] currSeg, int currSegLen)

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Closes the stream and releases any system resources associated with it.

public void
mark(int
Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.
x
)

Overrides java.io.Reader.mark.

Marks the present position in the stream.

public boolean
markSupported()

Overrides java.io.Reader.markSupported.

Tells whether this stream supports the mark() operation.

public int
read(char[]
Destination buffer
cbuf
,
int
Offset at which to start storing characters
offset
,
int
Maximum number of characters to read
len
)

Implements abstract java.io.Reader.read.

Reads characters into a portion of an array.

public boolean
ready()

Overrides java.io.Reader.ready.

Tells whether this stream is ready to be read.

public void
reset()

Overrides java.io.Reader.reset.

Resets the stream.

public long
skip(long
The number of characters to skip
amount
)

Overrides java.io.Reader.skip.

Skips characters.

Inherited from java.io.Reader:
nullReaderreadreadreadtransferTo

Field Detail

_currentLengthback to summary
pack-priv final int _currentLength
_currentOffsetback to summary
pack-priv int _currentOffset
_currentSegmentback to summary
pack-priv char[] _currentSegment
_segmentIndexback to summary
pack-priv int _segmentIndex
_segmentOffsetback to summary
pack-priv int _segmentOffset
_segmentsback to summary
pack-priv ArrayList<char[]> _segments

Constructor Detail

BufferReaderback to summary
public BufferReader(ArrayList<char[]> segs, char[] currSeg, int currSegLen)

Method Detail

closeback to summary
public void close()

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Doc from java.io.Reader.close.

Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

Annotations
@Override
markback to summary
public void mark(int x) throws IOException

Overrides java.io.Reader.mark.

Doc from java.io.Reader.mark.

Marks the present position in the stream. Subsequent calls to reset() will attempt to reposition the stream to this point. Not all character-input streams support the mark() operation.

Parameters
x:int

Limit on the number of characters that may be read while still preserving the mark. After reading this many characters, attempting to reset the stream may fail.

Annotations
@Override
Exceptions
IOException:
If the stream does not support mark(), or if some other I/O error occurs
markSupportedback to summary
public boolean markSupported()

Overrides java.io.Reader.markSupported.

Doc from java.io.Reader.markSupported.

Tells whether this stream supports the mark() operation. The default implementation always returns false. Subclasses should override this method.

Returns:boolean

true if and only if this stream supports the mark operation.

Annotations
@Override
readback to summary
public int read(char[] cbuf, int offset, int len)

Implements abstract java.io.Reader.read.

Doc from java.io.Reader.read.

Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

If len is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf.

Parameters
cbuf:char[]

Destination buffer

offset:int

Offset at which to start storing characters

len:int

Maximum number of characters to read

Returns:int

The number of characters read, or -1 if the end of the stream has been reached

Annotations
@Override
readyback to summary
public boolean ready()

Overrides java.io.Reader.ready.

Doc from java.io.Reader.ready.

Tells whether this stream is ready to be read.

Returns:boolean

True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.

Annotations
@Override
resetback to summary
public void reset() throws IOException

Overrides java.io.Reader.reset.

Doc from java.io.Reader.reset.

Resets the stream. If the stream has been marked, then attempt to reposition it at the mark. If the stream has not been marked, then attempt to reset it in some way appropriate to the particular stream, for example by repositioning it to its starting point. Not all character-input streams support the reset() operation, and some support reset() without supporting mark().

Annotations
@Override
Exceptions
IOException:
If the stream has not been marked, or if the mark has been invalidated, or if the stream does not support reset(), or if some other I/O error occurs
skipback to summary
public long skip(long amount)

Overrides java.io.Reader.skip.

Doc from java.io.Reader.skip.

Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached. If the stream is already at its end before this method is invoked, then no characters are skipped and zero is returned.

Parameters
amount:long

The number of characters to skip

Returns:long

The number of characters actually skipped

Annotations
@Override