From d4b9f03059fdd391bcca243b0abf3bc37243e8b8 Mon Sep 17 00:00:00 2001 From: Roded Bahat Date: Mon, 12 Aug 2019 18:46:56 +0300 Subject: [PATCH] JCLOUDS-1510: ParseSax use charset constant for all String creations This commit prevents the convertStreamToStringAndParse method from failing XML validation on environments in which the default charset is not compatible with UTF-8. --- core/src/main/java/org/jclouds/http/functions/ParseSax.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/jclouds/http/functions/ParseSax.java b/core/src/main/java/org/jclouds/http/functions/ParseSax.java index 1564db59b2..a277922937 100644 --- a/core/src/main/java/org/jclouds/http/functions/ParseSax.java +++ b/core/src/main/java/org/jclouds/http/functions/ParseSax.java @@ -25,6 +25,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; +import java.nio.charset.StandardCharsets; import javax.annotation.Resource; @@ -93,7 +94,7 @@ public class ParseSax implements Function, InvocationContext String from = null; try { byte[] fromBytes = closeClientButKeepContentStream(response); - from = new String(fromBytes); + from = new String(fromBytes, StandardCharsets.UTF_8); validateXml(from); // Use InputStream to skip over byte order mark. return doParse(new InputSource(new ByteArrayInputStream(fromBytes))); @@ -134,7 +135,7 @@ public class ParseSax implements Function, InvocationContext protected T doParse(InputSource from) throws IOException, SAXException { checkNotNull(from, "xml inputsource"); - from.setEncoding("UTF-8"); + from.setEncoding(StandardCharsets.UTF_8.name()); parser.setContentHandler(getHandler()); // This method should accept documents with a BOM (Byte-order mark) parser.parse(from);