@Override
    public int getText(Writer w, boolean preserveContents)
        throws IOException, XMLStreamException
    {
        final int currToken = mCurrToken;
        if (((1 << currToken) & MASK_GET_TEXT_WITH_WRITER) == 0) {
            throwNotTextual(currToken);
        }
        /* May need to be able to do fully streaming... but only for
         * text events that have not yet been fully read; for other
         * types there's less benefit, and for fully read ones, we
         * already have everything ready.
         */
        if (!preserveContents) {
            if (currToken == CHARACTERS) {
                int count = mTextBuffer.rawContentsTo(w);
                /* Let's also clear whatever was collected (as allowed by
                 * method contract) previously, to both save memory, and
                 * to ensure caller doesn't accidentally try to access it
                 * (and get otherwise 'random' results).
                 */
                mTextBuffer.resetWithEmpty();
                if (mTokenState < TOKEN_FULL_SINGLE) {
                    count += readAndWriteText(w);
                }
                if (mCfgCoalesceText &&
                    (mTokenState < TOKEN_FULL_COALESCED)) {
                    if (mCfgCoalesceText) {
                        count += readAndWriteCoalesced(w, false);
                    }
                }
                return count;
            } else if (currToken == CDATA) {
                int count = mTextBuffer.rawContentsTo(w);
                mTextBuffer.resetWithEmpty(); // same as with CHARACTERS
                if (mTokenState < TOKEN_FULL_SINGLE) {
                    count += readAndWriteCData(w);
                }
                if (mCfgCoalesceText &&
                    (mTokenState < TOKEN_FULL_COALESCED)) {
                    if (mCfgCoalesceText) {
                        count += readAndWriteCoalesced(w, true);
                    }
                }
                return count;
            }
        }
        if (mTokenState < mStTextThreshold) {
            /* Otherwise, let's just finish the token; and due to guarantee
             * by streaming method, let's try ensure we get it all.
             */
            finishToken(false); // false -> shouldn't defer errors
        }
        if (currToken == ENTITY_REFERENCE) {
            return mCurrEntity.getReplacementText(w);
        }
        if (currToken == DTD) {
            char[] ch = getDTDInternalSubsetArray();
            if (ch != null) {
                w.write(ch);
                return ch.length;
            }
            return 0;
        }
        return mTextBuffer.rawContentsTo(w);
    }
@ Override public int getText ( Writer w , boolean preserveContents ) throws IOException , XMLStreamException { final int currToken = mCurrToken ; if ( ( ( 1 << currToken ) & MASK_GET_TEXT_WITH_WRITER ) == 0 ) { throwNotTextual ( currToken ) ; } if ( ! preserveContents ) { if ( currToken == CHARACTERS ) { int count = mTextBuffer . rawContentsTo ( w ) ; mTextBuffer . resetWithEmpty ( ) ; if ( mTokenState < TOKEN_FULL_SINGLE ) { count += readAndWriteText ( w ) ; } if ( mCfgCoalesceText && ( mTokenState < TOKEN_FULL_COALESCED ) ) { if ( mCfgCoalesceText ) { count += readAndWriteCoalesced ( w , false ) ; } } return count ; } else if ( currToken == CDATA ) { int count = mTextBuffer . rawContentsTo ( w ) ; mTextBuffer . resetWithEmpty ( ) ; if ( mTokenState < TOKEN_FULL_SINGLE ) { count += readAndWriteCData ( w ) ; } if ( mCfgCoalesceText && ( mTokenState < TOKEN_FULL_COALESCED ) ) { if ( mCfgCoalesceText ) { count += readAndWriteCoalesced ( w , true ) ; } } return count ; } } if ( mTokenState < mStTextThreshold ) { finishToken ( false ) ; } if ( currToken == ENTITY_REFERENCE ) { return mCurrEntity . getReplacementText ( w ) ; } if ( currToken == DTD ) { char [ ] ch = getDTDInternalSubsetArray ( ) ; if ( ch != null ) { w . write ( ch ) ; return ch . length ; } return 0 ; } return mTextBuffer . rawContentsTo ( w ) ; }
Method similar to {@link #getText()}, except
that it just uses provided Writer to write all textual content.
For further optimization, it may also be allowed to do true
pass-through, thus possibly avoiding one temporary copy of the
data.
<p>
TODO: try to optimize to allow completely streaming pass-through:
currently will still read all data in memory buffers before
outputting

@param w Writer to use for writing textual contents
@param preserveContents If true, reader has to preserve contents
so that further calls to <code>getText</code> will return
proper conntets. If false, reader is allowed to skip creation
of such copies: this can improve performance, but it also means
that further calls to <code>getText</code> is not guaranteed to
return meaningful data.

@return Number of characters written to the reader