1 /* 2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 @bug 4153987 26 @summary Malformed surrogates should be handled by the converter in 27 substitution mode. 28 */ 29 30 import java.io.*; 31 32 public class MalformedSurrogates { 33 34 public static void main(String[] args) throws Exception { 35 36 String fe = System.getProperty("file.encoding"); 37 if ( fe.equalsIgnoreCase("UTF8") 38 || fe.equalsIgnoreCase("UTF-8") 39 || fe.equalsIgnoreCase("UTF_8")) 40 // This test is meaningless if the default charset 41 // does handle surrogates 42 return; 43 44 System.out.println("Testing string conversion..."); 45 /* Example with malformed surrogate, and an offset */ 46 String t = "abc\uD800\uDB00efgh"; 47 String t2 = t.substring(2); 48 byte[] b = t2.getBytes(); 49 System.err.println(b.length); 50 for (int i = 0; i < b.length; i++) 51 System.err.println("[" + i + "]" + "=" + (char) b[i] 52 + "=" + (int) b[i]); 53 if (b.length != 7) { 54 throw new Exception("Bad string conversion for bad surrogate"); 55 } 56 57 /* Example with a proper surrogate, no offset. Always worked */ 58 String t3 = "abc\uD800\uDC00efgh"; 59 byte[] b2 = t3.getBytes(); 60 System.out.println(b2.length); 61 for(int i = 0; i < b2.length; i++) 62 System.err.println("[" + i + "]" + "=" + (char) b2[i]); 63 if (b2.length != 8) { 64 throw new Exception("Bad string conversion for good surrogate"); 65 } 66 67 OutputStream os = new ByteArrayOutputStream(); 68 OutputStreamWriter osw = new OutputStreamWriter(os); 69 System.out.println("Testing flush...."); 70 /* Check for the case where the converter has a left over 71 high surrogate when flush is called on the converter */ 72 osw.flush(); 73 String s = "abc\uD800"; // High surrogate 74 char[] c = s.toCharArray(); 75 osw.write(s, 0, 4); 76 osw.flush(); 77 78 System.out.println("Testing convert..."); 79 /* Verify that all other characters go through */ 80 for (int k = 1; k < 65535 ; k++) { 81 osw.write("Char[" + k + "]=\"" + ((char) k) + "\""); 82 } 83 84 } 85 }