提交 08e3ca87 编写于 作者: A alanb

8022879: TEST_BUG: sun/nio/cs/MalformedSurrogates.java fails intermittently

Reviewed-by: martin
Contributed-by: yiming.wang@oracle.com
上级 72eaf1f6
/* /*
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -22,64 +22,132 @@ ...@@ -22,64 +22,132 @@
*/ */
/* @test /* @test
@bug 4153987 * @bug 4153987
@summary Malformed surrogates should be handled by the converter in * @summary Malformed surrogates should be handled by the converter in
substitution mode. * substitution mode.
*/ */
import java.io.*; import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.CharBuffer;
import java.nio.ByteBuffer;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.MalformedInputException;
import java.nio.charset.UnmappableCharacterException;
import java.util.SortedMap;
public class MalformedSurrogates { public class MalformedSurrogates {
public static void main(String[] args) throws Exception { private static final String PREFIX = "abc";
private static final String SUFFIX = "efgh";
private static final String MALFORMED_SURROGATE = PREFIX + "\uD800\uDB00" + SUFFIX;
private static final String NORMAL_SURROGATE = PREFIX + "\uD800\uDC00" + SUFFIX;
private static final String REVERSED_SURROGATE = PREFIX + "\uDC00\uD800" + SUFFIX;
private static final String SOLITARY_HIGH_SURROGATE = PREFIX + "\uD800" + SUFFIX;
private static final String SOLITARY_LOW_SURROGATE = PREFIX + "\uDC00" + SUFFIX;
String fe = System.getProperty("file.encoding"); public static void main(String[] args) throws IOException {
if ( fe.equalsIgnoreCase("UTF8") SortedMap<String, Charset> map = Charset.availableCharsets();
|| fe.equalsIgnoreCase("UTF-8") for (String name : map.keySet()) {
|| fe.equalsIgnoreCase("UTF_8")) Charset charset = map.get(name);
// This test is meaningless if the default charset if (charset.canEncode() && !charset.name().equals("x-COMPOUND_TEXT")) {
// does handle surrogates testNormalSurrogate(charset, NORMAL_SURROGATE);
return; testMalformedSurrogate(charset, MALFORMED_SURROGATE);
testMalformedSurrogate(charset, REVERSED_SURROGATE);
testMalformedSurrogate(charset, SOLITARY_HIGH_SURROGATE);
testMalformedSurrogate(charset, SOLITARY_LOW_SURROGATE);
testSurrogateWithReplacement(charset, NORMAL_SURROGATE);
testSurrogateWithReplacement(charset, MALFORMED_SURROGATE);
testSurrogateWithReplacement(charset, REVERSED_SURROGATE);
testSurrogateWithReplacement(charset, SOLITARY_HIGH_SURROGATE);
testSurrogateWithReplacement(charset, SOLITARY_LOW_SURROGATE);
}
}
}
public static void testMalformedSurrogate(Charset cs, String surrogate) throws IOException {
CharsetEncoder en = cs.newEncoder();
if (en.canEncode(surrogate)) {
throw new RuntimeException("testMalformedSurrogate failed with charset " + cs.name());
}
try {
en.encode(CharBuffer.wrap(surrogate));
throw new RuntimeException("Should throw MalformedInputException or UnmappableCharacterException");
} catch (MalformedInputException | UnmappableCharacterException ex) {
} finally {
en.reset();
}
System.out.println("Testing string conversion..."); try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream(), en)) {
/* Example with malformed surrogate, and an offset */ osw.write(surrogate);
String t = "abc\uD800\uDB00efgh"; throw new RuntimeException("Should throw MalformedInputException or UnmappableCharacterException");
String t2 = t.substring(2); } catch (MalformedInputException | UnmappableCharacterException ex) {
byte[] b = t2.getBytes(); }
System.err.println(b.length); }
for (int i = 0; i < b.length; i++)
System.err.println("[" + i + "]" + "=" + (char) b[i] public static void testNormalSurrogate(Charset cs, String surrogate) throws IOException {
+ "=" + (int) b[i]); CharsetEncoder en = cs.newEncoder();
if (b.length != 7) { try {
throw new Exception("Bad string conversion for bad surrogate"); en.encode(CharBuffer.wrap(surrogate));
} catch (UnmappableCharacterException ex) {
} finally {
en.reset();
} }
/* Example with a proper surrogate, no offset. Always worked */ try (OutputStreamWriter osw = new OutputStreamWriter(new ByteArrayOutputStream(), en)) {
String t3 = "abc\uD800\uDC00efgh"; osw.write(surrogate);
byte[] b2 = t3.getBytes(); } catch (UnmappableCharacterException ex) {
System.out.println(b2.length);
for(int i = 0; i < b2.length; i++)
System.err.println("[" + i + "]" + "=" + (char) b2[i]);
if (b2.length != 8) {
throw new Exception("Bad string conversion for good surrogate");
} }
}
OutputStream os = new ByteArrayOutputStream(); public static void testSurrogateWithReplacement(Charset cs, String surrogate) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(os); CharsetEncoder en = cs.newEncoder();
System.out.println("Testing flush...."); CharsetDecoder de = cs.newDecoder();
/* Check for the case where the converter has a left over if (!en.canEncode(NORMAL_SURROGATE)) {
high surrogate when flush is called on the converter */ return;
osw.flush(); }
String s = "abc\uD800"; // High surrogate String expected = null;
char[] c = s.toCharArray(); String replace = new String(en.replacement(), cs);
osw.write(s, 0, 4); switch (surrogate) {
osw.flush(); case MALFORMED_SURROGATE:
case REVERSED_SURROGATE:
expected = PREFIX + replace + replace + SUFFIX;
break;
case SOLITARY_HIGH_SURROGATE:
case SOLITARY_LOW_SURROGATE:
expected = PREFIX + replace + SUFFIX;
break;
default:
expected = NORMAL_SURROGATE;
}
System.out.println("Testing convert..."); try {
/* Verify that all other characters go through */ en.onMalformedInput(CodingErrorAction.REPLACE);
for (int k = 1; k < 65535 ; k++) { en.onUnmappableCharacter(CodingErrorAction.REPLACE);
osw.write("Char[" + k + "]=\"" + ((char) k) + "\""); ByteBuffer bbuf = en.encode(CharBuffer.wrap(surrogate));
CharBuffer cbuf = de.decode(bbuf);
if (!cbuf.toString().equals(expected)) {
throw new RuntimeException("charset " + cs.name() + " (en)decoded the surrogate " + surrogate + " to " + cbuf.toString() + " which is not same as the expected " + expected);
}
} finally {
en.reset();
de.reset();
} }
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(bos, en);) {
osw.write(surrogate);
osw.flush();
try (InputStreamReader isr = new InputStreamReader(new ByteArrayInputStream(bos.toByteArray()), de)) {
CharBuffer cbuf = CharBuffer.allocate(expected.length());
isr.read(cbuf);
cbuf.rewind();
if (!cbuf.toString().equals(expected)) {
throw new RuntimeException("charset " + cs.name() + " (en)decoded the surrogate " + surrogate + " to " + cbuf.toString() + " which is not same as the expected " + expected);
}
}
}
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册