From c7c8841b2efaa4ba6f35f1b005542d5242561693 Mon Sep 17 00:00:00 2001 From: aclement Date: Mon, 4 Apr 2011 18:17:39 +0000 Subject: [PATCH] 290741: encoding option on ICompilerConfiguration --- .../pr290741/base/src/demo/Converter.java | 34 ++++++++++++++ .../pr290741/base/src/demo/ConverterTest.java | 44 +++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100755 tests/multiIncremental/pr290741/base/src/demo/Converter.java create mode 100755 tests/multiIncremental/pr290741/base/src/demo/ConverterTest.java diff --git a/tests/multiIncremental/pr290741/base/src/demo/Converter.java b/tests/multiIncremental/pr290741/base/src/demo/Converter.java new file mode 100755 index 000000000..3eb98695d --- /dev/null +++ b/tests/multiIncremental/pr290741/base/src/demo/Converter.java @@ -0,0 +1,34 @@ +package demo; + +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; + +/** + * @author oliver + * @since 18.11.2005 + */ +public class Converter { + + /** + * You may get problems using String.getBytes("UTF-8") on a non UTF-8 + * environment. Here is an alternative for it from + * http://mindprod.com/jgloss/encoding.html. + * + * @param text + * @return + * @throws UnsupportedEncodingException + */ + public static byte[] utf8encode(String text) throws UnsupportedEncodingException { + //return text.getBytes("UTF-8"); + Charset cs = Charset.forName("UTF8"); + CharBuffer ss = CharBuffer.wrap(text); + ByteBuffer bb = cs.encode(ss); + int limit = bb.limit(); // how many chars in buffer + byte[] b = new byte[ limit ]; + bb.get( b, 0 /* offset */, limit ); + return b; + } + +} diff --git a/tests/multiIncremental/pr290741/base/src/demo/ConverterTest.java b/tests/multiIncremental/pr290741/base/src/demo/ConverterTest.java new file mode 100755 index 000000000..b7a4c0093 --- /dev/null +++ b/tests/multiIncremental/pr290741/base/src/demo/ConverterTest.java @@ -0,0 +1,44 @@ +package demo; + +import java.io.OutputStreamWriter; +import java.io.UnsupportedEncodingException; + +public class ConverterTest { + + /** + * For signature we need an UTF-8 environment - don't ask my why. + * + * @since 29-Jun-06 + * @see http://mindprod.com/jgloss/encoding.html + */ +/* + String encoding = System.getProperty("file.encoding"); + String outEncoding = new OutputStreamWriter(System.out).getEncoding(); + System.out.println("file.encoding=" + encoding + " # system property"); + System.out.println("System.out encoding = " + outEncoding); + } +*/ + + public static void run() throws Exception { + /** + * Do we really get the right bytes here if the encoding is wrong? + * @throws UnsupportedEncodingException + */ + //String name = "B\u00f6hm"; // this works + String name = "Böhm"; + System.out.println("Hello, my name is Mr. " + name); + byte[] bytes = Converter.utf8encode(name); + assertEquals(66, bytes[0]); + assertEquals(-61, bytes[1]); + assertEquals(-74, bytes[2]); + assertEquals(104, bytes[3]); + assertEquals(109, bytes[4]); + } + + public static void assertEquals(int i, int b) throws Exception { + if (i!=b) { +throw new RuntimeException("different"); + } + } + +} -- 2.39.5