Skip to content

Commit 0ee77ba

Browse files
committed
use charset instances
1 parent 1f58994 commit 0ee77ba

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

src/main/java/com/fasterxml/jackson/dataformat/xml/XmlFactory.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fasterxml.jackson.dataformat.xml;
22

33
import java.io.*;
4+
import java.nio.charset.Charset;
5+
import java.nio.charset.StandardCharsets;
46

57
import javax.xml.stream.*;
68

@@ -537,7 +539,7 @@ public ToXmlGenerator createGenerator(OutputStream out, JsonEncoding enc) throws
537539
* @throws IOException
538540
* @since 2.16
539541
*/
540-
public ToXmlGenerator createGenerator(OutputStream out, String encoding) throws IOException
542+
public ToXmlGenerator createGenerator(OutputStream out, Charset encoding) throws IOException
541543
{
542544
// false -> we won't manage the stream unless explicitly directed to
543545
final IOContext ctxt = _createContext(_createContentReference(out), false);
@@ -718,14 +720,14 @@ protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOEx
718720

719721
protected XMLStreamWriter _createXmlWriter(IOContext ctxt, OutputStream out) throws IOException
720722
{
721-
return _createXmlWriter(ctxt, out, "UTF-8");
723+
return _createXmlWriter(ctxt, out, StandardCharsets.UTF_8);
722724
}
723725

724-
protected XMLStreamWriter _createXmlWriter(IOContext ctxt, OutputStream out, String encoding) throws IOException
726+
protected final XMLStreamWriter _createXmlWriter(IOContext ctxt, OutputStream out, Charset encoding) throws IOException
725727
{
726728
XMLStreamWriter sw;
727729
try {
728-
sw = _xmlOutputFactory.createXMLStreamWriter(_decorate(ctxt, out), encoding);
730+
sw = _xmlOutputFactory.createXMLStreamWriter(_decorate(ctxt, out), encoding.name());
729731
} catch (Exception e) {
730732
throw new JsonGenerationException(e.getMessage(), e, null);
731733
}

src/main/java/com/fasterxml/jackson/dataformat/xml/XmlMapper.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.FileOutputStream;
55
import java.io.IOException;
66
import java.io.OutputStream;
7+
import java.nio.charset.Charset;
78

89
import javax.xml.stream.XMLInputFactory;
910
import javax.xml.stream.XMLOutputFactory;
@@ -408,7 +409,7 @@ public void writeValue(XMLStreamWriter w0, Object value) throws IOException {
408409
* @throws JsonProcessingException
409410
* @since 2.16
410411
*/
411-
public byte[] writeValueAsBytes(Object value, String encoding) throws JsonProcessingException {
412+
public byte[] writeValueAsBytes(Object value, Charset encoding) throws JsonProcessingException {
412413
try (ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler())) {
413414
_writeValueAndClose(createGenerator(bb, encoding), value);
414415
final byte[] result = bb.toByteArray();
@@ -433,7 +434,7 @@ public byte[] writeValueAsBytes(Object value, String encoding) throws JsonProces
433434
* @throws DatabindException
434435
* @since 2.16
435436
*/
436-
public void writeValue(File resultFile, Object value, String encoding)
437+
public void writeValue(File resultFile, Object value, Charset encoding)
437438
throws IOException, StreamWriteException, DatabindException
438439
{
439440
_writeValueAndClose(createGenerator(resultFile, encoding), value);
@@ -452,20 +453,20 @@ public void writeValue(File resultFile, Object value, String encoding)
452453
*
453454
* @since 2.16
454455
*/
455-
public void writeValue(OutputStream out, Object value, String encoding)
456+
public void writeValue(OutputStream out, Object value, Charset encoding)
456457
throws IOException, StreamWriteException, DatabindException
457458
{
458459
_writeValueAndClose(createGenerator(out, encoding), value);
459460
}
460461

461-
protected final JsonGenerator createGenerator(OutputStream out, String encoding) throws IOException {
462+
protected final JsonGenerator createGenerator(OutputStream out, Charset encoding) throws IOException {
462463
this._assertNotNull("out", out);
463464
JsonGenerator g = ((XmlFactory) _jsonFactory).createGenerator(out, encoding);
464465
this._serializationConfig.initialize(g);
465466
return g;
466467
}
467468

468-
protected final JsonGenerator createGenerator(File outputFile, String encoding) throws IOException {
469+
protected final JsonGenerator createGenerator(File outputFile, Charset encoding) throws IOException {
469470
_assertNotNull("outputFile", outputFile);
470471
JsonGenerator g = ((XmlFactory) _jsonFactory).createGenerator(
471472
new FileOutputStream(outputFile), encoding);

src/main/java/com/fasterxml/jackson/dataformat/xml/ser/ToXmlGenerator.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.math.BigDecimal;
55
import java.math.BigInteger;
6+
import java.nio.charset.Charset;
67
import java.nio.charset.StandardCharsets;
78
import java.util.*;
89

@@ -219,7 +220,7 @@ private Feature(boolean defaultState) {
219220
*
220221
* @since 2.16
221222
*/
222-
protected final String _encoding;
223+
protected final Charset _encoding;
223224

224225
/*
225226
/**********************************************************
@@ -230,12 +231,12 @@ private Feature(boolean defaultState) {
230231
public ToXmlGenerator(IOContext ctxt, int stdFeatures, int xmlFeatures,
231232
ObjectCodec codec, XMLStreamWriter sw, XmlNameProcessor nameProcessor)
232233
{
233-
this(ctxt, stdFeatures, xmlFeatures, codec, sw, nameProcessor, StandardCharsets.UTF_8.name());
234+
this(ctxt, stdFeatures, xmlFeatures, codec, sw, nameProcessor, StandardCharsets.UTF_8);
234235
}
235236

236237
public ToXmlGenerator(IOContext ctxt, int stdFeatures, int xmlFeatures,
237238
ObjectCodec codec, XMLStreamWriter sw, XmlNameProcessor nameProcessor,
238-
String encoding)
239+
Charset encoding)
239240
{
240241
super(stdFeatures, codec);
241242
_formatFeatures = xmlFeatures;
@@ -261,9 +262,9 @@ public void initGenerator() throws IOException
261262
_initialized = true;
262263
try {
263264
if (Feature.WRITE_XML_1_1.enabledIn(_formatFeatures)) {
264-
_xmlWriter.writeStartDocument(_encoding, "1.1");
265+
_xmlWriter.writeStartDocument(_encoding.name(), "1.1");
265266
} else if (Feature.WRITE_XML_DECLARATION.enabledIn(_formatFeatures)) {
266-
_xmlWriter.writeStartDocument(_encoding, "1.0");
267+
_xmlWriter.writeStartDocument(_encoding.name(), "1.0");
267268
} else {
268269
return;
269270
}

src/test/java/com/fasterxml/jackson/dataformat/xml/ser/TestCharset.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
55

66
import java.io.IOException;
7+
import java.nio.charset.Charset;
78

89
public class TestCharset extends XmlTestBase
910
{
@@ -13,12 +14,13 @@ static class StringBean {
1314

1415
public void testBig5() throws IOException
1516
{
17+
Charset big5 = Charset.forName("Big5");
1618
StringBean stringBean = new StringBean();
1719
stringBean.象形字 = "pictogram";
1820
XmlMapper xmlMapper = new XmlMapper();
1921
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
20-
byte[] xml = xmlMapper.writeValueAsBytes(stringBean, "Big5");
21-
String xmlText = new String(xml, "Big5");
22+
byte[] xml = xmlMapper.writeValueAsBytes(stringBean, big5);
23+
String xmlText = new String(xml, big5);
2224
String expected =
2325
"<?xml version='1.1' encoding='Big5'?><StringBean><象形字>pictogram</象形字></StringBean>";
2426
assertEquals(expected, xmlText);

0 commit comments

Comments
 (0)