Path: blob/master/test/jdk/sun/nio/cs/MalformedSurrogates.java
41149 views
/*1* Copyright (c) 2002, 2013, 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/* @test24* @bug 415398725* @summary Malformed surrogates should be handled by the converter in26* substitution mode.27*/28import java.io.*;29import java.nio.charset.Charset;30import java.nio.charset.CharsetDecoder;31import java.nio.charset.CharsetEncoder;32import java.nio.CharBuffer;33import java.nio.ByteBuffer;34import java.nio.charset.CodingErrorAction;35import java.nio.charset.MalformedInputException;36import java.nio.charset.UnmappableCharacterException;37import java.util.SortedMap;3839public class MalformedSurrogates {4041private static final String PREFIX = "abc";42private static final String SUFFIX = "efgh";43private static final String MALFORMED_SURROGATE = PREFIX + "\uD800\uDB00" + SUFFIX;44private static final String NORMAL_SURROGATE = PREFIX + "\uD800\uDC00" + SUFFIX;45private static final String REVERSED_SURROGATE = PREFIX + "\uDC00\uD800" + SUFFIX;46private static final String SOLITARY_HIGH_SURROGATE = PREFIX + "\uD800" + SUFFIX;47private static final String SOLITARY_LOW_SURROGATE = PREFIX + "\uDC00" + SUFFIX;4849public static void main(String[] args) throws IOException {50SortedMap<String, Charset> map = Charset.availableCharsets();51for (String name : map.keySet()) {52Charset charset = map.get(name);53if (charset.canEncode() && !charset.name().equals("x-COMPOUND_TEXT")) {54testNormalSurrogate(charset, NORMAL_SURROGATE);55testMalformedSurrogate(charset, MALFORMED_SURROGATE);56testMalformedSurrogate(charset, REVERSED_SURROGATE);57testMalformedSurrogate(charset, SOLITARY_HIGH_SURROGATE);58testMalformedSurrogate(charset, SOLITARY_LOW_SURROGATE);59testSurrogateWithReplacement(charset, NORMAL_SURROGATE);60testSurrogateWithReplacement(charset, MALFORMED_SURROGATE);61testSurrogateWithReplacement(charset, REVERSED_SURROGATE);62testSurrogateWithReplacement(charset, SOLITARY_HIGH_SURROGATE);63testSurrogateWithReplacement(charset, SOLITARY_LOW_SURROGATE);64}65}66}6768public static void testMalformedSurrogate(Charset cs, String surrogate) throws IOException {69CharsetEncoder en = cs.newEncoder();70if (en.canEncode(surrogate)) {71throw new RuntimeException("testMalformedSurrogate failed with charset " + cs.name());72}7374try {75en.encode(CharBuffer.wrap(surrogate));76throw new RuntimeException("Should throw MalformedInputException or UnmappableCharacterException");77} catch (MalformedInputException | UnmappableCharacterException ex) {78} finally {79en.reset();80}8182try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream(), en)) {83osw.write(surrogate);84throw new RuntimeException("Should throw MalformedInputException or UnmappableCharacterException");85} catch (MalformedInputException | UnmappableCharacterException ex) {86}87}8889public static void testNormalSurrogate(Charset cs, String surrogate) throws IOException {90CharsetEncoder en = cs.newEncoder();91try {92en.encode(CharBuffer.wrap(surrogate));93} catch (UnmappableCharacterException ex) {94} finally {95en.reset();96}9798try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream(), en)) {99osw.write(surrogate);100} catch (UnmappableCharacterException ex) {101}102}103104public static void testSurrogateWithReplacement(Charset cs, String surrogate) throws IOException {105CharsetEncoder en = cs.newEncoder();106CharsetDecoder de = cs.newDecoder();107if (!en.canEncode(NORMAL_SURROGATE)) {108return;109}110String expected = null;111String replace = new String(en.replacement(), cs);112switch (surrogate) {113case MALFORMED_SURROGATE:114case REVERSED_SURROGATE:115expected = PREFIX + replace + replace + SUFFIX;116break;117case SOLITARY_HIGH_SURROGATE:118case SOLITARY_LOW_SURROGATE:119expected = PREFIX + replace + SUFFIX;120break;121default:122expected = NORMAL_SURROGATE;123}124125try {126en.onMalformedInput(CodingErrorAction.REPLACE);127en.onUnmappableCharacter(CodingErrorAction.REPLACE);128ByteBuffer bbuf = en.encode(CharBuffer.wrap(surrogate));129CharBuffer cbuf = de.decode(bbuf);130if (!cbuf.toString().equals(expected)) {131throw new RuntimeException("charset " + cs.name() + " (en)decoded the surrogate " + surrogate + " to " + cbuf.toString() + " which is not same as the expected " + expected);132}133} finally {134en.reset();135de.reset();136}137138try (ByteArrayOutputStream bos = new ByteArrayOutputStream();139OutputStreamWriter osw = new OutputStreamWriter(bos, en);) {140osw.write(surrogate);141osw.flush();142try (InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(bos.toByteArray()), de)) {143CharBuffer cbuf = CharBuffer.allocate(expected.length());144isr.read(cbuf);145cbuf.rewind();146if (!cbuf.toString().equals(expected)) {147throw new RuntimeException("charset " + cs.name() + " (en)decoded the surrogate " + surrogate + " to " + cbuf.toString() + " which is not same as the expected " + expected);148}149}150}151}152}153154155