Repository: camel Updated Branches: refs/heads/master 1bb97cc69 -> d0b5f0498
CAMEL-8346 CAMEL-8356 corrections to default charset Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1a4fde13 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1a4fde13 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1a4fde13 Branch: refs/heads/master Commit: 1a4fde13536f657b83fd5335bdbfa4cda5d1f40f Parents: 1bb97cc Author: Stefan Mandel <mande...@gmail.com> Authored: Sun Mar 1 12:26:15 2015 +0100 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Mon Mar 2 13:38:44 2015 +0800 ---------------------------------------------------------------------- .../org/apache/camel/converter/IOConverter.java | 2 +- .../camel/converter/IOConverterCharsetTest.java | 41 ++++++++++++++++++-- .../apache/camel/jsonpath/JsonPathEngine.java | 9 +++-- 3 files changed, 43 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1a4fde13/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java b/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java index a4d4421..3993894 100644 --- a/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java +++ b/camel-core/src/main/java/org/apache/camel/converter/IOConverter.java @@ -81,7 +81,7 @@ public final class IOConverter { public static InputStream toInputStream(File file, String charset) throws IOException { if (charset != null) { final BufferedReader reader = toReader(file, charset); - final Charset defaultStreamCharset = Charset.forName("UTF-8"); + final Charset defaultStreamCharset = Charset.defaultCharset(); return new InputStream() { private ByteBuffer bufferBytes; private CharBuffer bufferedChars = CharBuffer.allocate(4096); http://git-wip-us.apache.org/repos/asf/camel/blob/1a4fde13/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java b/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java index fe74d75..52f2d10 100644 --- a/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java +++ b/camel-core/src/test/java/org/apache/camel/converter/IOConverterCharsetTest.java @@ -21,6 +21,8 @@ import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; +import java.lang.reflect.Field; +import java.nio.charset.Charset; import java.util.Arrays; import org.apache.camel.ContextTestSupport; @@ -29,10 +31,30 @@ public class IOConverterCharsetTest extends ContextTestSupport { private static final String CONTENT = "G\u00f6tzend\u00e4mmerung,Joseph und seine Br\u00fcder"; public void testToInputStreamFileWithCharsetUTF8() throws Exception { + switchToDefaultCharset("UTF-8"); File file = new File("src/test/resources/org/apache/camel/converter/german.utf-8.txt"); InputStream in = IOConverter.toInputStream(file, "UTF-8"); - // need to specify the encoding of the input stream bytes - BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); + // do read with default charset! + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); + try { + String line = reader.readLine(); + String naiveLine = naiveReader.readLine(); + assertEquals(naiveLine, line); + assertEquals(CONTENT, line); + } finally { + reader.close(); + naiveReader.close(); + } + + } + + public void testToInputStreamFileWithCharsetUTF8withOtherDefaultEncoding() throws Exception { + switchToDefaultCharset("ISO-8859-1"); + File file = new File("src/test/resources/org/apache/camel/converter/german.utf-8.txt"); + InputStream in = IOConverter.toInputStream(file, "UTF-8"); + // do read with default charset! + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); try { String line = reader.readLine(); @@ -47,10 +69,11 @@ public class IOConverterCharsetTest extends ContextTestSupport { } public void testToInputStreamFileWithCharsetLatin1() throws Exception { + switchToDefaultCharset("UTF-8"); File file = new File("src/test/resources/org/apache/camel/converter/german.iso-8859-1.txt"); InputStream in = IOConverter.toInputStream(file, "ISO-8859-1"); - // need to specify the encoding of the input stream bytes - BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); + // do read with default charset! + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); BufferedReader naiveReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "ISO-8859-1")); try { String line = reader.readLine(); @@ -64,6 +87,7 @@ public class IOConverterCharsetTest extends ContextTestSupport { } public void testToInputStreamFileDirectByteDumpWithCharsetLatin1() throws Exception { + switchToDefaultCharset("UTF-8"); File file = new File("src/test/resources/org/apache/camel/converter/german.iso-8859-1.txt"); InputStream in = IOConverter.toInputStream(file, "ISO-8859-1"); InputStream naiveIn = new FileInputStream(file); @@ -109,4 +133,13 @@ public class IOConverterCharsetTest extends ContextTestSupport { } } + + private void switchToDefaultCharset(String charset) { + try { + Field defaultCharset = Charset.class.getDeclaredField("defaultCharset"); + defaultCharset.setAccessible(true); + defaultCharset.set(null, Charset.forName(charset)); + } catch (Exception e) { + } + } } http://git-wip-us.apache.org/repos/asf/camel/blob/1a4fde13/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java ---------------------------------------------------------------------- diff --git a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java index 18bb7c7..9cccf9b 100644 --- a/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java +++ b/components/camel-jsonpath/src/main/java/org/apache/camel/jsonpath/JsonPathEngine.java @@ -20,9 +20,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; - -import com.jayway.jsonpath.Configuration; -import com.jayway.jsonpath.JsonPath; +import java.nio.charset.Charset; import org.apache.camel.Exchange; import org.apache.camel.InvalidPayloadException; @@ -31,6 +29,9 @@ import org.apache.camel.WrappedFile; import org.apache.camel.component.file.GenericFile; import org.apache.camel.component.file.GenericFileConverter; +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.JsonPath; + public class JsonPathEngine { private final JsonPath path; @@ -60,7 +61,7 @@ public class JsonPathEngine { return path.read(str); } else if (json instanceof InputStream) { InputStream is = (InputStream) json; - return path.read(is); + return path.read(is, Charset.defaultCharset().displayName(), configuration); } else if (json instanceof File) { File file = (File) json; return path.read(file);