Path: blob/master/test/jdk/sun/nio/cs/MalformedSurrogateStringTest.java
41149 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4153987 635452526* @summary Malformed surrogates should be handled by the converter in27* substitution mode.28*/2930import java.io.ByteArrayOutputStream;31import java.io.OutputStream;32import java.io.OutputStreamWriter;3334public class MalformedSurrogateStringTest {3536public static void main(String[] args) throws Exception {3738String fe = System.getProperty("file.encoding");39if ( fe.equalsIgnoreCase("UTF8")40|| fe.equalsIgnoreCase("UTF-8")41|| fe.equalsIgnoreCase("UTF_8"))42// This test is meaningless if the default charset43// does handle surrogates44return;4546System.out.println("Testing string conversion...");47/* Example with malformed surrogate, and an offset */48String t = "abc\uD800\uDB00efgh";49String t2 = t.substring(2);50byte[] b = t2.getBytes();51System.err.println(b.length);52for (int i = 0; i < b.length; i++)53System.err.println("[" + i + "]" + "=" + (char) b[i]54+ "=" + (int) b[i]);55if (b.length != 7) {56throw new Exception("Bad string conversion for bad surrogate");57}5859/* Example with a proper surrogate, no offset. Always worked */60String t3 = "abc\uD800\uDC00efgh";61byte[] b2 = t3.getBytes();62System.out.println(b2.length);63for(int i = 0; i < b2.length; i++)64System.err.println("[" + i + "]" + "=" + (char) b2[i]);65if (b2.length != 8) {66throw new Exception("Bad string conversion for good surrogate");67}6869OutputStream os = new ByteArrayOutputStream();70OutputStreamWriter osw = new OutputStreamWriter(os);71System.out.println("Testing flush....");72/* Check for the case where the converter has a left over73high surrogate when flush is called on the converter */74osw.flush();75String s = "abc\uD800"; // High surrogate76char[] c = s.toCharArray();77osw.write(s, 0, 4);78osw.flush();7980System.out.println("Testing convert...");81/* Verify that all other characters go through */82for (int k = 1; k < 65535 ; k++) {83osw.write("Char[" + k + "]=\"" + ((char) k) + "\"");84}8586}87}888990