< prev index next >

src/java.base/share/classes/jdk/internal/util/xml/impl/XMLStreamWriterImpl.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -64,11 +64,11 @@
     public static final char SEMICOLON = ';';
     //current state
     private int _state = 0;
     private Element _currentEle;
     private XMLWriter _writer;
-    private String _encoding;
+    private Charset _charset;
     /**
      * This flag can be used to turn escaping off for content. It does
      * not apply to attribute content.
      */
     boolean _escapeCharacters = true;

@@ -77,50 +77,47 @@
     //The system line separator for writing out line breaks.
     private char[] _lineSep =
             System.getProperty("line.separator").toCharArray();
 
     public XMLStreamWriterImpl(OutputStream os) throws XMLStreamException {
-        this(os, XMLStreamWriter.DEFAULT_ENCODING);
+        this(os, XMLStreamWriter.DEFAULT_CHARSET);
     }
 
-    public XMLStreamWriterImpl(OutputStream os, String encoding)
+    public XMLStreamWriterImpl(OutputStream os, Charset cs)
         throws XMLStreamException
     {
-        Charset cs = null;
-        if (encoding == null) {
-            _encoding = XMLStreamWriter.DEFAULT_ENCODING;
+        if (cs == null) {
+            _charset = XMLStreamWriter.DEFAULT_CHARSET;
         } else {
             try {
-                cs = getCharset(encoding);
+                _charset = checkCharset(cs);
             } catch (UnsupportedEncodingException e) {
                 throw new XMLStreamException(e);
             }
-
-            this._encoding = encoding;
         }
 
-        _writer = new XMLWriter(os, encoding, cs);
+        _writer = new XMLWriter(os, null, _charset);
     }
 
     /**
      * Write the XML Declaration. Defaults the XML version to 1.0, and the
      * encoding to utf-8.
      *
      * @throws XMLStreamException
      */
     public void writeStartDocument() throws XMLStreamException {
-        writeStartDocument(_encoding, XMLStreamWriter.DEFAULT_XML_VERSION);
+        writeStartDocument(_charset.name(), XMLStreamWriter.DEFAULT_XML_VERSION);
     }
 
     /**
      * Write the XML Declaration. Defaults the encoding to utf-8
      *
      * @param version version of the xml document
      * @throws XMLStreamException
      */
     public void writeStartDocument(String version) throws XMLStreamException {
-        writeStartDocument(_encoding, version, null);
+        writeStartDocument(_charset.name(), version, null);
     }
 
     /**
      * Write the XML Declaration. Note that the encoding parameter does not set
      * the actual encoding of the underlying output. That must be set when the

@@ -153,11 +150,11 @@
             throw new XMLStreamException("XML declaration must be as the first line in the XML document.");
         }
         _state = STATE_XML_DECL;
         String enc = encoding;
         if (enc == null) {
-            enc = _encoding;
+            enc = _charset.name();
         } else {
             //check if the encoding is supported
             try {
                 getCharset(encoding);
             } catch (UnsupportedEncodingException e) {

@@ -562,10 +559,24 @@
             throw new UnsupportedEncodingException(encoding);
         }
         return cs;
     }
 
+    /**
+     * Checks for charset support.
+     * @param charset the specified charset
+     * @return the charset
+     * @throws UnsupportedEncodingException if the charset is not supported
+     */
+    private Charset checkCharset(Charset charset) throws UnsupportedEncodingException {
+        if (charset.name().equalsIgnoreCase("UTF-32")) {
+            throw new UnsupportedEncodingException("The basic XMLWriter does "
+                    + "not support " + charset.name());
+        }
+        return charset;
+    }
+
     /*
      * Start of Internal classes.
      *
      */
     protected class Element {
< prev index next >