Class BufferingXmlWriter
- All Implemented Interfaces:
XMLStreamConstants
XmlWriter that will dispatch writes
to another writer (of type Writer, 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
FieldsModifier and TypeFieldDescription(package private) static final intLet's use a typical default to have a compromise between large enough chunks to output, and minimizing memory overhead.(package private) static final intChoosing 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 final intHighest valued character that may need to be encoded (minus charset encoding requirements) when writing attribute values.protected static final intHighest valued character that may need to be encoded (minus charset encoding requirements) when writing attribute values.private final intFirst Unicode character (one with lowest value) after (and including) which character entities have to be used.(package private) final charCharacter that is considered to be the enclosing quote character; for XML either single or double quote.(package private) final StringEntity String to use for escaping the quote character.protected final WriterActual Writer to use for outputting buffered data as appropriate.protected char[]protected intprotected intprotected final intThis 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 final OutputStreamActual physical stream that the writer is using, if known.protected static final int[]Fields inherited from class 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_LASTFields inherited from interface XMLStreamConstants
ATTRIBUTE, CDATA, CHARACTERS, COMMENT, DTD, END_DOCUMENT, END_ELEMENT, ENTITY_DECLARATION, ENTITY_REFERENCE, NAMESPACE, NOTATION_DECLARATION, PROCESSING_INSTRUCTION, SPACE, START_DOCUMENT, START_ELEMENT -
Constructor Summary
ConstructorsConstructorDescriptionBufferingXmlWriter(Writer out, WriterConfig cfg, String enc, boolean autoclose, OutputStream outs, int bitsize) -
Method Summary
Modifier and TypeMethodDescriptionvoidclose(boolean forceRealClose) Method called to flush the buffer(s), and close the output sink (stream or writer) if enabled (auto-closing) or forced.private final voidfastWriteRaw(char c) private final voidfastWriteRaw(char c1, char c2) private final voidfastWriteRaw(String str) final voidflush()private final voidprotected intprotected final OutputStreamprotected final Writerstatic intMethod 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(String content) protected intverifyCommentContent(String content) protected final voidwriteAsEntity(int c) voidwriteAttribute(String localName, char[] value, int offset, int vlen) voidwriteAttribute(String localName, 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(String prefix, String localName, char[] value, int offset, int vlen) voidwriteAttribute(String prefix, String localName, 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 final voidwriteAttrValue(char[] value, int offset, int len) private final voidwriteAttrValue(String value, int len) intwriteCData(char[] cbuf, int offset, int len) intwriteCData(String data) final voidfinal voidvoidwriteCharacters(char[] cbuf, int offset, int len) voidwriteCharacters(String text) intwriteComment(String data) Method that will try to output the content as specified.final voidfinal voidvoidOlder "legacy" output method for outputting DOCTYPE declaration.voidvoidwriteEndTag(String localName) voidwriteEndTag(String prefix, String localName) voidwriteEntityReference(String name) intfinal voidfinal voidwritePIStart(String target, boolean addSpace) protected final voidwritePrefixedName(String prefix, String localName) voidwriteRaw(char[] cbuf, int offset, int len) voidvoidfinal 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(String content, int index) protected voidwriteSegmentedComment(String content, int index) voidvoidvoidwriteStartTagStart(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(String prefix, 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(String prefix, String localName, 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(String prefix, 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(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.final voidwriteTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) LikeXmlWriter.writeRaw(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.final voidwriteTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer) LikeXmlWriter.writeRaw(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(String version, String encoding, String standalone) Methods inherited from class XmlWriter
enableXml11, getAbsOffset, getColumn, getRow, handleInvalidChar, reportNwfContent, reportNwfName, throwOutputError, throwOutputError, verifyNameValidity, wrapAsRawWriter, wrapAsTextWriter
-
Field Details
-
DEFAULT_BUFFER_SIZE
static final int DEFAULT_BUFFER_SIZELet'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:
-
DEFAULT_SMALL_SIZE
static final int DEFAULT_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). Let's just use about 1/4 of the full buffer size.- See Also:
-
HIGHEST_ENCODABLE_ATTR_CHAR
protected static final int HIGHEST_ENCODABLE_ATTR_CHARHighest valued character that may need to be encoded (minus charset encoding requirements) when writing attribute values.- See Also:
-
HIGHEST_ENCODABLE_TEXT_CHAR
protected static final int HIGHEST_ENCODABLE_TEXT_CHARHighest valued character that may need to be encoded (minus charset encoding requirements) when writing attribute values.- See Also:
-
QUOTABLE_TEXT_CHARS
protected static final int[] QUOTABLE_TEXT_CHARS -
mOut
Actual Writer to use for outputting buffered data as appropriate. -
mOutputBuffer
protected char[] mOutputBuffer -
mSmallWriteSize
protected final int mSmallWriteSizeThis 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
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 mEncHighCharFirst Unicode character (one with lowest value) after (and including) which character entities have to be used. For -
mEncQuoteChar
final char mEncQuoteCharCharacter that is considered to be the enclosing quote character; for XML either single or double quote. -
mEncQuoteEntity
Entity String to use for escaping the quote character.
-
-
Constructor Details
-
BufferingXmlWriter
public BufferingXmlWriter(Writer out, WriterConfig cfg, String enc, boolean autoclose, OutputStream outs, int bitsize) throws IOException - Parameters:
outs- Underlying OutputStream that the writer (out) is using, if known. Needed to support (optional) access to the underlying stream- Throws:
IOException
-
-
Method Details
-
getOutputPtr
protected int getOutputPtr()- Specified by:
getOutputPtrin classXmlWriter
-
getOutputStream
- Specified by:
getOutputStreamin classXmlWriter- Returns:
- Underlying OutputStream used for physical output, if the writer was constructed using one
-
getWriter
-
close
Description copied from class:XmlWriterMethod called to flush the buffer(s), and close the output sink (stream or writer) if enabled (auto-closing) or forced.- Specified by:
closein classXmlWriter- Throws:
IOException
-
flush
- Specified by:
flushin classXmlWriter- Throws:
IOException
-
writeRaw
- Specified by:
writeRawin classXmlWriter- Throws:
IOException
-
writeRawAscii
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.- Specified by:
writeRawAsciiin classXmlWriter- Throws:
IOException
-
writeRaw
- Overrides:
writeRawin classXmlWriter- Throws:
IOException
-
writeRaw
- Specified by:
writeRawin classXmlWriter- Throws:
IOException
-
writeCDataStart
- Specified by:
writeCDataStartin classXmlWriter- Throws:
IOException
-
writeCDataEnd
- Specified by:
writeCDataEndin classXmlWriter- Throws:
IOException
-
writeCommentStart
- Specified by:
writeCommentStartin classXmlWriter- Throws:
IOException
-
writeCommentEnd
- Specified by:
writeCommentEndin classXmlWriter- Throws:
IOException
-
writePIStart
- Specified by:
writePIStartin classXmlWriter- Throws:
IOException
-
writePIEnd
- Specified by:
writePIEndin classXmlWriter- Throws:
IOException
-
writeCData
- 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:
IOException
-
writeCData
- Specified by:
writeCDatain classXmlWriter- Throws:
IOException
-
writeCharacters
- Specified by:
writeCharactersin classXmlWriter- Throws:
IOException
-
writeCharacters
- Specified by:
writeCharactersin classXmlWriter- Throws:
IOException
-
writeComment
Method 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:
IOException
-
writeDTD
Description copied from class:XmlWriterOlder "legacy" output method for outputting DOCTYPE declaration. Assumes that the passed-in String contains a complete DOCTYPE declaration properly quoted.- Specified by:
writeDTDin classXmlWriter- Throws:
IOException
-
writeDTD
public void writeDTD(String rootName, String systemId, String publicId, String internalSubset) throws IOException, XMLStreamException - Specified by:
writeDTDin classXmlWriter- Throws:
IOExceptionXMLStreamException
-
writeEntityReference
- Specified by:
writeEntityReferencein classXmlWriter- Throws:
IOExceptionXMLStreamException
-
writeXmlDeclaration
public void writeXmlDeclaration(String version, String encoding, String standalone) throws IOException - Specified by:
writeXmlDeclarationin classXmlWriter- Throws:
IOException
-
writePI
- Specified by:
writePIin classXmlWriter- Throws:
IOExceptionXMLStreamException
-
writeStartTagStart
Description 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:
IOExceptionXMLStreamException
-
writeStartTagStart
public void writeStartTagStart(String prefix, String localName) throws IOException, XMLStreamException Description 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:
IOExceptionXMLStreamException
-
writeStartTagEnd
- Specified by:
writeStartTagEndin classXmlWriter- Throws:
IOException
-
writeStartTagEmptyEnd
- Specified by:
writeStartTagEmptyEndin classXmlWriter- Throws:
IOException
-
writeEndTag
- Specified by:
writeEndTagin classXmlWriter- Throws:
IOException
-
writeEndTag
- Specified by:
writeEndTagin classXmlWriter- Throws:
IOException
-
writeAttribute
Description 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:
IOExceptionXMLStreamException
-
writeAttribute
public void writeAttribute(String localName, char[] value, int offset, int vlen) throws IOException, XMLStreamException - Specified by:
writeAttributein classXmlWriter- Throws:
IOExceptionXMLStreamException
-
writeAttribute
public void writeAttribute(String prefix, String localName, String value) throws IOException, XMLStreamException Description 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:
IOExceptionXMLStreamException
-
writeAttribute
public void writeAttribute(String prefix, String localName, char[] value, int offset, int vlen) throws IOException, XMLStreamException - Specified by:
writeAttributein classXmlWriter- Throws:
IOExceptionXMLStreamException
-
writeAttrValue
- Throws:
IOException
-
writeAttrValue
- Throws:
IOException
-
writeTypedElement
public final void writeTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws IOException Description copied from class:XmlWriterLikeXmlWriter.writeRaw(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:
IOException
-
writeTypedElement
public final void writeTypedElement(org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer) throws IOException, XMLStreamException Description copied from class:XmlWriterLikeXmlWriter.writeRaw(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:
IOExceptionXMLStreamException
-
writeTypedAttribute
public void writeTypedAttribute(String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws IOException, XMLStreamException Description 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:
IOExceptionXMLStreamException
-
writeTypedAttribute
public void writeTypedAttribute(String prefix, String localName, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc) throws IOException, XMLStreamException Description 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:
IOExceptionXMLStreamException
-
writeTypedAttribute
public void writeTypedAttribute(String prefix, String localName, String nsURI, org.codehaus.stax2.ri.typed.AsciiValueEncoder enc, org.codehaus.stax2.validation.XMLValidator validator, char[] copyBuffer) throws IOException, XMLStreamException Description 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:
IOExceptionXMLStreamException
-
writePrefixedName
- Throws:
IOException
-
flushBuffer
- Throws:
IOException
-
fastWriteRaw
- Throws:
IOException
-
fastWriteRaw
- Throws:
IOException
-
fastWriteRaw
- Throws:
IOException
-
verifyCDataContent
- 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
-
writeSegmentedCData
- Throws:
IOException
-
writeSegmentedCData
- Throws:
IOException
-
writeSegmentedComment
- Throws:
IOException
-
guessEncodingBitSize
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
- Throws:
IOException
-