|
1 /* |
|
2 * Copyright (c) 2018, 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 /* |
|
25 * @test |
|
26 * @bug 4153987 6354525 |
|
27 * @summary Malformed surrogates should be handled by the converter in |
|
28 * substitution mode. |
|
29 */ |
|
30 |
|
31 import java.io.ByteArrayOutputStream; |
|
32 import java.io.OutputStream; |
|
33 import java.io.OutputStreamWriter; |
|
34 |
|
35 public class MalformedSurrogateStringTest { |
|
36 |
|
37 public static void main(String[] args) throws Exception { |
|
38 |
|
39 String fe = System.getProperty("file.encoding"); |
|
40 if ( fe.equalsIgnoreCase("UTF8") |
|
41 || fe.equalsIgnoreCase("UTF-8") |
|
42 || fe.equalsIgnoreCase("UTF_8")) |
|
43 // This test is meaningless if the default charset |
|
44 // does handle surrogates |
|
45 return; |
|
46 |
|
47 System.out.println("Testing string conversion..."); |
|
48 /* Example with malformed surrogate, and an offset */ |
|
49 String t = "abc\uD800\uDB00efgh"; |
|
50 String t2 = t.substring(2); |
|
51 byte[] b = t2.getBytes(); |
|
52 System.err.println(b.length); |
|
53 for (int i = 0; i < b.length; i++) |
|
54 System.err.println("[" + i + "]" + "=" + (char) b[i] |
|
55 + "=" + (int) b[i]); |
|
56 if (b.length != 7) { |
|
57 throw new Exception("Bad string conversion for bad surrogate"); |
|
58 } |
|
59 |
|
60 /* Example with a proper surrogate, no offset. Always worked */ |
|
61 String t3 = "abc\uD800\uDC00efgh"; |
|
62 byte[] b2 = t3.getBytes(); |
|
63 System.out.println(b2.length); |
|
64 for(int i = 0; i < b2.length; i++) |
|
65 System.err.println("[" + i + "]" + "=" + (char) b2[i]); |
|
66 if (b2.length != 8) { |
|
67 throw new Exception("Bad string conversion for good surrogate"); |
|
68 } |
|
69 |
|
70 OutputStream os = new ByteArrayOutputStream(); |
|
71 OutputStreamWriter osw = new OutputStreamWriter(os); |
|
72 System.out.println("Testing flush...."); |
|
73 /* Check for the case where the converter has a left over |
|
74 high surrogate when flush is called on the converter */ |
|
75 osw.flush(); |
|
76 String s = "abc\uD800"; // High surrogate |
|
77 char[] c = s.toCharArray(); |
|
78 osw.write(s, 0, 4); |
|
79 osw.flush(); |
|
80 |
|
81 System.out.println("Testing convert..."); |
|
82 /* Verify that all other characters go through */ |
|
83 for (int k = 1; k < 65535 ; k++) { |
|
84 osw.write("Char[" + k + "]=\"" + ((char) k) + "\""); |
|
85 } |
|
86 |
|
87 } |
|
88 } |