import java.io.*; import java.nio.charset.*; public class StringBreaker { public static void main( String [] args ) { PrintStream p = System.out; try { // UTF-8 works, and this is how the others SHOULD work too. byte utf8_bytes[] = "Moo cow".getBytes( StandardCharsets.UTF_8 ); String UTF8_substring = new String( utf8_bytes, 4, 3, StandardCharsets.UTF_8 ); p.println( "UTF8_substring is " + UTF8_substring ); // prints "cow" // UTF-16BE fails byte utf16be_bytes[] = "Moo cow".getBytes( StandardCharsets.UTF_16BE ); String UTF16BE_substring = new String( utf16be_bytes, 4, 3, StandardCharsets.UTF_16BE ); // substring now holds the letter 'o' with visible garbage after it. p.println( "UTF16BE_substring is " + UTF16BE_substring ); // UTF-16LE fails byte utf16le_bytes[] = "Moo cow".getBytes( StandardCharsets.UTF_16LE ); String UTF16LE_substring = new String( utf16le_bytes, 4, 3, StandardCharsets.UTF_16LE ); // substring now holds the letter 'o' with visible garbage after it. p.println( "UTF16LE_substring is " + UTF16LE_substring ); p.println( "The constructors that convert the whole byte array work fine. Only the substring constructors are broken." ); p.println( "UTF-16BE bytes printable: " + new String( utf16be_bytes, StandardCharsets.UTF_16BE ) ); p.println( "UTF-16LE bytes printable: " + new String( utf16le_bytes, StandardCharsets.UTF_16LE ) ); } catch( Exception e ) { p.println( "ERROR: Bad charset string." ); System.exit(1); } } }