Skip to content

Commit 6a5b920

Browse files
author
Niall Pemberton
committed
IO-162 Allow the default encoding to be configurable
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1004737 13f79535-47bb-0310-9956-ffa450edef68
1 parent 7b6afbb commit 6a5b920

2 files changed

Lines changed: 69 additions & 17 deletions

File tree

src/main/java/org/apache/commons/io/output/XmlStreamWriter.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,32 +41,61 @@
4141
public 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

src/test/java/org/apache/commons/io/output/XmlStreamWriterTest.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
21+
import java.util.Arrays;
2122

2223
import junit.framework.TestCase;
2324

@@ -47,27 +48,36 @@ private static String createXmlContent(String text, String encoding) {
4748
return xml;
4849
}
4950

50-
private static void checkXmlContent(String xml, String encoding)
51+
private static void checkXmlContent(String xml, String encoding, String defaultEncoding)
5152
throws IOException {
5253
ByteArrayOutputStream out = new ByteArrayOutputStream();
53-
XmlStreamWriter writer = new XmlStreamWriter(out);
54+
XmlStreamWriter writer = new XmlStreamWriter(out, defaultEncoding);
5455
writer.write(xml);
5556
writer.close();
5657
byte[] xmlContent = out.toByteArray();
57-
String result = new String(xmlContent, encoding);
58-
assertEquals(xml, result);
58+
assertEquals(encoding, writer.getEncoding());
59+
assertTrue(Arrays.equals(xml.getBytes(encoding), xmlContent));
60+
5961
}
6062

6163
private static void checkXmlWriter(String text, String encoding)
6264
throws IOException {
65+
checkXmlWriter(text, encoding, null);
66+
}
67+
68+
private static void checkXmlWriter(String text, String encoding, String defaultEncoding)
69+
throws IOException {
6370
String xml = createXmlContent(text, encoding);
64-
String effectiveEncoding = (encoding == null) ? "UTF-8" : encoding;
65-
checkXmlContent(xml, effectiveEncoding);
71+
String effectiveEncoding = encoding;
72+
if (effectiveEncoding == null) {
73+
effectiveEncoding = (defaultEncoding == null ? "UTF-8" : defaultEncoding);
74+
}
75+
checkXmlContent(xml, effectiveEncoding, defaultEncoding);
6676
}
6777

6878
public void testNoXmlHeader() throws IOException {
6979
String xml = "<text>text with no XML header</text>";
70-
checkXmlContent(xml, "UTF-8");
80+
checkXmlContent(xml, "UTF-8", null);
7181
}
7282

7383
public void testEmpty() throws IOException {
@@ -82,7 +92,11 @@ public void testEmpty() throws IOException {
8292
}
8393

8494
public void testDefaultEncoding() throws IOException {
85-
checkXmlWriter(TEXT_UNICODE, null);
95+
checkXmlWriter(TEXT_UNICODE, null, null);
96+
checkXmlWriter(TEXT_UNICODE, null, "UTF-8");
97+
checkXmlWriter(TEXT_UNICODE, null, "UTF-16");
98+
checkXmlWriter(TEXT_UNICODE, null, "UTF-16BE");
99+
checkXmlWriter(TEXT_UNICODE, null, "ISO-8859-1");
86100
}
87101

88102
public void testUTF8Encoding() throws IOException {

0 commit comments

Comments
 (0)