4141public class XmlStreamWriter extends Writer {
4242 private static final int BUFFER_SIZE = 4096 ;
4343
44- private StringWriter xmlPrologWriter = new StringWriter (BUFFER_SIZE );
44+ private final OutputStream out ;
45+
46+ private final String defaultEncoding ;
4547
46- private OutputStream out ;
48+ private StringWriter xmlPrologWriter = new StringWriter ( BUFFER_SIZE ) ;
4749
4850 private Writer writer ;
4951
5052 private String encoding ;
5153
5254 /**
53- * Construct an new XML stream writer for the specified output stream.
55+ * Construct an new XML stream writer for the specified output stream
56+ * with a default encoding of UTF-8.
5457 *
5558 * @param out The output stream
5659 */
5760 public XmlStreamWriter (OutputStream out ) {
61+ this (out , null );
62+ }
63+
64+ /**
65+ * Construct an new XML stream writer for the specified output stream
66+ * with the specified default encoding.
67+ *
68+ * @param out The output stream
69+ * @param defaultEncoding The default encoding if not encoding could be detected
70+ */
71+ public XmlStreamWriter (OutputStream out , String defaultEncoding ) {
5872 this .out = out ;
73+ this .defaultEncoding = (defaultEncoding != null ? defaultEncoding : "UTF-8" );
5974 }
6075
6176 /**
62- * Construct an new XML stream writer for the specified file.
77+ * Construct an new XML stream writer for the specified file
78+ * with a default encoding of UTF-8.
6379 *
6480 * @param file The file to write to
6581 * @throws FileNotFoundException if there is an error creating or
6682 * opening the file
6783 */
6884 public XmlStreamWriter (File file ) throws FileNotFoundException {
69- this (new FileOutputStream (file ));
85+ this (file , null );
86+ }
87+
88+ /**
89+ * Construct an new XML stream writer for the specified file
90+ * with the specified default encoding.
91+ *
92+ * @param file The file to write to
93+ * @param defaultEncoding The default encoding if not encoding could be detected
94+ * @throws FileNotFoundException if there is an error creating or
95+ * opening the file
96+ */
97+ public XmlStreamWriter (File file , String defaultEncoding ) throws FileNotFoundException {
98+ this (new FileOutputStream (file ), defaultEncoding );
7099 }
71100
72101 /**
@@ -78,6 +107,15 @@ public String getEncoding() {
78107 return encoding ;
79108 }
80109
110+ /**
111+ * Return the default encoding.
112+ *
113+ * @return the default encoding
114+ */
115+ public String getDefaultEncoding () {
116+ return defaultEncoding ;
117+ }
118+
81119 /**
82120 * Close the underlying writer.
83121 *
@@ -86,7 +124,7 @@ public String getEncoding() {
86124 @ Override
87125 public void close () throws IOException {
88126 if (writer == null ) {
89- encoding = "UTF-8" ;
127+ encoding = defaultEncoding ;
90128 writer = new OutputStreamWriter (out , encoding );
91129 writer .write (xmlPrologWriter .toString ());
92130 }
@@ -137,18 +175,18 @@ private void detectEncoding(char[] cbuf, int off, int len)
137175 } else {
138176 // no encoding found in XML prolog: using default
139177 // encoding
140- encoding = "UTF-8" ;
178+ encoding = defaultEncoding ;
141179 }
142180 } else {
143181 if (xmlProlog .length () >= BUFFER_SIZE ) {
144182 // no encoding found in first characters: using default
145183 // encoding
146- encoding = "UTF-8" ;
184+ encoding = defaultEncoding ;
147185 }
148186 }
149187 } else {
150188 // no XML prolog: using default encoding
151- encoding = "UTF-8" ;
189+ encoding = defaultEncoding ;
152190 }
153191 if (encoding != null ) {
154192 // encoding has been chosen: let's do it
0 commit comments