Class BufferingXmlWriter
- java.lang.Object
-
- com.ctc.wstx.sw.XmlWriter
-
- com.ctc.wstx.sw.BufferingXmlWriter
-
- All Implemented Interfaces:
javax.xml.stream.XMLStreamConstants
public final class BufferingXmlWriter extends XmlWriter implements javax.xml.stream.XMLStreamConstants
Concrete implementation ofXmlWriterthat will dispatch writes to another writer (of typeWriter, and will NOT handle encoding. It will, however, do basic buffering such that the underlying Writer need (and thus, should) not do buffering.One design goal for this class is to avoid unnecessary buffering: since there will be another Writer doing the actual encoding, amount of buffering needed should still be limited. To this end, a threshold is used to define what's the threshold of writes that we do want to coalesce, ie. buffer. Writes bigger than this should in general proceed without buffering.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static intDEFAULT_BUFFER_SIZELet's use a typical default to have a compromise between large enough chunks to output, and minimizing memory overhead.(package private) static intDEFAULT_SMALL_SIZEChoosing threshold for 'small size' is a compromise between excessive buffering (high small size), and too many fragmented calls to the underlying writer (low small size).protected static intHIGHEST_ENCODABLE_ATTR_CHARHighest valued character that may need to be encoded (minus charset encoding requirements) when writing attribute values.protected static intHIGHEST_ENCODABLE_TEXT_CHARHighest valued character that may need to be encoded (minus charset encoding requirements) when writing attribute values.private intmEncHighCharFirst Unicode character (one with lowest value) after (and including) which character entities have to be used.(package private) charmEncQuoteCharCharacter that is considered to be the enclosing quote character; for XML either single or double quote.(package private) java.lang.StringmEncQuoteEntityEntity String to use for escaping the quote character.protected java.io.WritermOutActual Writer to use for outputting buffered data as appropriate.protected char[]mOutputBufferprotected intmOutputBufLenprotected intmOutputPtrprotected intmSmallWriteSizeThis is the threshold used to check what is considered a "small" write; small writes will be buffered until resulting size will be above the threshold.protected java.io.OutputStreammUnderlyingStreamActual physical stream that the writer is using, if known.protected static int[]QUOTABLE_TEXT_CHARS-
Fields inherited from class com.ctc.wstx.sw.XmlWriter
DEFAULT_QUOTE_CHAR, mAddSpaceAfterEmptyElem, mAttrValueWriter, mAutoCloseOutput, mCheckContent, mCheckNames, mCheckStructure, mConfig, mEncoding, mEscapeCR, mFixContent, mLocPastChars, mLocRowNr, mLocRowStartOffset, mNsAware, mRawWrapper, mTextWrapper, mTextWriter, mUseDoubleQuotesInXmlDecl, mXml11, SURR1_FIRST, SURR1_LAST, SURR2_FIRST, SURR2_LAST
-
-
Constructor Summary
Constructors Constructor Description BufferingXmlWriter(java.io.Writer out, WriterConfig cfg, java.lang.String enc, boolean autoclose, java.io.OutputStream outs, int bitsize)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose(boolean forceRealClose)Method called to flush the buffer(s), and close the output sink (stream or writer) if enabled (auto-closing) or forced.private voidfastWriteRaw(char c)private voidfastWriteRaw(char c1, char c2)private voidfastWriteRaw(java.lang.String str)voidflush()private voidflushBuffer()protected intgetOutputPtr()protected java.io.OutputStreamgetOutputStream()protected java.io.WritergetWriter()static intguessEncodingBitSize(java.lang.String enc)Method used to figure out which part of the Unicode char set the encoding can natively support.protected intverifyCDataContent(char[] c, int start, int end)protected intverifyCDataContent(java.lang.String content)protected intverifyCommentContent(java.lang.String content)protected voidwriteAsEntity(int c)voidwriteAttribute(java.lang.String localName, char[] value, int offset, int vlen)voidwriteAttribute(java.lang.String localName, java.lang.String value)Note: can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)voidwriteAttribute(java.lang.String prefix, java.lang.String localName, char[] value, int offset, int vlen)voidwriteAttribute(java.lang.String prefix, java.lang.String localName, java.lang.String value)Note: can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)private voidwriteAttrValue(char[] value, int offset, int len)private voidwriteAttrValue(java.lang.String value, int len)intwriteCData(char[] cbuf, int offset, int len)intwriteCData(java.lang.String data)voidwriteCDataEnd()voidwriteCDataStart()voidwriteCharacters(char[] cbuf, int offset, int len)voidwriteCharacters(java.lang.String text)intwriteComment(java.lang.String data)Method that will try to output the content as specified.voidwriteCommentEnd()voidwriteCommentStart()voidwriteDTD(java.lang.String data)Older "legacy" output method for outputting DOCTYPE declaration.voidwriteDTD(java.lang.String rootName, java.lang.String systemId, java.lang.String publicId, java.lang.String internalSubset)voidwriteEndTag(java.lang.String localName)voidwriteEndTag(java.lang.String prefix, java.lang.String localName)voidwriteEntityReference(java.lang.String name)intwritePI(java.lang.String target, java.lang.String data)voidwritePIEnd()voidwritePIStart(java.lang.String target, boolean addSpace)protected voidwritePrefixedName(java.lang.String prefix, java.lang.String localName)voidwriteRaw(char[] cbuf, int offset, int len)voidwriteRaw(java.lang.String str)voidwriteRaw(java.lang.String str, int offset, int len)voidwriteRawAscii(char[] cbuf, int offset, int len)Method called to output typed values (int, long, double, float etc) that are known not to contain any escapable characters, or anything else beyond 7-bit ascii range.protected voidwriteSegmentedCData(char[] c, int start, int len, int index)protected voidwriteSegmentedCData(java.lang.String content, int index)protected voidwriteSegmentedComment(java.lang.String content, int index)voidwriteStartTagEmptyEnd()voidwriteStartTagEnd()voidwriteStartTagStart(java.lang.String localName)Note: can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)voidwriteStartTagStart(java.lang.String prefix, java.lang.String localName)Note: can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)voidwriteTypedAttribute(java.lang.String prefix, java.lang.String localName, java.lang.String nsURI, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer)Method similar toXmlWriter.writeAttribute(String,String,char[],int,int)but where is known not to require escaping.voidwriteTypedAttribute(java.lang.String prefix, java.lang.String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc)Method similar toXmlWriter.writeAttribute(String,String,char[],int,int)but where is known not to require escaping.voidwriteTypedAttribute(java.lang.String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc)Method similar toXmlWriter.writeAttribute(String,String,char[],int,int)but where is known not to require escaping.voidwriteTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc)LikeXmlWriter.writeRaw(java.lang.String, int, int), but caller guarantees that the contents additionally are known to be in 7-bit ascii range, and also passes an encoder object that will encode values only when being handed a buffer to append to.voidwriteTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer)LikeXmlWriter.writeRaw(java.lang.String, int, int), but caller guarantees that the contents additionally are known to be in 7-bit ascii range, and also passes an encoder object that will encode values only when being handed a buffer to append to.voidwriteXmlDeclaration(java.lang.String version, java.lang.String encoding, java.lang.String standalone)-
Methods inherited from class com.ctc.wstx.sw.XmlWriter
enableXml11, getAbsOffset, getColumn, getRow, handleInvalidChar, reportNwfContent, reportNwfName, throwOutputError, throwOutputError, verifyNameValidity, wrapAsRawWriter, wrapAsTextWriter
-
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
static final int DEFAULT_BUFFER_SIZE
Let's use a typical default to have a compromise between large enough chunks to output, and minimizing memory overhead. Compared to encoding writers, buffer size can be bit smaller since there's one more level of processing (at encoding), which may use bigger buffering.- See Also:
- Constant Field Values
-
DEFAULT_SMALL_SIZE
static final int DEFAULT_SMALL_SIZE
Choosing threshold for 'small size' is a compromise between excessive buffering (high small size), and too many fragmented calls to the underlying writer (low small size). Let's just use about 1/4 of the full buffer size.- See Also:
- Constant Field Values
-
HIGHEST_ENCODABLE_ATTR_CHAR
protected static final int HIGHEST_ENCODABLE_ATTR_CHAR
Highest valued character that may need to be encoded (minus charset encoding requirements) when writing attribute values.- See Also:
- Constant Field Values
-
HIGHEST_ENCODABLE_TEXT_CHAR
protected static final int HIGHEST_ENCODABLE_TEXT_CHAR
Highest valued character that may need to be encoded (minus charset encoding requirements) when writing attribute values.- See Also:
- Constant Field Values
-
QUOTABLE_TEXT_CHARS
protected static final int[] QUOTABLE_TEXT_CHARS
-
mOut
protected final java.io.Writer mOut
Actual Writer to use for outputting buffered data as appropriate.
-
mOutputBuffer
protected char[] mOutputBuffer
-
mSmallWriteSize
protected final int mSmallWriteSize
This is the threshold used to check what is considered a "small" write; small writes will be buffered until resulting size will be above the threshold.
-
mOutputPtr
protected int mOutputPtr
-
mOutputBufLen
protected int mOutputBufLen
-
mUnderlyingStream
protected final java.io.OutputStream mUnderlyingStream
Actual physical stream that the writer is using, if known. Not used for actual output, only needed so that calling application may (try to) figure out the original source.
-
mEncHighChar
private final int mEncHighChar
First Unicode character (one with lowest value) after (and including) which character entities have to be used. For
-
mEncQuoteChar
final char mEncQuoteChar
Character that is considered to be the enclosing quote character; for XML either single or double quote.
-
mEncQuoteEntity
final java.lang.String mEncQuoteEntity
Entity String to use for escaping the quote character.
-
-
Constructor Detail
-
BufferingXmlWriter
public BufferingXmlWriter(java.io.Writer out, WriterConfig cfg, java.lang.String enc, boolean autoclose, java.io.OutputStream outs, int bitsize) throws java.io.IOException- Parameters:
outs- Underlying OutputStream that the writer (out) is using, if known. Needed to support (optional) access to the underlying stream- Throws:
java.io.IOException
-
-
Method Detail
-
getOutputPtr
protected int getOutputPtr()
- Specified by:
getOutputPtrin classXmlWriter
-
getOutputStream
protected final java.io.OutputStream getOutputStream()
- Specified by:
getOutputStreamin classXmlWriter- Returns:
- Underlying OutputStream used for physical output, if the writer was constructed using one
-
getWriter
protected final java.io.Writer getWriter()
-
close
public void close(boolean forceRealClose) throws java.io.IOExceptionDescription copied from class:XmlWriterMethod called to flush the buffer(s), and close the output sink (stream or writer) if enabled (auto-closing) or forced.
-
flush
public final void flush() throws java.io.IOException
-
writeRaw
public void writeRaw(char[] cbuf, int offset, int len) throws java.io.IOException
-
writeRawAscii
public final void writeRawAscii(char[] cbuf, int offset, int len) throws java.io.IOExceptionMethod called to output typed values (int, long, double, float etc) that are known not to contain any escapable characters, or anything else beyond 7-bit ascii range.- Specified by:
writeRawAsciiin classXmlWriter- Throws:
java.io.IOException
-
writeRaw
public void writeRaw(java.lang.String str) throws java.io.IOException
-
writeRaw
public void writeRaw(java.lang.String str, int offset, int len) throws java.io.IOException
-
writeCDataStart
public final void writeCDataStart() throws java.io.IOException- Specified by:
writeCDataStartin classXmlWriter- Throws:
java.io.IOException
-
writeCDataEnd
public final void writeCDataEnd() throws java.io.IOException- Specified by:
writeCDataEndin classXmlWriter- Throws:
java.io.IOException
-
writeCommentStart
public final void writeCommentStart() throws java.io.IOException- Specified by:
writeCommentStartin classXmlWriter- Throws:
java.io.IOException
-
writeCommentEnd
public final void writeCommentEnd() throws java.io.IOException- Specified by:
writeCommentEndin classXmlWriter- Throws:
java.io.IOException
-
writePIStart
public final void writePIStart(java.lang.String target, boolean addSpace) throws java.io.IOException- Specified by:
writePIStartin classXmlWriter- Throws:
java.io.IOException
-
writePIEnd
public final void writePIEnd() throws java.io.IOException- Specified by:
writePIEndin classXmlWriter- Throws:
java.io.IOException
-
writeCData
public int writeCData(java.lang.String data) throws java.io.IOException- Specified by:
writeCDatain classXmlWriter- Parameters:
data- Contents of the CDATA section to write out- Returns:
- offset of the (first) illegal content segment ("]]>") in passed content and not in repairing mode; or -1 if none or is repairing
- Throws:
java.io.IOException
-
writeCData
public int writeCData(char[] cbuf, int offset, int len) throws java.io.IOException- Specified by:
writeCDatain classXmlWriter- Throws:
java.io.IOException
-
writeCharacters
public void writeCharacters(java.lang.String text) throws java.io.IOException- Specified by:
writeCharactersin classXmlWriter- Throws:
java.io.IOException
-
writeCharacters
public void writeCharacters(char[] cbuf, int offset, int len) throws java.io.IOException- Specified by:
writeCharactersin classXmlWriter- Throws:
java.io.IOException
-
writeComment
public int writeComment(java.lang.String data) throws java.io.IOExceptionMethod that will try to output the content as specified. If the content passed in has embedded "--" in it, it will either add an intervening space between consequtive hyphens (if content fixing is enabled), or return the offset of the first hyphen in multi-hyphen sequence.- Specified by:
writeCommentin classXmlWriter- Throws:
java.io.IOException
-
writeDTD
public void writeDTD(java.lang.String data) throws java.io.IOExceptionDescription copied from class:XmlWriterOlder "legacy" output method for outputting DOCTYPE declaration. Assumes that the passed-in String contains a complete DOCTYPE declaration properly quoted.
-
writeDTD
public void writeDTD(java.lang.String rootName, java.lang.String systemId, java.lang.String publicId, java.lang.String internalSubset) throws java.io.IOException, javax.xml.stream.XMLStreamException
-
writeEntityReference
public void writeEntityReference(java.lang.String name) throws java.io.IOException, javax.xml.stream.XMLStreamException- Specified by:
writeEntityReferencein classXmlWriter- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writeXmlDeclaration
public void writeXmlDeclaration(java.lang.String version, java.lang.String encoding, java.lang.String standalone) throws java.io.IOException- Specified by:
writeXmlDeclarationin classXmlWriter- Throws:
java.io.IOException
-
writePI
public int writePI(java.lang.String target, java.lang.String data) throws java.io.IOException, javax.xml.stream.XMLStreamException
-
writeStartTagStart
public void writeStartTagStart(java.lang.String localName) throws java.io.IOException, javax.xml.stream.XMLStreamExceptionDescription copied from class:XmlWriterNote: can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)
- Specified by:
writeStartTagStartin classXmlWriter- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writeStartTagStart
public void writeStartTagStart(java.lang.String prefix, java.lang.String localName) throws java.io.IOException, javax.xml.stream.XMLStreamExceptionDescription copied from class:XmlWriterNote: can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)
- Specified by:
writeStartTagStartin classXmlWriter- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writeStartTagEnd
public void writeStartTagEnd() throws java.io.IOException- Specified by:
writeStartTagEndin classXmlWriter- Throws:
java.io.IOException
-
writeStartTagEmptyEnd
public void writeStartTagEmptyEnd() throws java.io.IOException- Specified by:
writeStartTagEmptyEndin classXmlWriter- Throws:
java.io.IOException
-
writeEndTag
public void writeEndTag(java.lang.String localName) throws java.io.IOException- Specified by:
writeEndTagin classXmlWriter- Throws:
java.io.IOException
-
writeEndTag
public void writeEndTag(java.lang.String prefix, java.lang.String localName) throws java.io.IOException- Specified by:
writeEndTagin classXmlWriter- Throws:
java.io.IOException
-
writeAttribute
public void writeAttribute(java.lang.String localName, java.lang.String value) throws java.io.IOException, javax.xml.stream.XMLStreamExceptionDescription copied from class:XmlWriterNote: can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)
- Specified by:
writeAttributein classXmlWriter- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writeAttribute
public void writeAttribute(java.lang.String localName, char[] value, int offset, int vlen) throws java.io.IOException, javax.xml.stream.XMLStreamException- Specified by:
writeAttributein classXmlWriter- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writeAttribute
public void writeAttribute(java.lang.String prefix, java.lang.String localName, java.lang.String value) throws java.io.IOException, javax.xml.stream.XMLStreamExceptionDescription copied from class:XmlWriterNote: can throw XMLStreamException, if name checking is enabled, and name is invalid (name check has to be in this writer, not caller, since it depends not only on xml limitations, but also on encoding limitations)
- Specified by:
writeAttributein classXmlWriter- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writeAttribute
public void writeAttribute(java.lang.String prefix, java.lang.String localName, char[] value, int offset, int vlen) throws java.io.IOException, javax.xml.stream.XMLStreamException- Specified by:
writeAttributein classXmlWriter- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writeAttrValue
private final void writeAttrValue(java.lang.String value, int len) throws java.io.IOException- Throws:
java.io.IOException
-
writeAttrValue
private final void writeAttrValue(char[] value, int offset, int len) throws java.io.IOException- Throws:
java.io.IOException
-
writeTypedElement
public final void writeTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws java.io.IOExceptionDescription copied from class:XmlWriterLikeXmlWriter.writeRaw(java.lang.String, int, int), but caller guarantees that the contents additionally are known to be in 7-bit ascii range, and also passes an encoder object that will encode values only when being handed a buffer to append to.- Specified by:
writeTypedElementin classXmlWriter- Parameters:
enc- Encoder that will produce content- Throws:
java.io.IOException
-
writeTypedElement
public final void writeTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer) throws java.io.IOException, javax.xml.stream.XMLStreamExceptionDescription copied from class:XmlWriterLikeXmlWriter.writeRaw(java.lang.String, int, int), but caller guarantees that the contents additionally are known to be in 7-bit ascii range, and also passes an encoder object that will encode values only when being handed a buffer to append to.- Specified by:
writeTypedElementin classXmlWriter- Parameters:
enc- Encoder that will produce contentvalidator- Validator to use for validating serialized textual content (can not be null)copyBuffer- Temporary buffer that writer can use for temporary copies as necessary- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writeTypedAttribute
public void writeTypedAttribute(java.lang.String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws java.io.IOException, javax.xml.stream.XMLStreamExceptionDescription copied from class:XmlWriterMethod similar toXmlWriter.writeAttribute(String,String,char[],int,int)but where is known not to require escaping. No validation needs to be performed.- Specified by:
writeTypedAttributein classXmlWriter- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writeTypedAttribute
public void writeTypedAttribute(java.lang.String prefix, java.lang.String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws java.io.IOException, javax.xml.stream.XMLStreamExceptionDescription copied from class:XmlWriterMethod similar toXmlWriter.writeAttribute(String,String,char[],int,int)but where is known not to require escaping. No validation needs to be performed.- Specified by:
writeTypedAttributein classXmlWriter- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writeTypedAttribute
public void writeTypedAttribute(java.lang.String prefix, java.lang.String localName, java.lang.String nsURI, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer) throws java.io.IOException, javax.xml.stream.XMLStreamExceptionDescription copied from class:XmlWriterMethod similar toXmlWriter.writeAttribute(String,String,char[],int,int)but where is known not to require escaping. Validation of the attribute value must be done by calling given validator appropriately.- Specified by:
writeTypedAttributein classXmlWriter- Throws:
java.io.IOExceptionjavax.xml.stream.XMLStreamException
-
writePrefixedName
protected final void writePrefixedName(java.lang.String prefix, java.lang.String localName) throws java.io.IOException- Throws:
java.io.IOException
-
flushBuffer
private final void flushBuffer() throws java.io.IOException- Throws:
java.io.IOException
-
fastWriteRaw
private final void fastWriteRaw(char c) throws java.io.IOException- Throws:
java.io.IOException
-
fastWriteRaw
private final void fastWriteRaw(char c1, char c2) throws java.io.IOException- Throws:
java.io.IOException
-
fastWriteRaw
private final void fastWriteRaw(java.lang.String str) throws java.io.IOException- Throws:
java.io.IOException
-
verifyCDataContent
protected int verifyCDataContent(java.lang.String content)
- Returns:
- Index at which a problem was found, if any; -1 if there's no problem.
-
verifyCDataContent
protected int verifyCDataContent(char[] c, int start, int end)
-
verifyCommentContent
protected int verifyCommentContent(java.lang.String content)
-
writeSegmentedCData
protected void writeSegmentedCData(java.lang.String content, int index) throws java.io.IOException- Throws:
java.io.IOException
-
writeSegmentedCData
protected void writeSegmentedCData(char[] c, int start, int len, int index) throws java.io.IOException- Throws:
java.io.IOException
-
writeSegmentedComment
protected void writeSegmentedComment(java.lang.String content, int index) throws java.io.IOException- Throws:
java.io.IOException
-
guessEncodingBitSize
public static int guessEncodingBitSize(java.lang.String enc)
Method used to figure out which part of the Unicode char set the encoding can natively support. Values returned are 7, 8 and 16, to indicate (respectively) "ascii", "ISO-Latin" and "native Unicode". These just best guesses, but should work ok for the most common encodings.
-
writeAsEntity
protected final void writeAsEntity(int c) throws java.io.IOException- Throws:
java.io.IOException
-
-