--- old/src/java.xml/share/classes/com/sun/xml/internal/stream/writers/XMLStreamWriterImpl.java 2016-10-07 08:56:24.004424620 -0700 +++ new/src/java.xml/share/classes/com/sun/xml/internal/stream/writers/XMLStreamWriterImpl.java 2016-10-07 08:56:23.876418233 -0700 @@ -71,7 +71,7 @@ * @author Santiago.Pericas-Geertsen@sun.com * @author Sunitha.Reddy@sun.com */ -public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamWriter { +public final class XMLStreamWriterImpl extends AbstractMap implements XMLStreamWriterBase { public static final String START_COMMENT = ""; @@ -1112,90 +1112,55 @@ } /** - * @throws XMLStreamException + * Writes the XML declaration. + * + * @throws XMLStreamException in case of an IOException */ public void writeStartDocument() throws XMLStreamException { - try { - fWriter.write(DEFAULT_XMLDECL); - } catch (IOException ex) { - throw new XMLStreamException(ex); - } + writeStartDocument(null, null, false, false); } /** - * @param version - * @throws XMLStreamException + * Writes the XML declaration. + * + * @param version the specified version + * @throws XMLStreamException in case of an IOException */ public void writeStartDocument(String version) throws XMLStreamException { - try { - if ((version == null) || version.equals("")) { - writeStartDocument(); - - return; - } - - fWriter.write(""); - } catch (IOException ex) { - throw new XMLStreamException(ex); - } + writeStartDocument(null, version, false, false); } /** - * @param encoding - * @param version - * @throws XMLStreamException + * Writes the XML declaration. + * + * @param encoding the specified encoding + * @param version the specified version + * @throws XMLStreamException in case of an IOException */ + @Override public void writeStartDocument(String encoding, String version) throws XMLStreamException { - //Revisit : What about standalone ? - try { - if ((encoding == null) && (version == null)) { - writeStartDocument(); - - return; - } + writeStartDocument(encoding, version, false, false); + } - if (encoding == null) { - writeStartDocument(version); + @Override + public void writeStartDocument(String encoding, String version, + boolean standalone, boolean standaloneSet) + throws XMLStreamException { + try { + if ((encoding == null || encoding.length() == 0) + && (version == null || version.length() == 0) + && (!standaloneSet)) { + fWriter.write(DEFAULT_XMLDECL); return; } - String streamEncoding = null; - if (fWriter instanceof OutputStreamWriter) { - streamEncoding = ((OutputStreamWriter) fWriter).getEncoding(); - } - else if (fWriter instanceof UTF8OutputStreamWriter) { - streamEncoding = ((UTF8OutputStreamWriter) fWriter).getEncoding(); - } - else if (fWriter instanceof XMLWriter) { - streamEncoding = ((OutputStreamWriter) ((XMLWriter)fWriter).getWriter()).getEncoding(); - } - - if (streamEncoding != null && !streamEncoding.equalsIgnoreCase(encoding)) { - // If the equality check failed, check for charset encoding aliases - boolean foundAlias = false; - Set aliases = Charset.forName(encoding).aliases(); - for (Iterator it = aliases.iterator(); !foundAlias && it.hasNext(); ) { - if (streamEncoding.equalsIgnoreCase((String) it.next())) { - foundAlias = true; - } - } - // If no alias matches the encoding name, then report error - if (!foundAlias) { - throw new XMLStreamException("Underlying stream encoding '" - + streamEncoding - + "' and input paramter for writeStartDocument() method '" - + encoding + "' do not match."); - } + // Verify the encoding before writing anything + if (encoding != null && !encoding.equals("")) { + verifyEncoding(encoding); } - fWriter.write(""); } catch (IOException ex) { throw new XMLStreamException(ex); } } + /** + * Verifies that the encoding is consistent between the underlying encoding + * and that specified. + * + * @param encoding the specified encoding + * @throws XMLStreamException if they do not match + */ + private void verifyEncoding(String encoding) throws XMLStreamException { + + String streamEncoding = null; + if (fWriter instanceof OutputStreamWriter) { + streamEncoding = ((OutputStreamWriter) fWriter).getEncoding(); + } + else if (fWriter instanceof UTF8OutputStreamWriter) { + streamEncoding = ((UTF8OutputStreamWriter) fWriter).getEncoding(); + } + else if (fWriter instanceof XMLWriter) { + streamEncoding = ((OutputStreamWriter) ((XMLWriter)fWriter).getWriter()).getEncoding(); + } + + if (streamEncoding != null && !streamEncoding.equalsIgnoreCase(encoding)) { + // If the equality check failed, check for charset encoding aliases + boolean foundAlias = false; + Set aliases = Charset.forName(encoding).aliases(); + for (Iterator it = aliases.iterator(); !foundAlias && it.hasNext(); ) { + if (streamEncoding.equalsIgnoreCase((String) it.next())) { + foundAlias = true; + } + } + // If no alias matches the encoding name, then report error + if (!foundAlias) { + throw new XMLStreamException("Underlying stream encoding '" + + streamEncoding + + "' and input paramter for writeStartDocument() method '" + + encoding + "' do not match."); + } + } + } + /** * @param localName * @throws XMLStreamException