public void testCustomStringEncoding() throws Exception { final String ENCODING = System.getProperty("file.encoding"); // Keep stuff within the extended ASCII range so we work with more // limited native encodings String UNICODE = "Un \u00e9l\u00e9ment gr\u00e2ce \u00e0 l'index"; if (!UNICODE.equals(new String(UNICODE.getBytes()))) { // If the extended characters aren't encodable in the default // encoding, punt and use straight ASCII UNICODE = ""; for (char ch = 1; ch < 128; ch++) { UNICODE += ch; } } final String UNICODEZ = UNICODE + "\0more stuff"; byte[] customEncoded = Native.getBytes(UNICODE, ENCODING); byte[] expected = UNICODE.getBytes(ENCODING); for (int i = 0; i < Math.min(customEncoded.length, expected.length); i++) { assertEquals( "Improperly encoded (" + ENCODING + ") from Java at " + i, expected[i], customEncoded[i]); } assertEquals( "Wrong number of encoded characters (" + ENCODING + ")", expected.length, customEncoded.length); String result = Native.toString(customEncoded, ENCODING); assertEquals("Improperly decoded from native bytes (" + ENCODING + ")", UNICODE, result); assertEquals( "Should truncate bytes at NUL terminator", UNICODE, Native.toString(UNICODEZ.getBytes(ENCODING), ENCODING)); }
public void testDefaultStringEncoding() throws Exception { final String UNICODE = "\u0444\u043b\u0441\u0432\u0443"; final String UNICODEZ = UNICODE + "\0more stuff"; byte[] utf8 = Native.getBytes(UNICODE); byte[] expected = UNICODE.getBytes(Native.DEFAULT_ENCODING); for (int i = 0; i < Math.min(utf8.length, expected.length); i++) { assertEquals("Improperly encoded at " + i, expected[i], utf8[i]); } assertEquals("Wrong number of encoded characters", expected.length, utf8.length); String result = Native.toString(utf8); assertEquals("Improperly decoded", UNICODE, result); assertEquals( "Should truncate bytes at NUL terminator", UNICODE, Native.toString(UNICODEZ.getBytes(Native.DEFAULT_ENCODING))); }
@Nullable public static String toStringViaUTF8(ID cfString) { if (cfString.intValue() == 0) return null; int lengthInChars = myFoundationLibrary.CFStringGetLength(cfString); int potentialLengthInBytes = 3 * lengthInChars + 1; // UTF8 fully escaped 16 bit chars, plus nul byte[] buffer = new byte[potentialLengthInBytes]; byte ok = myFoundationLibrary.CFStringGetCString( cfString, buffer, buffer.length, FoundationLibrary.kCFStringEncodingUTF8); if (ok == 0) throw new RuntimeException("Could not convert string"); return Native.toString(buffer); }
public void testWideStringConversion() { char[] buf = (getName() + NUL).toCharArray(); assertEquals("Wide C string improperly converted", getName(), Native.toString(buf)); }
public void testStringConversionWithEncoding() throws Exception { byte[] buf = (getName() + UNICODE + NUL).getBytes("utf8"); assertEquals( "Encoded C string improperly converted", getName() + UNICODE, Native.toString(buf, "utf8")); }
public void testStringConversion() { byte[] buf = (getName() + NUL).getBytes(); assertEquals("C string improperly converted", getName(), Native.toString(buf)); }
public void testByteArrayToString() { byte[] buf = {'a', 'b', 'c', '\0', 'd', 'e'}; assertEquals("Wrong String generated", "abc", Native.toString(buf)); }