提交 dca5d60a 编写于 作者: S sherman

6675856: Open charset tests

Summary: Moved non-confidiential test cased from closed repo to open repo
Reviewed-by: martin
上级 d05cedcc
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4834154
@summary Decode a file using EUC-TW, test for decode errors
*/
/*
* Tests for decode errors in NIO EUC-TW decoder. 4734607 details
* decoding errors which occur when the input file > 8k in size
* and contains numerous US-ASCII range chars
*/
import java.io.*;
public class BufferUnderflowEUCTWTest {
private static int BUFFERSIZE = 8194;
public static void main (String[] args) throws Exception {
int i = 0;
byte[] b = new byte[BUFFERSIZE];
for (; i < BUFFERSIZE - 4; i++) // pad with zeroes
b[i] = 0;
// Overspill a valid EUC-TW 4 byte sequence between 2
// successive input buffers.
b[i++] = (byte)0x8E;
b[i++] = (byte)0xA2;
b[i++] = (byte)0xA1;
b[i++] = (byte)0xA6;
ByteArrayInputStream r = new ByteArrayInputStream(b);
try {
InputStreamReader isr=new InputStreamReader(r, "EUC-TW");
char[] cc = new char[BUFFERSIZE];
int cx = isr.read(cc);
} catch (ArrayIndexOutOfBoundsException e) {
throw new Exception("Array Index error: bug 4834154");
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
* @bug 4216191 4721369 4807283
@summary Test to validate case insensitivity of encoding alias names
*/
// Fixed since 1.4.0 by virtue of NIO charset lookup mechanism
// which is by design case insensitive
import java.lang.*;
import java.io.*;
public class CheckCaseInsensitiveEncAliases
{
public static void main(String args[]) throws Exception
{
// Try various encoding names in mixed cases
// Tests subset of encoding names provided within bugID 4216191
// Various forms of US-ASCII
tryToEncode( "ANSI_X3.4-1968" );
tryToEncode( "iso-ir-6" );
tryToEncode( "ANSI_X3.4-1986" );
tryToEncode( "ISO_646.irv:1991" );
tryToEncode( "ASCII" );
tryToEncode( "ascii" );
tryToEncode( "Ascii" );
tryToEncode( "Ascii7" );
tryToEncode( "ascii7" );
tryToEncode( "ISO646-US" );
tryToEncode( "US-ASCII" );
tryToEncode( "us-ascii" );
tryToEncode( "US-Ascii" );
tryToEncode( "us" );
tryToEncode( "IBM367" );
tryToEncode( "cp367" );
tryToEncode( "csASCII" );
// Variants on Unicode
tryToEncode( "Unicode" );
tryToEncode( "UNICODE" );
tryToEncode( "unicode" );
// Variants on Big5
tryToEncode( "Big5" );
tryToEncode( "big5" );
tryToEncode( "bIg5" );
tryToEncode( "biG5" );
tryToEncode( "bIG5" );
// Variants of Cp1252
tryToEncode( "Cp1252" );
tryToEncode( "cp1252" );
tryToEncode( "CP1252" );
// Variants of PCK
tryToEncode( "pck" );
tryToEncode( "Pck" );
}
public static final String ENCODE_STRING = "Encode me";
public static void tryToEncode( String encoding) throws Exception
{
try
{
byte[] bytes = ENCODE_STRING.getBytes( encoding );
System.out.println( "Encoding \"" + encoding + "\" recognized" );
}
catch( UnsupportedEncodingException e )
{
throw new Exception("Encoding \"" + encoding + "\" NOT recognized");
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4513767 4961027
@summary Checks canonical names match between old and (NIO) core charsets
*/
import java.io.InputStreamReader;
import java.io.IOException;
public class CheckHistoricalNames {
static int failed = 0;
public static void main (String[] args) throws Exception {
checkHistoricalName("ASCII");
checkHistoricalName("Cp1252");
checkHistoricalName("ISO8859_1");
checkHistoricalName("UnicodeBigUnmarked");
checkHistoricalName("UnicodeLittle");
checkHistoricalName("UnicodeLittleUnmarked");
checkHistoricalName("UTF8");
checkHistoricalName("UTF-16");
checkMappedName("UnicodeBig", "UTF-16");
checkMappedName("US-ASCII", "ASCII");
checkMappedName("ISO-8859-1", "ISO8859_1");
checkMappedName("UTF-8", "UTF8");
checkMappedName("UTF-16BE", "UnicodeBigUnmarked");
checkMappedName("UTF-16LE", "UnicodeLittleUnmarked");
checkHistoricalName("ISO8859_2");
checkHistoricalName("ISO8859_4");
checkHistoricalName("ISO8859_5");
checkHistoricalName("ISO8859_7");
checkHistoricalName("ISO8859_9");
checkHistoricalName("ISO8859_13");
checkHistoricalName("KOI8_R");
checkHistoricalName("Cp1250");
checkHistoricalName("Cp1251");
checkHistoricalName("Cp1253");
checkHistoricalName("Cp1254");
checkHistoricalName("Cp1257");
checkMappedName("ISO-8859-2", "ISO8859_2");
checkMappedName("ISO-8859-4", "ISO8859_4");
checkMappedName("ISO-8859-5", "ISO8859_5");
checkMappedName("ISO-8859-7", "ISO8859_7");
checkMappedName("ISO-8859-9", "ISO8859_9");
checkMappedName("ISO-8859-13", "ISO8859_13");
checkMappedName("KOI8-R", "KOI8_R");
checkMappedName("windows-1250", "Cp1250");
checkMappedName("windows-1251","Cp1251");
checkMappedName("windows-1253", "Cp1253");
checkMappedName("windows-1254", "Cp1254");
checkMappedName("windows-1257", "Cp1257");
checkHistoricalName("EUC_CN");
checkHistoricalName("EUC_JP");
checkHistoricalName("EUC_JP_LINUX");
checkHistoricalName("EUC_KR");
checkHistoricalName("EUC_TW");
checkHistoricalName("ISO2022CN");
checkHistoricalName("ISO2022JP");
checkHistoricalName("ISO2022KR");
checkHistoricalName("ISO8859_3");
checkHistoricalName("ISO8859_6");
checkHistoricalName("ISO8859_8");
checkHistoricalName("Cp1255");
checkHistoricalName("Cp1256");
checkHistoricalName("Cp1258");
checkHistoricalName("MS936");
checkHistoricalName("MS949");
checkHistoricalName("MS950");
checkHistoricalName("TIS620");
checkMappedName("EUC-CN", "EUC_CN");
checkMappedName("EUC-JP", "EUC_JP");
checkMappedName("EUC-JP-LINUX", "EUC_JP_LINUX");
checkMappedName("EUC-TW", "EUC_TW");
checkMappedName("EUC-KR", "EUC_KR");
checkMappedName("ISO-2022-CN", "ISO2022CN");
checkMappedName("ISO-2022-JP", "ISO2022JP");
checkMappedName("ISO-2022-KR", "ISO2022KR");
checkMappedName("ISO-8859-3", "ISO8859_3");
checkMappedName("ISO-8859-6", "ISO8859_6");
checkMappedName("ISO-8859-8", "ISO8859_8");
checkMappedName("windows-1255", "Cp1255");
checkMappedName("windows-1256", "Cp1256");
checkMappedName("windows-1258", "Cp1258");
checkMappedName("windows-936", "GBK");
checkMappedName("windows-949", "MS949");
checkMappedName("windows-950", "MS950");
checkMappedName("x-MS950-HKSCS", "MS950_HKSCS");
checkMappedName("x-PCK", "PCK");
checkMappedName("Shift_JIS", "SJIS");
checkMappedName("x-JISAutoDetect", "JISAutoDetect");
checkMappedName("TIS-620", "TIS620");
checkMappedName("x-Big5-Solaris", "Big5_Solaris");
checkHistoricalName("Cp037");
checkHistoricalName("Cp1006");
checkHistoricalName("Cp1025");
checkHistoricalName("Cp1026");
checkHistoricalName("Cp1046");
checkHistoricalName("Cp1047");
checkHistoricalName("Cp1097");
checkHistoricalName("Cp1098");
checkHistoricalName("Cp1112");
checkHistoricalName("Cp1122");
checkHistoricalName("Cp1123");
checkHistoricalName("Cp1124");
checkHistoricalName("Cp1140");
checkHistoricalName("Cp1141");
checkHistoricalName("Cp1142");
checkHistoricalName("Cp1143");
checkHistoricalName("Cp1144");
checkHistoricalName("Cp1145");
checkHistoricalName("Cp1146");
checkHistoricalName("Cp1147");
checkHistoricalName("Cp1148");
checkHistoricalName("Cp1149");
checkHistoricalName("Cp1381");
checkHistoricalName("Cp1383");
checkHistoricalName("Cp273");
checkHistoricalName("Cp277");
checkHistoricalName("Cp278");
checkHistoricalName("Cp280");
checkHistoricalName("Cp284");
checkHistoricalName("Cp285");
checkHistoricalName("Cp297");
checkHistoricalName("Cp33722");
checkHistoricalName("Cp420");
checkHistoricalName("Cp424");
checkHistoricalName("Cp437");
checkHistoricalName("Cp500");
checkHistoricalName("Cp737");
checkHistoricalName("Cp775");
checkHistoricalName("Cp838");
checkHistoricalName("Cp850");
checkHistoricalName("Cp852");
checkHistoricalName("Cp855");
checkHistoricalName("Cp856");
checkHistoricalName("Cp857");
checkHistoricalName("Cp858");
checkHistoricalName("Cp860");
checkHistoricalName("Cp861");
checkHistoricalName("Cp862");
checkHistoricalName("Cp863");
checkHistoricalName("Cp864");
checkHistoricalName("Cp865");
checkHistoricalName("Cp866");
checkHistoricalName("Cp868");
checkHistoricalName("Cp869");
checkHistoricalName("Cp870");
checkHistoricalName("Cp871");
checkHistoricalName("Cp874");
checkHistoricalName("Cp875");
checkHistoricalName("Cp918");
checkHistoricalName("Cp921");
checkHistoricalName("Cp922");
checkHistoricalName("Cp933");
checkHistoricalName("Cp939");
checkHistoricalName("Cp949");
checkHistoricalName("Cp964");
checkHistoricalName("Cp970");
checkMappedName("IBM037", "Cp037");
checkMappedName("IBM1006", "Cp1006");
checkMappedName("IBM1025", "Cp1025");
checkMappedName("IBM1026", "Cp1026");
checkMappedName("x-IBM1046", "Cp1046");
checkMappedName("IBM1047", "Cp1047");
checkMappedName("IBM1097", "Cp1097");
checkMappedName("IBM1098", "Cp1098");
checkMappedName("IBM1112", "Cp1112");
checkMappedName("IBM1122", "Cp1122");
checkMappedName("IBM1123", "Cp1123");
checkMappedName("IBM1124", "Cp1124");
checkMappedName("IBM01140", "Cp1140");
checkMappedName("IBM01141", "Cp1141");
checkMappedName("IBM01142", "Cp1142");
checkMappedName("IBM01143", "Cp1143");
checkMappedName("IBM01144", "Cp1144");
checkMappedName("IBM01145", "Cp1145");
checkMappedName("IBM01146", "Cp1146");
checkMappedName("IBM01147", "Cp1147");
checkMappedName("IBM01148", "Cp1148");
checkMappedName("IBM01149", "Cp1149");
checkMappedName("IBM1381", "Cp1381");
checkMappedName("IBM1383", "Cp1383");
checkMappedName("IBM273", "Cp273");
checkMappedName("IBM277", "Cp277");
checkMappedName("IBM278", "Cp278");
checkMappedName("IBM280", "Cp280");
checkMappedName("IBM284", "Cp284");
checkMappedName("IBM285", "Cp285");
checkMappedName("IBM297", "Cp297");
checkMappedName("IBM33722", "Cp33722");
checkMappedName("IBM420", "Cp420");
checkMappedName("IBM424", "Cp424");
checkMappedName("IBM437", "Cp437");
checkMappedName("IBM500", "Cp500");
checkMappedName("IBM737", "Cp737");
checkMappedName("IBM775", "Cp775");
checkMappedName("IBM838", "Cp838");
checkMappedName("IBM850", "Cp850");
checkMappedName("IBM852", "Cp852");
checkMappedName("IBM855", "Cp855");
checkMappedName("IBM856", "Cp856");
checkMappedName("IBM857", "Cp857");
checkMappedName("IBM00858", "Cp858");
checkMappedName("IBM860", "Cp860");
checkMappedName("IBM861", "Cp861");
checkMappedName("IBM862", "Cp862");
checkMappedName("IBM863", "Cp863");
checkMappedName("IBM864", "Cp864");
checkMappedName("IBM865", "Cp865");
checkMappedName("IBM866", "Cp866");
checkMappedName("IBM868", "Cp868");
checkMappedName("IBM869", "Cp869");
checkMappedName("IBM870", "Cp870");
checkMappedName("IBM871", "Cp871");
checkMappedName("IBM874", "Cp874");
checkMappedName("IBM875", "Cp875");
checkMappedName("IBM918", "Cp918");
checkMappedName("IBM921", "Cp921");
checkMappedName("IBM922", "Cp922");
checkMappedName("x-IBM930", "Cp930");
checkMappedName("IBM933", "Cp933");
checkMappedName("x-IBM935", "Cp935");
checkMappedName("x-IBM937", "Cp937");
checkMappedName("IBM939", "Cp939");
checkMappedName("x-IBM942", "Cp942");
checkMappedName("x-IBM942C", "Cp942C");
checkMappedName("x-IBM943", "Cp943");
checkMappedName("x-IBM943C", "Cp943C");
checkMappedName("x-IBM948", "Cp948");
checkMappedName("IBM949", "Cp949");
checkMappedName("x-IBM949C", "Cp949C");
checkMappedName("x-IBM950", "Cp950");
checkMappedName("IBM964", "Cp964");
checkMappedName("IBM970", "Cp970");
checkHistoricalName("MacArabic");
checkHistoricalName("MacCentralEurope");
checkHistoricalName("MacCroatian");
checkHistoricalName("MacCyrillic");
checkHistoricalName("MacDingbat");
checkHistoricalName("MacGreek");
checkHistoricalName("MacHebrew");
checkHistoricalName("MacIceland");
checkHistoricalName("MacRoman");
checkHistoricalName("MacRomania");
checkHistoricalName("MacSymbol");
checkHistoricalName("MacThai");
checkHistoricalName("MacTurkish");
checkHistoricalName("MacUkraine");
checkMappedName("x-MacArabic", "MacArabic");
checkMappedName("x-MacCentralEurope", "MacCentralEurope");
checkMappedName("x-MacCroatian", "MacCroatian");
checkMappedName("x-MacCyrillic", "MacCyrillic");
checkMappedName("x-MacDingbat", "MacDingbat");
checkMappedName("x-MacGreek", "MacGreek");
checkMappedName("x-MacHebrew", "MacHebrew");
checkMappedName("x-MacIceland", "MacIceland");
checkMappedName("x-MacRoman", "MacRoman");
checkMappedName("x-MacRomania", "MacRomania");
checkMappedName("x-MacSymbol", "MacSymbol");
checkMappedName("x-MacThai", "MacThai");
checkMappedName("x-MacTurkish", "MacTurkish");
checkMappedName("x-MacUkraine", "MacUkraine");
if (failed != 0)
throw new Exception("Test Failed: " + failed);
else
System.out.println("Test Passed!");
}
private static void checkHistoricalName(String name) throws Exception {
checkMappedName(name, name);
}
private static void checkMappedName(String alias, String canonical)
throws Exception {
InputStreamReader reader = new InputStreamReader(System.in, alias);
if (!reader.getEncoding().equals(canonical)) {
System.out.println("Failed canonical names : mismatch for " + alias
+ " - expected " + canonical
+ ", got " + reader.getEncoding());
failed++;
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4094987
@summary Verify that malformed expression exceptions are thrown
but no internal errors in certain pathologial cases.
*/
import java.io.*;
import java.nio.charset.*;
public class ConvertSingle {
public static void main(String args[]) throws Exception {
// This conversion is pathologically bad - it is attempting to
// read unicode from an ascii encoded string.
// The orignal bug: A internal error in ISR results if the
// byte counter in ByteToCharUnicode
// is not advanced as the input is consumed.
try{
String s = "\n";
byte ss[] = null;
String sstring = "x";
ss = s.getBytes();
ByteArrayInputStream BAIS = new ByteArrayInputStream(ss);
InputStreamReader ISR = new InputStreamReader(BAIS, "Unicode");
BufferedReader BR = new BufferedReader(ISR);
sstring = BR.readLine();
BR.close();
System.out.println(sstring);
} catch (MalformedInputException e){
// Right error
return;
} catch (java.lang.InternalError e) {
throw new Exception("ByteToCharUnicode is failing incorrectly for "
+ " single byte input");
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**/
public class Decode {
private static boolean isAscii(char c) {
return c < '\u0080';
}
private static boolean isPrintable(char c) {
return ('\u0020' < c) && (c < '\u007f');
}
public static void main(String[] args) throws Throwable {
if (args.length < 2)
throw new Exception("Usage: java Decode CHARSET BYTE [BYTE ...]");
String cs = args[0];
byte[] bytes = new byte[args.length-1];
for (int i = 1; i < args.length; i++) {
String arg = args[i];
bytes[i-1] =
(arg.length() == 1 && isAscii(arg.charAt(0))) ?
(byte) arg.charAt(0) :
arg.equals("ESC") ? 0x1b :
arg.equals("SO") ? 0x0e :
arg.equals("SI") ? 0x0f :
arg.equals("SS2") ? (byte) 0x8e :
arg.equals("SS3") ? (byte) 0x8f :
arg.matches("0x.*") ? Integer.decode(arg).byteValue() :
Integer.decode("0x"+arg).byteValue();
}
String s = new String(bytes, cs);
for (int j = 0; j < s.length(); j++) {
if (j > 0)
System.out.print(' ');
char c = s.charAt(j);
if (isPrintable(c))
System.out.print(c);
else if (c == '\u001b') System.out.print("ESC");
else
System.out.printf("\\u%04x", (int) c);
}
System.out.print("\n");
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 5101128
@summary Check behavior of CharsetDecoder.decode when overflow occurs
@author Martin Buchholz
*/
import java.util.*;
import java.nio.*;
import java.nio.charset.*;
public class DecoderOverflow {
static int failures = 0;
public static void main(String[] args) throws Exception {
for (String csn : Charset.availableCharsets().keySet()) {
try {
test(csn);
} catch (Throwable t) {
System.out.println(csn);
t.printStackTrace();
failures++;
}
}
if (failures > 0)
throw new Exception(failures + " charsets failed");
}
static void test(String encoding) throws Exception {
String text = "Vote for Duke!";
Charset cs = Charset.forName(encoding);
if (! cs.canEncode() || ! cs.newEncoder().canEncode('.')) return;
ByteBuffer in = ByteBuffer.wrap(text.getBytes(encoding));
CharBuffer out = CharBuffer.allocate(text.length()/2);
CoderResult result = cs.newDecoder().decode(in, out, true);
if (out.hasRemaining() || ! result.isOverflow())
throw new Exception
("out.hasRemaining()=" + out.hasRemaining() +
" result.isOverflow()=" + result.isOverflow() +
" in.capacity()=" + in.capacity() +
" encoding=" + encoding);
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4867457
@summary Check for correct byte buffer underflow handling in EUC-JP
*/
import java.io.*;
import java.nio.*;
import java.nio.charset.*;
public class EUCJPUnderflowDecodeTest {
public static void main(String[] args) throws Exception{
ByteBuffer bb = ByteBuffer.allocateDirect(255);
CharBuffer cc = CharBuffer.allocate(255);
// Test both regular EUC-JP and Linux variant
String[] charsetNames = { "EUC_JP", "EUC-JP-LINUX" };
for (int i = 0 ; i < charsetNames.length; i++) {
Charset cs = Charset.forName(charsetNames[i]);
CharsetDecoder decoder = cs.newDecoder();
bb.clear();
cc.clear();
// Fakes a partial 3 byte EUC_JP (JIS-X-0212 range)
// encoded character/byte sequence
bb.put((byte)0x8f);
bb.put((byte)0xa2);
bb.flip();
// Now decode with endOfInput method param set to
// indicate to decoder that there is more encoded
// data to follow in a subsequent invocation
CoderResult result = decoder.decode(bb, cc, false);
// java.nio.charset.CharsetDecoder spec specifies
// that the coder ought to return CoderResult.UNDERFLOW
// when insufficient bytes have been supplied to complete
// the decoding operation
if (result != CoderResult.UNDERFLOW) {
throw new Exception("test failed - UNDERFLOW not returned");
}
// Repeat the test with the lead byte (minus its pursuing
// trail byte) for the EUC-JP 2 byte (JIS208) range
decoder.reset();
bb.clear();
cc.clear();
bb.put((byte)0xa1);
bb.flip();
result = decoder.decode(bb, cc, false);
if (result != CoderResult.UNDERFLOW) {
throw new Exception("test failed");
}
// finally ensure that a valid JIS208 range EUC-JP
// 2 byte value is correctly decoded when it is presented
// at the trailing bounds of a ByteBuffer in the case where
// charset decoder expects (endOfInput ==false) more
//input to follow
decoder.reset();
bb.clear();
cc.clear();
bb.put((byte)0xa1);
bb.put((byte)0xc0);
bb.flip();
result = decoder.decode(bb, cc, false);
cc.flip();
if (result != CoderResult.UNDERFLOW && cc.get() != '\uFF3c') {
throw new Exception("test failed to decode EUC-JP (0xA1C0)");
}
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6350021
* @summary Consistency checks when input buffer contains JISX0212 characters
* @author Martin Buchholz
*/
import java.io.*;
import java.util.*;
import java.nio.*;
import java.nio.charset.*;
public class EucJpLinux0212 {
private static void equal(CharBuffer b1, CharBuffer b2) {
equal(b1.position(), b2.position());
equal(b1.limit(), b2.limit());
System.out.printf("positions=%d %d%n", b1.position(), b2.position());
System.out.printf("limits=%d %d%n", b1.limit(), b2.limit());
for (int i = b1.position(); i < b1.limit(); i++)
equal((int)b1.get(i), (int)b2.get(i));
}
private static void realMain(String[] args) throws Throwable {
List<ByteBuffer> bbs = Arrays.asList(
ByteBuffer.allocate(10),
ByteBuffer.allocateDirect(10));
List<CharBuffer> cbs = new ArrayList<CharBuffer>();
for (ByteBuffer bb : bbs) {
bb.put(new byte[]{ (byte)0x8f, 0x01, 0x02,
(byte)0xa1, (byte)0xc0,
0x02, 0x03});
bb.flip();
CharsetDecoder decoder = Charset.forName("EUC_JP_LINUX").newDecoder();
decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
CharBuffer cb = decoder.decode(bb);
cbs.add(cb);
}
equal(cbs.get(0), cbs.get(1));
}
//--------------------- Infrastructure ---------------------------
static volatile int passed = 0, failed = 0;
static void pass() {passed++;}
static void fail() {failed++; Thread.dumpStack();}
static void fail(String msg) {System.out.println(msg); fail();}
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
static void check(boolean cond) {if (cond) pass(); else fail();}
static void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
try {realMain(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 5016049
@summary ensure euc-jp-linux charset decoder recovery for unmappable input
*/
import java.io.*;
public class EucJpLinuxDecoderRecoveryTest {
public static void main(String[] args) throws Exception {
byte[] encoded = {
// EUC_JP_LINUX mappable JIS X 0208 range
(byte)0xa6, (byte)0xc5,
// EUC_JP_LINUX Unmappable (JIS X 0212 range)
(byte)0x8f, (byte)0xa2, (byte)0xb7,
// EUC_JP_LINUX mappable JIS X 0208 range
(byte)0xa6, (byte)0xc7 };
char[] decodedChars = new char[3];
char[] expectedChars =
{
'\u03B5', // mapped
'\ufffd', // unmapped
'\u03B7' // mapped
};
ByteArrayInputStream bais = new ByteArrayInputStream(encoded);
InputStreamReader isr = new InputStreamReader(bais, "EUC_JP_LINUX");
int n = 0; // number of chars decoded
try {
n = isr.read(decodedChars);
} catch (Exception ex) {
throw new Error("euc-jp-linux decoding broken");
}
// check number of decoded chars is what is expected
if (n != expectedChars.length)
throw new Error("Unexpected number of chars decoded");
// Compare actual decoded with expected
for (int i = 0; i < n; i++) {
if (expectedChars[i] != decodedChars[i])
throw new Error("euc-jp-linux decoding incorrect");
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/**
* @test
* @bug 4114080
* @summary Make sure the euro converters, which are derived from
* existing converters, only differ from their parents at the expected
* code point.
*/
import java.text.*;
import java.util.*;
import java.io.*;
/* Author: Alan Liu
* 7/14/98
*/
public class EuroConverter {
public static void main(String args[]) throws Exception {
boolean pass = true;
char[] map = new char[256]; // map for the encoding
byte[] bytes = new byte[1]; // scratch
char[] chars = new char[1]; // scratch
for (int i=0; i<DATA.length; ) {
String euroEnc = DATA[i++];
String parentEnc = DATA[i++];
System.out.println("Checking encoder " + euroEnc + " against " + parentEnc);
String currentEnc = parentEnc;
try {
// Fill map with parent values
for (int j=-128; j<128; ++j) {
bytes[0] = (byte)j;
char parentValue = new String(bytes, parentEnc).charAt(0);
// NOTE: 0x25 doesn't round trip on the EBCDIC code pages,
// so we don't check that code point in the sanity check.
if (j != 0x0025) {
chars[0] = parentValue;
int parentRoundTrip = new String(chars).getBytes(parentEnc)[0];
// This is a sanity check -- we aren't really testing the parent
// encoder here.
if (parentRoundTrip != j) {
pass = false;
System.out.println("Error: Encoder " + parentEnc +
" fails round-trip: " + j +
" -> \\u" + Integer.toHexString(parentValue) +
" -> " + parentRoundTrip);
}
}
map[(j+0x100)&0xFF] = parentValue;
}
// Modify map with new expected values. Each pair has code point, parent value, euro value.
// Terminated by null.
while (DATA[i] != null) {
int codePoint = Integer.valueOf(DATA[i++], 16).intValue();
char expectedParentValue = DATA[i++].charAt(0);
char expectedEuroValue = DATA[i++].charAt(0);
// This is a sanity check -- we aren't really testing the parent
// encoder here.
if (map[codePoint] != expectedParentValue) {
pass = false;
System.out.println("Error: Encoder " + parentEnc +
" " + Integer.toHexString(codePoint) + " -> \\u" +
Integer.toHexString(map[codePoint]) + ", expected \\u" +
Integer.toHexString(expectedParentValue));
}
// Fill in new expected value
map[codePoint] = expectedEuroValue;
}
++i; // Skip over null at end of set
// Now verify the euro encoder
currentEnc = euroEnc;
for (int j=-128; j<128; ++j) {
bytes[0] = (byte)j;
char euroValue = new String(bytes, euroEnc).charAt(0);
chars[0] = euroValue;
// NOTE: 0x15 doesn't round trip on the EBCDIC code pages,
// so we don't check that code point in the sanity check.
if (j != 0x0015) {
int euroRoundTrip = new String(chars).getBytes(euroEnc)[0];
if (euroRoundTrip != j) {
pass = false;
System.out.println("Error: Encoder " + euroEnc +
" fails round-trip at " + j);
}
}
// Compare against the map
if (euroValue != map[(j+0x100)&0xFF]) {
pass = false;
System.out.println("Error: Encoder " + euroEnc +
" " + Integer.toHexString((j+0x100)&0xFF) + " -> \\u" +
Integer.toHexString(euroValue) + ", expected \\u" +
Integer.toHexString(map[(j+0x100)&0xFF]));
}
}
} catch (UnsupportedEncodingException e) {
System.out.println("Unsupported encoding " + currentEnc);
pass = false;
while (i < DATA.length && DATA[i] != null) ++i;
++i; // Skip over null
}
}
if (!pass) {
throw new RuntimeException("Bug 4114080 - Euro encoder test failed");
}
}
static String[] DATA = {
// New converter, parent converter, [ code point that changed, parent code point value,
// euro code point value ], null
// Any number of changed code points may be specified, including zero.
"ISO8859_15_FDIS", "ISO8859_1",
"A4", "\u00A4", "\u20AC",
"A6", "\u00A6", "\u0160",
"A8", "\u00A8", "\u0161",
"B4", "\u00B4", "\u017D",
"B8", "\u00B8", "\u017E",
"BC", "\u00BC", "\u0152",
"BD", "\u00BD", "\u0153",
"BE", "\u00BE", "\u0178",
null,
// 923 is IBM's name for ISO 8859-15; make sure they're identical
"Cp923", "ISO8859_15_FDIS", null,
"Cp858", "Cp850", "D5", "\u0131", "\u20AC", null,
"Cp1140", "Cp037", "9F", "\u00A4", "\u20AC", null,
"Cp1141", "Cp273", "9F", "\u00A4", "\u20AC", null,
"Cp1142", "Cp277", "5A", "\u00A4", "\u20AC", null,
"Cp1143", "Cp278", "5A", "\u00A4", "\u20AC", null,
"Cp1144", "Cp280", "9F", "\u00A4", "\u20AC", null,
"Cp1145", "Cp284", "9F", "\u00A4", "\u20AC", null,
"Cp1146", "Cp285", "9F", "\u00A4", "\u20AC", null,
"Cp1147", "Cp297", "9F", "\u00A4", "\u20AC", null,
"Cp1148", "Cp500", "9F", "\u00A4", "\u20AC", null,
"Cp1149", "Cp871", "9F", "\u00A4", "\u20AC", null,
};
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
* @bug 6196991
* @summary Roundtrip Encoding/Decoding of just one ASCII char
* @author Martin Buchholz
*/
import java.util.*;
import java.nio.*;
import java.nio.charset.*;
public class FindASCIICodingBugs {
private static int failures = 0;
private static void check(boolean condition) {
if (! condition) {
new Error("test failed").printStackTrace();
failures++;
}
}
private static boolean equals(byte[] ba, ByteBuffer bb) {
if (ba.length != bb.limit())
return false;
for (int i = 0; i < ba.length; i++)
if (ba[i] != bb.get(i))
return false;
return true;
}
public static void main(String[] args) throws Exception {
for (Map.Entry<String,Charset> e
: Charset.availableCharsets().entrySet()) {
String csn = e.getKey();
Charset cs = e.getValue();
// Delete the following lines when these charsets are fixed!
if (csn.equals("x-JIS0208")) continue; // MalformedInput
if (csn.equals("JIS_X0212-1990")) continue; // MalformedInput
if (! cs.canEncode()) continue;
CharsetEncoder enc = cs.newEncoder();
CharsetDecoder dec = cs.newDecoder();
if (! enc.canEncode('A')) continue;
System.out.println(csn);
try {
byte[] bytes1 = "A".getBytes(csn);
ByteBuffer bb = enc.encode(CharBuffer.wrap(new char[]{'A'}));
check(equals(bytes1, bb));
check(new String(bytes1, csn).equals("A"));
CharBuffer cb = dec.decode(bb);
check(cb.toString().equals("A"));
} catch (Throwable t) {
t.printStackTrace();
failures++;
}
}
if (failures > 0)
throw new Exception(failures + "tests failed");
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
* @bug 6378295
* @summary Roundtrip Encoding/Decoding of ASCII chars from 0x00-0x7f
*/
import java.util.*;
import java.nio.*;
import java.nio.charset.*;
public class FindASCIIRangeCodingBugs {
private static int failures = 0;
private static byte[] asciiBytes = new byte[0x80];
private static char[] asciiChars = new char[0x80];
private static String asciiString;
private static void check(String csn) throws Exception {
System.out.println(csn);
if (! Arrays.equals(asciiString.getBytes(csn), asciiBytes)) {
System.out.printf("%s -> bytes%n", csn);
failures++;
}
if (! new String(asciiBytes, csn).equals(asciiString)) {
System.out.printf("%s -> chars%n", csn);
failures++;
}
}
public static void main(String[] args) throws Exception {
for (int i = 0; i < 0x80; i++) {
asciiBytes[i] = (byte) i;
asciiChars[i] = (char) i;
}
asciiString = new String(asciiChars);
Charset ascii = Charset.forName("ASCII");
for (Map.Entry<String,Charset> e
: Charset.availableCharsets().entrySet()) {
String csn = e.getKey();
Charset cs = e.getValue();
if (!cs.contains(ascii) ||
csn.matches(".*2022.*") || //iso2022 family
csn.matches("x-windows-5022[0|1]") || //windows 2022jp
csn.matches(".*UTF-[16|32].*")) //multi-bytes
continue;
if (! cs.canEncode()) continue;
try {
check(csn);
} catch (Throwable t) {
t.printStackTrace();
failures++;
}
}
if (failures > 0)
throw new Exception(failures + "tests failed");
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 5066863 5066867 5066874 5066879 5066884 5066887
@summary canEncode() false iff encode() throws CharacterCodingException
@run main/timeout=1200 FindCanEncodeBugs
@author Martin Buchholz
*/
import java.util.*;
import java.nio.charset.*;
import java.nio.*;
public class FindCanEncodeBugs {
static boolean encodable1(CharsetEncoder enc, char c) {
enc.reset();
return enc.canEncode(c);
}
static boolean encodable2(CharsetEncoder enc, char c) {
enc.reset();
try { enc.encode(CharBuffer.wrap(new char[]{c})); return true; }
catch (CharacterCodingException e) { return false; }
}
public static void main(String[] args) throws Exception {
int failures = 0;
for (Map.Entry<String,Charset> e
: Charset.availableCharsets().entrySet()) {
String csn = e.getKey();
Charset cs = e.getValue();
if (! cs.canEncode() ||
csn.matches("x-COMPOUND_TEXT") ||
csn.matches("x-ISO-2022-CN-CNS") || // ISO2022_CN_CNS supports less
csn.matches("(x-)?IBM(970).*")) // Broken as of 2004-07
continue;
//System.out.println(csn);
CharsetEncoder enc = cs.newEncoder();
for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {
boolean encodable1 = encodable1(enc, (char)i);
boolean encodable2 = encodable2(enc, (char)i);
if (encodable1 != encodable2) {
int start = i;
int end = i;
for (int j = i;
j <= '\uffff' &&
encodable1(enc, (char)j) == encodable1 &&
encodable2(enc, (char)j) == encodable2;
j++)
end = j;
System.out.printf("charset=%-18s canEncode=%-5b ",
csn, encodable1);
if (start == end)
System.out.printf("\'\\u%04x\'%n", start);
else
System.out.printf("\'\\u%04x\' - \'\\u%04x\'%n",
start, end);
i = end;
failures++;
}
}
}
if (failures > 0)
throw new Exception(failures + " failures");
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6380723
* @summary Decode many byte sequences in many ways
* @run main/timeout=1800 FindDecoderBugs
* @author Martin Buchholz
*/
import java.util.*;
import java.util.regex.*;
import java.nio.*;
import java.nio.charset.*;
public class FindDecoderBugs {
static boolean isBroken(String csn) {
if (csn.equals("x-COMPOUND_TEXT")) return true;
return false;
}
static <T extends Comparable<? super T>> List<T> sort(Collection<T> c) {
List<T> list = new ArrayList<T>(c);
Collections.sort(list);
return list;
}
static class TooManyFailures extends RuntimeException {
private static final long serialVersionUID = 0L;
}
static String string(byte[] a) {
final StringBuilder sb = new StringBuilder();
for (byte b : a) {
if (sb.length() != 0) sb.append(' ');
sb.append(String.format("%02x", b & 0xff));
}
return sb.toString();
}
static String string(char[] a) {
final StringBuilder sb = new StringBuilder();
for (char c : a) {
if (sb.length() != 0) sb.append(' ');
sb.append(String.format("\\u%04x", (int) c));
}
return sb.toString();
}
static class Reporter {
// Some machinery to make sure only a small number of errors
// that are "too similar" are reported.
static class Counts extends HashMap<String, Long> {
private static final long serialVersionUID = -1;
long inc(String signature) {
Long count = get(signature);
if (count == null) count = 0L;
put(signature, count+1);
return count+1;
}
}
final Counts failureCounts = new Counts();
final static long maxFailures = 2;
final static Pattern hideBytes = Pattern.compile("\"[0-9a-f ]+\"");
final static Pattern hideChars = Pattern.compile("\\\\u[0-9a-f]{4}");
boolean bug(String format, Object... args) {
String signature = String.format(format, args);
signature = hideBytes.matcher(signature).replaceAll("\"??\"");
signature = hideChars.matcher(signature).replaceAll("\\u????");
failed++;
if (failureCounts.inc(signature) <= maxFailures) {
System.out.printf(format, args);
System.out.println();
return true;
}
return false;
}
void summarize() {
for (String key : sort(failureCounts.keySet()))
System.out.printf("-----%n%s%nfailures=%d%n",
key, failureCounts.get(key));
}
}
static final Reporter reporter = new Reporter();
static class Result {
final int limit;
final int ipos;
final boolean direct;
final byte[] ia;
final char[] oa;
final CoderResult cr;
Result(ByteBuffer ib, CharBuffer ob, CoderResult cr) {
ipos = ib.position();
ia = toArray(ib);
oa = toArray(ob);
direct = ib.isDirect();
limit = ob.limit();
this.cr = cr;
}
static byte[] toArray(ByteBuffer b) {
int pos = b.position();
byte[] a = new byte[b.limit()];
b.position(0);
b.get(a);
b.position(pos);
return a;
}
static char[] toArray(CharBuffer b) {
char[] a = new char[b.position()];
b.position(0);
b.get(a);
return a;
}
static boolean eq(Result x, Result y) {
return x == y ||
(x != null && y != null &&
(Arrays.equals(x.oa, y.oa) &&
x.ipos == y.ipos &&
x.cr == y.cr));
}
public String toString() {
return String.format("\"%s\"[%d/%d] => %s \"%s\"[%d/%d]%s",
string(ia), ipos, ia.length,
cr, string(oa), oa.length, limit,
(direct ? " (direct)" : ""));
}
}
// legend: r=regular d=direct In=Input Ou=Output
static final int maxBufSize = 20;
static final ByteBuffer[] ribs = new ByteBuffer[maxBufSize];
static final ByteBuffer[] dibs = new ByteBuffer[maxBufSize];
static final CharBuffer[] robs = new CharBuffer[maxBufSize];
static final CharBuffer[] dobs = new CharBuffer[maxBufSize];
static {
for (int i = 0; i < maxBufSize; i++) {
ribs[i] = ByteBuffer.allocate(i);
dibs[i] = ByteBuffer.allocateDirect(i);
robs[i] = CharBuffer.allocate(i);
dobs[i] = ByteBuffer.allocateDirect(i*2).asCharBuffer();
}
}
static class CharsetTester {
private final Charset cs;
private static final long maxFailures = 5;
private long failures = 0;
// private static final long maxCharsetFailures = Long.MAX_VALUE;
private static final long maxCharsetFailures = 10000L;
private final long failed0 = failed;
CharsetTester(Charset cs) {
this.cs = cs;
}
static boolean bug(String format, Object... args) {
return reporter.bug(format, args);
}
Result recode(ByteBuffer ib, CharBuffer ob) {
try {
char canary = '\u4242';
ib.clear(); // Prepare to read
ob.clear(); // Prepare to write
for (int i = 0; i < ob.limit(); i++)
ob.put(i, canary);
CharsetDecoder coder = cs.newDecoder();
CoderResult cr = coder.decode(ib, ob, false);
equal(ib.limit(), ib.capacity());
equal(ob.limit(), ob.capacity());
Result r = new Result(ib, ob, cr);
if (cr.isError())
check(cr.length() > 0);
if (cr.isOverflow() && ob.remaining() > 10)
bug("OVERFLOW, but there's lots of room: %s %s",
cs, r);
// if (cr.isOverflow() && ib.remaining() == 0)
// bug("OVERFLOW, yet remaining() == 0: %s %s",
// cs, r);
if (cr.isError() && ib.remaining() < cr.length())
bug("remaining() < CoderResult.length(): %s %s",
cs, r);
// if (ib.position() == 0 && ob.position() > 0)
// reporter. bug("output only if input consumed: %s %s",
// cs, r);
// Should we warn if cr.isUnmappable() ??
CoderResult cr2 = coder.decode(ib, ob, false);
if (ib.position() != r.ipos ||
ob.position() != r.oa.length ||
cr != cr2)
bug("Coding operation not idempotent: %s%n %s%n %s",
cs, r, new Result(ib, ob, cr2));
if (ob.position() < ob.limit() &&
ob.get(ob.position()) != canary)
bug("Buffer overrun: %s %s %s",
cs, r, ob.get(ob.position()));
return r;
} catch (Throwable t) {
if (bug("Unexpected exception: %s %s %s",
cs, t.getClass().getSimpleName(),
new Result(ib, ob, null)))
t.printStackTrace();
return null;
}
}
Result recode2(byte[] ia, int n) {
int len = ia.length;
ByteBuffer rib = ByteBuffer.wrap(ia);
ByteBuffer dib = dibs[len];
dib.clear(); dib.put(ia); dib.clear();
CharBuffer rob = robs[n];
CharBuffer dob = dobs[n];
equal(rob.limit(), n);
equal(dob.limit(), n);
check(dib.isDirect());
check(dob.isDirect());
Result r1 = recode(rib, rob);
Result r2 = recode(dib, dob);
if (r1 != null && r2 != null && ! Result.eq(r1, r2))
bug("Results differ for direct buffers: %s%n %s%n %s",
cs, r1, r2);
return r1;
}
Result test(byte[] ia) {
if (failed - failed0 >= maxCharsetFailures)
throw new TooManyFailures();
Result roomy = recode2(ia, maxBufSize - 1);
if (roomy == null) return roomy;
int olen = roomy.oa.length;
if (olen > 0) {
if (roomy.ipos == roomy.ia.length) {
Result perfectFit = recode2(ia, olen);
if (! Result.eq(roomy, perfectFit))
bug("Results differ: %s%n %s%n %s",
cs, roomy, perfectFit);
}
for (int i = 0; i < olen; i++) {
Result claustrophobic = recode2(ia, i);
if (claustrophobic == null) return roomy;
if (roomy.cr.isUnderflow() &&
! claustrophobic.cr.isOverflow())
bug("Expected OVERFLOW: %s%n %s%n %s",
cs, roomy, claustrophobic);
}
}
return roomy;
}
void testExhaustively(byte[] prefix, int n) {
int len = prefix.length;
byte[] ia = Arrays.copyOf(prefix, len + 1);
for (int i = 0; i < 0x100; i++) {
ia[len] = (byte) i;
if (n == 1)
test(ia);
else
testExhaustively(ia, n - 1);
}
}
void testRandomly(byte[] prefix, int n) {
int len = prefix.length;
byte[] ia = Arrays.copyOf(prefix, len + n);
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < n; j++)
ia[len + j] = randomByte();
test(ia);
}
}
void testPrefix(byte[] prefix) {
if (prefix.length > 0)
System.out.printf("Testing prefix %s%n", string(prefix));
test(prefix);
testExhaustively(prefix, 1);
testExhaustively(prefix, 2);
// Can you spare a week of CPU time?
// testExhaustively(cs, tester, prefix, 3);
testRandomly(prefix, 3);
testRandomly(prefix, 4);
}
}
private final static Random rnd = new Random();
private static byte randomByte() {
return (byte) rnd.nextInt(0x100);
}
private static byte[] randomBytes(int len) {
byte[] a = new byte[len];
for (int i = 0; i < len; i++)
a[i] = randomByte();
return a;
}
private static final byte SS2 = (byte) 0x8e;
private static final byte SS3 = (byte) 0x8f;
private static final byte ESC = (byte) 0x1b;
private static final byte SO = (byte) 0x0e;
private static final byte SI = (byte) 0x0f;
private final static byte[][] stateChangers = {
{SS2}, {SS3}, {SO}, {SI}
};
private final static byte[][]escapeSequences = {
{ESC, '(', 'B'},
{ESC, '(', 'I'},
{ESC, '(', 'J'},
{ESC, '$', '@'},
{ESC, '$', 'A'},
{ESC, '$', ')', 'A'},
{ESC, '$', ')', 'C'},
{ESC, '$', ')', 'G'},
{ESC, '$', '*', 'H'},
{ESC, '$', '+', 'I'},
{ESC, '$', 'B'},
{ESC, 'N'},
{ESC, 'O'},
{ESC, '$', '(', 'D'},
};
private static boolean isStateChanger(Charset cs, byte[] ia) {
Result r = new CharsetTester(cs).recode2(ia, 9);
return r == null ? false :
(r.cr.isUnderflow() &&
r.ipos == ia.length &&
r.oa.length == 0);
}
private final static byte[][] incompletePrefixes = {
{ESC},
{ESC, '('},
{ESC, '$'},
{ESC, '$', '(',},
};
private static boolean isIncompletePrefix(Charset cs, byte[] ia) {
Result r = new CharsetTester(cs).recode2(ia, 9);
return r == null ? false :
(r.cr.isUnderflow() &&
r.ipos == 0 &&
r.oa.length == 0);
}
private static void testCharset(Charset cs) throws Throwable {
final String csn = cs.name();
if (isBroken(csn)) {
System.out.printf("Skipping possibly broken charset %s%n", csn);
return;
}
System.out.println(csn);
CharsetTester tester = new CharsetTester(cs);
tester.testPrefix(new byte[0]);
if (! csn.matches("(?:x-)?(?:UTF|JIS(?:_X)?0).*")) {
for (byte[] prefix : stateChangers)
if (isStateChanger(cs, prefix))
tester.testPrefix(prefix);
for (byte[] prefix : incompletePrefixes)
if (isIncompletePrefix(cs, prefix))
tester.testPrefix(prefix);
if (isIncompletePrefix(cs, new byte[] {ESC}))
for (byte[] prefix : escapeSequences)
if (isStateChanger(cs, prefix))
tester.testPrefix(prefix);
}
}
private static void realMain(String[] args) {
for (Charset cs : sort(Charset.availableCharsets().values())) {
try {
testCharset(cs);
} catch (TooManyFailures e) {
System.out.printf("Too many failures for %s%n", cs);
} catch (Throwable t) {
unexpected(t);
}
}
reporter.summarize();
}
//--------------------- Infrastructure ---------------------------
static volatile long passed = 0, failed = 0;
static void pass() {passed++;}
static void fail() {failed++; Thread.dumpStack();}
static void fail(String format, Object... args) {
System.out.println(String.format(format, args)); failed++;}
static void fail(String msg) {System.out.println(msg); fail();}
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
static void check(boolean cond) {if (cond) pass(); else fail();}
static void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
try {realMain(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
}
此差异已折叠。
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 5058133 6233345 6381699 6381702 6381705 6381706
* @summary Check that all one-char sequences can be encoded by all charsets
* @run main/timeout=1200 FindOneCharEncoderBugs
* @author Martin Buchholz
*/
import java.util.*;
import java.nio.*;
import java.nio.charset.*;
public class FindOneCharEncoderBugs {
final static String[] brokenCharsets = {
// Delete the following lines when these charsets are fixed!
"x-IBM970",
"x-COMPOUND_TEXT", // Direct buffers not supported
};
private static boolean equals(byte[] ba, ByteBuffer bb) {
if (ba.length != bb.limit())
return false;
for (int i = 0; i < ba.length; i++)
if (ba[i] != bb.get(i))
return false;
return true;
}
private static String toString(byte[] bytes) {
final StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
if (sb.length() != 0) sb.append(' ');
sb.append(String.format("%02x", (int)b));
}
return sb.toString();
}
private static String toString(ByteBuffer bb) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < bb.limit(); i++) {
if (sb.length() != 0) sb.append(' ');
sb.append(String.format("%02x", (int)bb.get(i)));
}
return sb.toString();
}
private static ByteBuffer convert(Charset cs, char c, CharBuffer cb) throws Throwable {
cb.clear(); cb.put(c); cb.flip();
return cs.newEncoder()
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.onMalformedInput(CodingErrorAction.REPLACE)
.encode(cb);
}
/** Returns a direct CharBuffer with the same capacity as ordinary CharBuffer ocb */
private static CharBuffer directCharBuffer(CharBuffer ocb) {
final CharBuffer dcb =
ByteBuffer.allocateDirect(ocb.capacity() * Character.SIZE / Byte.SIZE)
.asCharBuffer();
check(! ocb.isDirect());
check( dcb.isDirect());
equal(ocb.capacity(), dcb.capacity());
return dcb;
}
private static void testChar(byte[] expected, CharBuffer cb, Charset cs, char c) {
try {
final ByteBuffer bb = convert(cs, c, cb);
if (! equals(expected, bb))
fail("bytes differ charset=%s direct=%s char=\\u%04x%n%s%n%s",
cs, cb.isDirect(), (int)c,
toString(expected), toString(bb));
} catch (Throwable t) {
System.out.printf("Unexpected exception charset=%s direct=%s char=\\u%04x%n",
cs, cb.isDirect(), (int)c);
unexpected(t);
failed++;
}
}
private static void testCharset(Charset cs) throws Throwable {
if (! cs.canEncode())
return;
final String csn = cs.name();
for (String n : brokenCharsets)
if (csn.equals(n)) {
System.out.printf("Skipping possibly broken charset %s%n", csn);
return;
}
System.out.println(csn);
final char[] theChar = new char[1];
final CharBuffer ocb = CharBuffer.allocate(1);
final CharBuffer dcb = directCharBuffer(ocb);
final int maxFailuresPerCharset = 5;
final int failed0 = failed;
for (char c = '\u0000';
(c+1 != 0x10000) && (failed - failed0 < maxFailuresPerCharset);
c++) {
theChar[0] = c;
byte[] bytes = new String(theChar).getBytes(csn);
if (bytes.length == 0)
fail("Empty output?! charset=%s char=\\u%04x", cs, (int)c);
testChar(bytes, ocb, cs, c);
testChar(bytes, dcb, cs, c);
}
}
private static void realMain(String[] args) {
for (Charset cs : Charset.availableCharsets().values()) {
try { testCharset(cs); }
catch (Throwable t) { unexpected(t); }
}
}
//--------------------- Infrastructure ---------------------------
static volatile int passed = 0, failed = 0;
static void pass() {passed++;}
static void fail() {failed++; Thread.dumpStack();}
static void fail(String format, Object... args) {
System.out.println(String.format(format, args)); failed++;}
static void fail(String msg) {System.out.println(msg); fail();}
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
static void check(boolean cond) {if (cond) pass(); else fail();}
static void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
try {realMain(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
private static abstract class Fun {abstract void f() throws Throwable;}
static void THROWS(Class<? extends Throwable> k, Fun... fs) {
for (Fun f : fs)
try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
catch (Throwable t) {
if (k.isAssignableFrom(t.getClass())) pass();
else unexpected(t);}}
private static abstract class CheckedThread extends Thread {
abstract void realRun() throws Throwable;
public void run() {
try {realRun();} catch (Throwable t) {unexpected(t);}}}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4715330
@summary Check MS932/windows-31j encoding (char->byte) for halfwidth katakana chars
*/
/*
* Tests encodeability of the Unicode defined Halfwidth Katakana
* characters using the MS932/windows-31j encoder
*/
public class HWKatakanaMS932EncodeTest {
public static void main(String[] args) throws Exception {
char[] testChars = new char[1];
byte[] testBytes = new byte[1];
int offset = 0;
String encoding = "windows-31j";
// Halfwidth Katakana chars run from U+FF61 --> U+FF9F
// and their native equivalents in Code page 932 run
// sequentially from 0xa1 --> 0xdf
for (int lsByte = 0x61 ; lsByte <= 0x9F; lsByte++, offset++) {
testChars[0] = (char) (lsByte | 0xFF00);
String s = new String(testChars);
testBytes = s.getBytes(encoding);
if ( testBytes[0] != (byte)(0xa1 + offset))
throw new Exception("failed Test");
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4328178
@summary Performs baseline and regression test on the ISCII91 charset
*/
import java.io.*;
public class ISCIITest {
private static void failureReport() {
System.err.println ("Failed ISCII91 Regression Test");
}
private static void mapEquiv(int start,
int end,
String testName)
throws Exception
{
byte[] singleByte = new byte[1];
byte[] encoded = new byte[1];
for (int i = start; i <= end; i++ ) {
singleByte[0] = (byte) i;
try {
String unicodeStr =
new String (singleByte,"ISCII91");
if (i != (int)unicodeStr.charAt(0)) {
System.err.println ("FAILED ISCII91 Regression test"
+ "input byte is " + i );
throw new Exception("");
}
encoded = unicodeStr.getBytes("ISCII91");
if (encoded[0] != singleByte[0]) {
System.err.println("Encoding error " + testName);
throw new Exception("Failed ISCII91 Regression test");
}
} catch (UnsupportedEncodingException e) {
failureReport();
}
}
return;
}
private static void checkUnmapped(int start,
int end,
String testName)
throws Exception {
byte[] singleByte = new byte[1];
for (int i = start; i <= end; i++ ) {
singleByte[0] = (byte) i;
try {
String unicodeStr = new String (singleByte, "ISCII91");
if (unicodeStr.charAt(0) != '\uFFFD') {
System.err.println("FAILED " + testName +
"input byte is " + i );
throw new Exception ("Failed ISCII91 regression test");
}
} catch (UnsupportedEncodingException e) {
System.err.println("Unsupported character encoding");
}
}
return;
}
/*
*
*/
private static void checkRange(int start, int end,
char[] expectChars,
String testName)
throws Exception {
byte[] singleByte = new byte[1];
byte[] encoded = new byte[1];
int lookupOffset = 0;
for (int i=start; i <= end; i++ ) {
singleByte[0] = (byte) i;
String unicodeStr = new String (singleByte, "ISCII91");
if (unicodeStr.charAt(0) != expectChars[lookupOffset++]) {
throw new Exception ("Failed ISCII91 Regression Test");
}
encoded = unicodeStr.getBytes("ISCII");
}
return;
}
/*
* Tests the ISCII91 Indic character encoding
* as per IS 13194:1991 Bureau of Indian Standards.
*/
private static void test () throws Exception {
try {
// ISCII91 is an 8-byte encoding which retains the ASCII
// mappings in the lower half.
mapEquiv(0, 0x7f, "7 bit ASCII range");
// Checks a range of characters which are unmappable according
// to the standards.
checkUnmapped(0x81, 0x9f, "UNMAPPED");
// Vowel Modifier chars can be used to modify the vowel
// sound of the preceding consonant, vowel or matra character.
byte[] testByte = new byte[1];
char[] vowelModChars = {
'\u0901', // Vowel modifier Chandrabindu
'\u0902', // Vowel modifier Anuswar
'\u0903' // Vowel modifier Visarg
};
checkRange(0xa1, 0xa3, vowelModChars, "INDIC VOWEL MODIFIER CHARS");
char[] expectChars = {
'\u0905', // a4 -- Vowel A
'\u0906', // a5 -- Vowel AA
'\u0907', // a6 -- Vowel I
'\u0908', // a7 -- Vowel II
'\u0909', // a8 -- Vowel U
'\u090a', // a9 -- Vowel UU
'\u090b', // aa -- Vowel RI
'\u090e', // ab -- Vowel E ( Southern Scripts )
'\u090f', // ac -- Vowel EY
'\u0910', // ad -- Vowel AI
'\u090d', // ae -- Vowel AYE ( Devanagari Script )
'\u0912', // af -- Vowel O ( Southern Scripts )
'\u0913', // b0 -- Vowel OW
'\u0914', // b1 -- Vowel AU
'\u0911', // b2 -- Vowel AWE ( Devanagari Script )
};
checkRange(0xa4, 0xb2, expectChars, "INDIC VOWELS");
char[] expectConsChars =
{
'\u0915', // b3 -- Consonant KA
'\u0916', // b4 -- Consonant KHA
'\u0917', // b5 -- Consonant GA
'\u0918', // b6 -- Consonant GHA
'\u0919', // b7 -- Consonant NGA
'\u091a', // b8 -- Consonant CHA
'\u091b', // b9 -- Consonant CHHA
'\u091c', // ba -- Consonant JA
'\u091d', // bb -- Consonant JHA
'\u091e', // bc -- Consonant JNA
'\u091f', // bd -- Consonant Hard TA
'\u0920', // be -- Consonant Hard THA
'\u0921', // bf -- Consonant Hard DA
'\u0922', // c0 -- Consonant Hard DHA
'\u0923', // c1 -- Consonant Hard NA
'\u0924', // c2 -- Consonant Soft TA
'\u0925', // c3 -- Consonant Soft THA
'\u0926', // c4 -- Consonant Soft DA
'\u0927', // c5 -- Consonant Soft DHA
'\u0928', // c6 -- Consonant Soft NA
'\u0929', // c7 -- Consonant NA ( Tamil )
'\u092a', // c8 -- Consonant PA
'\u092b', // c9 -- Consonant PHA
'\u092c', // ca -- Consonant BA
'\u092d', // cb -- Consonant BHA
'\u092e', // cc -- Consonant MA
'\u092f', // cd -- Consonant YA
'\u095f', // ce -- Consonant JYA ( Bengali, Assamese & Oriya )
'\u0930', // cf -- Consonant RA
'\u0931', // d0 -- Consonant Hard RA ( Southern Scripts )
'\u0932', // d1 -- Consonant LA
'\u0933', // d2 -- Consonant Hard LA
'\u0934', // d3 -- Consonant ZHA ( Tamil & Malayalam )
'\u0935', // d4 -- Consonant VA
'\u0936', // d5 -- Consonant SHA
'\u0937', // d6 -- Consonant Hard SHA
'\u0938', // d7 -- Consonant SA
'\u0939', // d8 -- Consonant HA
};
checkRange(0xb3, 0xd8, expectConsChars, "INDIC CONSONANTS");
char[] matraChars = {
'\u093e', // da -- Vowel Sign AA
'\u093f', // db -- Vowel Sign I
'\u0940', // dc -- Vowel Sign II
'\u0941', // dd -- Vowel Sign U
'\u0942', // de -- Vowel Sign UU
'\u0943', // df -- Vowel Sign RI
'\u0946', // e0 -- Vowel Sign E ( Southern Scripts )
'\u0947', // e1 -- Vowel Sign EY
'\u0948', // e2 -- Vowel Sign AI
'\u0945', // e3 -- Vowel Sign AYE ( Devanagari Script )
'\u094a', // e4 -- Vowel Sign O ( Southern Scripts )
'\u094b', // e5 -- Vowel Sign OW
'\u094c', // e6 -- Vowel Sign AU
'\u0949' // e7 -- Vowel Sign AWE ( Devanagari Script )
};
// Matras or Vowel signs alter the implicit
// vowel sound associated with an Indic consonant.
checkRange(0xda, 0xe7, matraChars, "INDIC MATRAS");
char[] loneContextModifierChars = {
'\u094d', // e8 -- Vowel Omission Sign ( Halant )
'\u093c', // e9 -- Diacritic Sign ( Nukta )
'\u0964' // ea -- Full Stop ( Viram, Northern Scripts )
};
checkRange(0xe8, 0xea,
loneContextModifierChars, "LONE INDIC CONTEXT CHARS");
// Test Indic script numeral chars
// (as opposed to international numerals)
char[] expectNumeralChars =
{
'\u0966', // f1 -- Digit 0
'\u0967', // f2 -- Digit 1
'\u0968', // f3 -- Digit 2
'\u0969', // f4 -- Digit 3
'\u096a', // f5 -- Digit 4
'\u096b', // f6 -- Digit 5
'\u096c', // f7 -- Digit 6
'\u096d', // f8 -- Digit 7
'\u096e', // f9 -- Digit 8
'\u096f' // fa -- Digit 9
};
checkRange(0xf1, 0xfa,
expectNumeralChars, "NUMERAL/DIGIT CHARACTERS");
int lookupOffset = 0;
char[] expectNuktaSub = {
'\u0950',
'\u090c',
'\u0961',
'\u0960',
'\u0962',
'\u0963',
'\u0944',
'\u093d'
};
/*
* ISCII uses a number of code extension techniques
* to access a number of lesser used characters.
* The Nukta character which ordinarily signifies
* a diacritic is used in combination with existing
* characters to escape them to a different character.
* value.
*/
byte[] codeExtensionBytes = {
(byte)0xa1 , (byte)0xe9, // Chandrabindu + Nukta
// =>DEVANAGARI OM SIGN
(byte)0xa6 , (byte)0xe9, // Vowel I + Nukta
// => DEVANAGARI VOCALIC L
(byte)0xa7 , (byte)0xe9, // Vowel II + Nukta
// => DEVANAGARI VOCALIC LL
(byte)0xaa , (byte)0xe9, // Vowel RI + Nukta
// => DEVANAGARI VOCALIC RR
(byte)0xdb , (byte)0xe9, // Vowel sign I + Nukta
// => DEVANAGARI VOWEL SIGN VOCALIC L
(byte)0xdc , (byte)0xe9, // Vowel sign II + Nukta
// => DEVANAGARI VOWEL SIGN VOCALIC LL
(byte)0xdf , (byte)0xe9, // Vowel sign Vocalic R + Nukta
// => DEVANAGARI VOWEL SIGN VOCALIC RR
(byte)0xea , (byte)0xe9 // Full stop/Phrase separator + Nukta
// => DEVANAGARI SIGN AVAGRAHA
};
lookupOffset = 0;
byte[] bytePair = new byte[2];
for (int i=0; i < (codeExtensionBytes.length)/2; i++ ) {
bytePair[0] = (byte) codeExtensionBytes[lookupOffset++];
bytePair[1] = (byte) codeExtensionBytes[lookupOffset++];
String unicodeStr = new String (bytePair,"ISCII91");
if (unicodeStr.charAt(0) != expectNuktaSub[i]) {
throw new Exception("Failed Nukta Sub");
}
}
lookupOffset = 0;
byte[] comboBytes = {
(byte)0xe8 , (byte)0xe8, //HALANT + HALANT
(byte)0xe8 , (byte)0xe9 //HALANT + NUKTA aka. Soft Halant
};
char[] expectCombChars = {
'\u094d',
'\u200c',
'\u094d',
'\u200d'
};
for (int i=0; i < (comboBytes.length)/2; i++ ) {
bytePair[0] = (byte) comboBytes[lookupOffset++];
bytePair[1] = (byte) comboBytes[lookupOffset];
String unicodeStr = new String (bytePair, "ISCII91");
if (unicodeStr.charAt(0) != expectCombChars[lookupOffset-1]
&& unicodeStr.charAt(1) != expectCombChars[lookupOffset]) {
throw new Exception("Failed ISCII91 Regression Test");
}
lookupOffset++;
}
} catch (UnsupportedEncodingException e) {
System.err.println ("ISCII91 encoding not supported");
throw new Exception ("Failed ISCII91 Regression Test");
}
}
public static void main (String[] args) throws Exception {
test();
}
}
此差异已折叠。
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6419791
* @summary
* @author Martin Buchholz
*/
import java.io.*;
import java.util.*;
import java.nio.charset.*;
import java.nio.*;
public class ISO8859x {
final static byte[] lowBytes = new byte[0xa0];
final static char[] lowChars = new char[0xa0];
final static String lowString;
static {
for (int i = 0; i < 0xa0; i++) {
lowBytes[i] = (byte) i;
lowChars[i] = (char) i;
}
lowString = new String(lowChars);
}
private static void testCharset(Charset cs) throws Throwable {
String csn = cs.name();
System.out.println(csn);
check(cs.canEncode());
check(Arrays.equals(lowString.getBytes(csn), lowBytes));
check(new String(lowBytes, csn).equals(lowString));
CharsetEncoder encoder = cs.newEncoder();
CharsetDecoder decoder = cs.newDecoder();
decoder.onUnmappableCharacter(CodingErrorAction.REPORT)
.onMalformedInput(CodingErrorAction.REPORT);
encoder.onUnmappableCharacter(CodingErrorAction.REPORT)
.onMalformedInput(CodingErrorAction.REPORT);
byte[] bytes = new byte[1];
for (int c = 0xa0; c < 0x100; c++) {
try {
bytes[0] = (byte) c;
char[] chars;
try { chars = decoder.decode(ByteBuffer.wrap(bytes)).array(); }
catch (UnmappableCharacterException x) { continue; }
equal(chars.length, 1);
byte[] bytes2 = encoder.encode(CharBuffer.wrap(chars)).array();
check(Arrays.equals(bytes2, bytes));
} catch (Throwable t) {
System.out.printf("cs=%s c=%02x%n", cs, c);
unexpected(t);
}
}
}
private static void realMain(String[] args) throws Throwable {
for (Map.Entry<String,Charset> e
: Charset.availableCharsets().entrySet()) {
String csn = e.getKey();
Charset cs = e.getValue();
if (csn.matches(".*(8859).*"))
try { testCharset(cs); }
catch (Throwable t) { unexpected(t); }
}
}
//--------------------- Infrastructure ---------------------------
static volatile int passed = 0, failed = 0;
static void pass() {passed++;}
static void fail() {failed++; Thread.dumpStack();}
static void fail(String msg) {System.out.println(msg); fail();}
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
static void check(boolean cond) {if (cond) pass(); else fail();}
static void equal(Object x, Object y) {
if (x == null ? y == null : x.equals(y)) pass();
else fail(x + " not equal to " + y);}
public static void main(String[] args) throws Throwable {
try {realMain(args);} catch (Throwable t) {unexpected(t);}
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
if (failed > 0) throw new AssertionError("Some tests failed");}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 4087261 4184592
* @summary Make sure to determine Japanese text encoding as correctly
* as possible.
*/
import java.nio.charset.*;
import java.nio.*;
public class JISAutoDetectTest {
class TestData {
byte[] input;
byte[] input2; // for second call
String expectedCharset;
}
TestData[] data = new TestData[50];
public static void main(String[] argv) throws Exception {
JISAutoDetectTest test = new JISAutoDetectTest();
test.execute();
}
void execute() throws Exception {
CharBuffer output = CharBuffer.allocate(128);
CharBuffer expectedOutput = CharBuffer.allocate(128);
for (int i = 0; i < data.length; i++) {
if (data[i] == null)
break;
CharsetDecoder autoDetect = Charset.forName("JISAutoDetect").newDecoder();
CharsetDecoder dec = Charset.forName(data[i].expectedCharset).newDecoder();
CoderResult ncr, mcr;
output.clear();
expectedOutput.clear();
ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input),
output,
true);
mcr = dec.decode(ByteBuffer.wrap(data[i].input),
expectedOutput,
true);
if (data[i].input2 != null) {
ncr = autoDetect.decode(ByteBuffer.wrap(data[i].input2),
output,
true);
mcr = dec.decode(ByteBuffer.wrap(data[i].input2),
expectedOutput,
true);
}
String testNumber = " (test#: " + i + ")";
if (ncr != mcr)
throw new Exception("JISAutoDetect returned a wrong result");
output.flip();
expectedOutput.flip();
if (output.limit() != expectedOutput.limit())
throw new Exception("JISAutoDetect returned a wrong length"+testNumber);
for (int x = 0; x < output.limit(); x++) {
if (expectedOutput.charAt(x) != output.charAt(x))
throw new Exception("JISAutoDetect returned a wrong string"+testNumber);
}
}
}
public JISAutoDetectTest() {
int i = 0;
// 0
data[i] = new TestData();
data[i].input = new byte[] { (byte)'C', (byte)'o', (byte)'p', (byte)'y',
(byte)'r', (byte)'i', (byte)'g', (byte)'h',
(byte)'t', (byte)' ', (byte)0xa9, (byte)' ',
(byte)'1', (byte)'9', (byte)'9', (byte)'8' };
data[i].expectedCharset = "SJIS";
// 1
i++;
data[i] = new TestData();
data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
(byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 };
data[i].expectedCharset = "SJIS";
// 2
i++;
data[i] = new TestData();
data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde};
data[i].expectedCharset = "SJIS";
// 3
i++;
data[i] = new TestData();
data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd };
data[i].expectedCharset = "SJIS";
// 4
i++;
data[i] = new TestData();
data[i].input = new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa };
data[i].expectedCharset = "SJIS";
// 5
i++;
data[i] = new TestData();
data[i].input = new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,
(byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca };
data[i].expectedCharset = "EUC_JP";
// 6
i++;
data[i] = new TestData();
data[i].input = new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
(byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 };
data[i].expectedCharset = "EUC_JP";
// 7 (for 4184592)
i++;
data[i] = new TestData();
data[i].input = new byte[] { (byte)'a', (byte)'b', (byte)'c' };
data[i].input2 = new byte[] { (byte)0x1b, (byte)'$', (byte)'B',
(byte)'#', (byte)'4', (byte)'$', (byte)'5',
(byte)0x1b, (byte)'(', (byte)'B' };
data[i].expectedCharset = "ISO2022JP";
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4658679 4879644
@summary Checks replacement logic within EUC-TW decoder
*/
/*
* Tests goodness of fix for bugID 4658679: EUC-TW decoder should
* perform replacement when it encounters latin chars outside the
* normal US-ASCII range. For example: Isolated occurrences of
* French accented chars. See bugID: 4658679.
*/
import java.io.*;
public class LatinCharReplacementTWTest {
public static void main(String[] args) throws Exception {
final String bugID = "4658679";
// Attempt to decode
byte[] input = { (byte)0xa1,
(byte)0xf0,
(byte)'r',
(byte)'e',
(byte)'s',
(byte)0xe9, // illegal within EUC-TW
(byte)'r',
(byte)'v',
(byte)0xe9, // illegal within EUC-TW
(byte)'s',
(byte)0xa2,
(byte)0xf8
};
char[] expected = { (char) 0xa7,
(char) 'r',
(char) 'e',
(char) 's',
(char) 0xFFFD, // replacement for accented lowercase e
(char) 'r',
(char) 'v',
(char) 0xFFFD, // replacement for accented lowercase e
(char) 's',
(char) 0xb0 };
ByteArrayInputStream bais = new ByteArrayInputStream(input);
InputStreamReader isr = new InputStreamReader(bais, "x-EUC-TW");
char[] decoded = new char[128];
int numChars = isr.read(decoded);
if (numChars != expected.length) {
throw new Exception("failure of test for bug " + bugID);
}
for (int i = 0 ; i < numChars; i++) {
if (decoded[i] != expected[i])
throw new Exception("failure of test for bug " + bugID);
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4160949
@summary Verify that left over high surrogate does not
cause an UnknownCharacterException when substitutition mode is turned on.
*/
import java.nio.*;
import java.nio.charset.*;
public class LeftOverSurrogate {
public static void main(String args[]) throws Exception {
String s = "abc\uD800\uDC00qrst"; // Valid surrogate
char[] c = s.toCharArray();
CharsetEncoder enc = Charset.forName("ISO8859_1").newEncoder()
.onUnmappableCharacter(CodingErrorAction.REPLACE);
/* Process the first 4 characters, including the high surrogate
which should be stored */
ByteBuffer bb = ByteBuffer.allocate(10);
CharBuffer cb = CharBuffer.wrap(c);
cb.limit(4);
enc.encode(cb, bb, false);
cb.limit(7);
enc.encode(cb, bb, true);
byte[] first = bb.array();
for(int i = 0; i < 7; i++)
System.err.printf("[%d]=%d was %d\n",
i,
(int) first[i] &0xffff,
(int) c[i] & 0xffff);
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4153987
@summary Malformed surrogates should be handled by the converter in
substitution mode.
*/
import java.io.*;
public class MalformedSurrogates {
public static void main(String[] args) throws Exception {
String fe = System.getProperty("file.encoding");
if ( fe.equalsIgnoreCase("UTF8")
|| fe.equalsIgnoreCase("UTF-8")
|| fe.equalsIgnoreCase("UTF_8"))
// This test is meaningless if the default charset
// does handle surrogates
return;
System.out.println("Testing string conversion...");
/* Example with malformed surrogate, and an offset */
String t = "abc\uD800\uDB00efgh";
String t2 = t.substring(2);
byte[] b = t2.getBytes();
System.err.println(b.length);
for (int i = 0; i < b.length; i++)
System.err.println("[" + i + "]" + "=" + (char) b[i]
+ "=" + (int) b[i]);
if (b.length != 7) {
throw new Exception("Bad string conversion for bad surrogate");
}
/* Example with a proper surrogate, no offset. Always worked */
String t3 = "abc\uD800\uDC00efgh";
byte[] b2 = t3.getBytes();
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();
OutputStreamWriter osw = new OutputStreamWriter(os);
System.out.println("Testing flush....");
/* Check for the case where the converter has a left over
high surrogate when flush is called on the converter */
osw.flush();
String s = "abc\uD800"; // High surrogate
char[] c = s.toCharArray();
osw.write(s, 0, 4);
osw.flush();
System.out.println("Testing convert...");
/* Verify that all other characters go through */
for (int k = 1; k < 65535 ; k++) {
osw.write("Char[" + k + "]=\"" + ((char) k) + "\"");
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 4831163 5053096 5056440
* @summary NIO charset basic verification of JISAutodetect decoder
* @author Martin Buchholz
*/
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import static java.lang.System.*;
public class NIOJISAutoDetectTest {
private static int failures = 0;
private static void fail(String failureMsg) {
System.out.println(failureMsg);
failures++;
}
private static void check(boolean cond, String msg) {
if (!cond) {
fail("test failed: " + msg);
new Exception().printStackTrace();
}
}
private static String SJISName() throws Exception {
return detectingCharset(new byte[] {(byte)0xbb, (byte)0xdd,
(byte)0xcf, (byte)0xb2});
}
private static String EUCJName() throws Exception {
return detectingCharset(new byte[] {(byte)0xa4, (byte)0xd2,
(byte)0xa4, (byte)0xe9});
}
private static String detectingCharset(byte[] bytes) throws Exception {
//----------------------------------------------------------------
// Test special public methods of CharsetDecoder while we're here
//----------------------------------------------------------------
CharsetDecoder cd = Charset.forName("JISAutodetect").newDecoder();
check(cd.isAutoDetecting(), "isAutodecting()");
check(! cd.isCharsetDetected(), "isCharsetDetected");
cd.decode(ByteBuffer.wrap(new byte[] {(byte)'A'}));
check(! cd.isCharsetDetected(), "isCharsetDetected");
try {
cd.detectedCharset();
fail("no IllegalStateException");
} catch (IllegalStateException e) {}
cd.decode(ByteBuffer.wrap(bytes));
check(cd.isCharsetDetected(), "isCharsetDetected");
Charset cs = cd.detectedCharset();
check(cs != null, "cs != null");
check(! cs.newDecoder().isAutoDetecting(), "isAutodetecting()");
return cs.name();
}
public static void main(String[] argv) throws Exception {
//----------------------------------------------------------------
// Used to throw BufferOverflowException
//----------------------------------------------------------------
out.println(new String(new byte[] {0x61}, "JISAutoDetect"));
//----------------------------------------------------------------
// InputStreamReader(...JISAutoDetect) used to infloop
//----------------------------------------------------------------
{
byte[] bytes = "ABCD\n".getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
InputStreamReader isr = new InputStreamReader(bais, "JISAutoDetect");
BufferedReader reader = new BufferedReader(isr);
check (reader.readLine().equals("ABCD"), "first read gets text");
// used to return "ABCD" on second and subsequent reads
check (reader.readLine() == null, "second read gets null");
}
//----------------------------------------------------------------
// Check all Japanese chars for sanity
//----------------------------------------------------------------
String SJIS = SJISName();
String EUCJ = EUCJName();
out.printf("SJIS charset is %s%n", SJIS);
out.printf("EUCJ charset is %s%n", EUCJ);
int cnt2022 = 0;
int cnteucj = 0;
int cntsjis = 0;
int cntBAD = 0;
for (char c = '\u0000'; c < '\uffff'; c++) {
if (c == '\u001b' || // ESC
c == '\u2014') // Em-Dash?
continue;
String s = new String (new char[] {c});
//----------------------------------------------------------------
// JISAutoDetect can handle all chars that EUC-JP can,
// unless there is an ambiguity with SJIS.
//----------------------------------------------------------------
byte[] beucj = s.getBytes(EUCJ);
String seucj = new String(beucj, EUCJ);
if (seucj.equals(s)) {
cnteucj++;
String sauto = new String(beucj, "JISAutoDetect");
if (! sauto.equals(seucj)) {
cntBAD++;
String ssjis = new String(beucj, SJIS);
if (! sauto.equals(ssjis)) {
fail("Autodetection agrees with neither EUC nor SJIS");
}
}
} else
continue; // Optimization
//----------------------------------------------------------------
// JISAutoDetect can handle all chars that ISO-2022-JP can.
//----------------------------------------------------------------
byte[] b2022 = s.getBytes("ISO-2022-JP");
if (new String(b2022, "ISO-2022-JP").equals(s)) {
cnt2022++;
check(new String(b2022,"JISAutoDetect").equals(s),
"ISO2022 autodetection");
}
//----------------------------------------------------------------
// JISAutoDetect can handle almost all chars that SJIS can.
//----------------------------------------------------------------
byte[] bsjis = s.getBytes(SJIS);
if (new String(bsjis, SJIS).equals(s)) {
cntsjis++;
check(new String(bsjis,"JISAutoDetect").equals(s),
"SJIS autodetection");
}
}
out.printf("There are %d ISO-2022-JP-encodable characters.%n", cnt2022);
out.printf("There are %d SJIS-encodable characters.%n", cntsjis);
out.printf("There are %d EUC-JP-encodable characters.%n", cnteucj);
out.printf("There are %d characters that are " +
"misdetected as SJIS after being EUC-encoded.%n", cntBAD);
//----------------------------------------------------------------
// tests for specific byte sequences
//----------------------------------------------------------------
test("ISO-2022-JP", new byte[] {'A', 'B', 'C'});
test("EUC-JP", new byte[] {'A', 'B', 'C'});
test("SJIS", new byte[] {'A', 'B', 'C'});
test("SJIS",
new byte[] { 'C', 'o', 'p', 'y', 'r', 'i', 'g', 'h', 't',
' ', (byte)0xa9, ' ', '1', '9', '9', '8' });
test("SJIS",
new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
(byte)0x82, (byte)0xc5, (byte)0x82, (byte)0xb7 });
test("EUC-JP",
new byte[] { (byte)0xa4, (byte)0xd2, (byte)0xa4, (byte)0xe9,
(byte)0xa4, (byte)0xac, (byte)0xa4, (byte)0xca });
test("SJIS",
new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde});
test("SJIS",
new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd });
test("SJIS",
new byte[] { (byte)0x8f, (byte)0xa1, (byte)0xaa });
test("EUC-JP",
new byte[] { (byte)0x8f, (byte)0xc5, (byte)0xe0, (byte)0x20});
test("EUC-JP",
new byte[] { (byte)0xbb, (byte)0xdd, (byte)0xcf, (byte)0xb2,
(byte)0xb8, (byte)0xdb, (byte)0xbc, (byte)0xbd,
(byte)0xc3, (byte)0xd1, (byte)0xbd, (byte)0xde,
(byte)0xa4, (byte)0xc7, (byte)0xa4, (byte)0xb9 });
test("ISO-2022-JP",
new byte[] { 0x1b, '$', 'B', '#', '4', '$', '5', 0x1b, '(', 'B' });
//----------------------------------------------------------------
// Check handling of ambiguous end-of-input in middle of first char
//----------------------------------------------------------------
{
CharsetDecoder dc = Charset.forName("x-JISAutoDetect").newDecoder();
ByteBuffer bb = ByteBuffer.allocate(128);
CharBuffer cb = CharBuffer.allocate(128);
bb.put((byte)'A').put((byte)0x8f);
bb.flip();
CoderResult res = dc.decode(bb,cb,false);
check(res.isUnderflow(), "isUnderflow");
check(bb.position() == 1, "bb.position()");
check(cb.position() == 1, "cb.position()");
res = dc.decode(bb,cb,false);
check(res.isUnderflow(), "isUnderflow");
check(bb.position() == 1, "bb.position()");
check(cb.position() == 1, "cb.position()");
bb.compact();
bb.put((byte)0xa1);
bb.flip();
res = dc.decode(bb,cb,true);
check(res.isUnderflow(), "isUnderflow");
check(bb.position() == 2, "bb.position()");
check(cb.position() == 2, "cb.position()");
}
if (failures > 0)
throw new RuntimeException(failures + " tests failed");
}
static void checkCoderResult(CoderResult result) {
check(result.isUnderflow(),
"Unexpected coder result: " + result);
}
static void test(String expectedCharset, byte[] input) throws Exception {
Charset cs = Charset.forName("x-JISAutoDetect");
CharsetDecoder autoDetect = cs.newDecoder();
Charset cs2 = Charset.forName(expectedCharset);
CharsetDecoder decoder = cs2.newDecoder();
ByteBuffer bb = ByteBuffer.allocate(128);
CharBuffer charOutput = CharBuffer.allocate(128);
CharBuffer charExpected = CharBuffer.allocate(128);
bb.put(input);
bb.flip();
bb.mark();
CoderResult result = autoDetect.decode(bb, charOutput, true);
checkCoderResult(result);
charOutput.flip();
String actual = charOutput.toString();
bb.reset();
result = decoder.decode(bb, charExpected, true);
checkCoderResult(result);
charExpected.flip();
String expected = charExpected.toString();
check(actual.equals(expected),
String.format("actual=%s expected=%s", actual, expected));
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
* @summary Verify that if InputStream.read returns 0 we throw an exception.
* @bug 4684515
*/
import java.io.*;
public class ReadZero {
public static void main(String [] args) throws IOException {
ReadZero r = new ReadZero();
r.testInputStream();
}
private void testInputStream() throws IOException {
File f = new File(System.getProperty("test.src", "."), "ReadZero.java");
InputStream is = new FileInputStream(f) {
public int read(byte [] b, int off, int len) {
System.out.println("FileInputStream.read");
return 0;
}
};
is.read(new byte[1], 0, 1); // ok
InputStreamReader isr = new InputStreamReader(is);
try {
int res = isr.read(new char[1], 0, 1);
} catch (IOException x) {
System.out.println("IOException caught");
return;
}
throw new RuntimeException("IOException not thrown");
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4913702
@summary validates canEncode(char c) method for sun.nio.cs.Shift_JIS
*/
import java.nio.*;
import java.nio.charset.*;
public class SJISCanEncode {
private Charset cs;
private CharsetEncoder encoder;
private void canEncodeTest(char inputChar,
boolean expectedResult)
throws Exception {
String msg = "err: Shift_JIS canEncode() return value ";
if (encoder.canEncode(inputChar) != expectedResult) {
throw new Exception(msg + !(expectedResult) +
": " + Integer.toHexString((int)inputChar));
}
}
public static void main(String[] args) throws Exception {
SJISCanEncode test = new SJISCanEncode();
test.cs = Charset.forName("SJIS");
test.encoder = test.cs.newEncoder();
// us-ascii (mappable by Shift_JIS)
test.canEncodeTest('\u0001', true);
// Halfwidth Katakana
test.canEncodeTest('\uFF01', true);
// CJK ideograph
test.canEncodeTest('\u4E9C', true);
//Hiragana
test.canEncodeTest('\u3041', true);
// fullwidth Katakana
test.canEncodeTest('\u30A1', true);
// U+0080 should be unmappable
// U+4000 is a BMP character not covered by Shift_JISe
test.canEncodeTest('\u0080', false);
test.canEncodeTest('\u4000', false);
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 5005426
@summary Check if StreamEncoder close() method works correctly from
error recovery after the underneath OutputStream failed to
close the first time.
*/
import java.io.*;
public class StreamEncoderClose {
public static void main( String arg[] ) throws Exception {
byte[] expected = {(byte)0x1b,(byte)0x24,(byte)0x42,
(byte)0x30,(byte)0x6c,
(byte)0x1b,(byte)0x28,(byte)0x42};
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MyBufferedOutputStream mbos = new MyBufferedOutputStream(baos);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(mbos, "ISO-2022-JP"));
mbos.dontClose();
pw.write("\u4e00");
pw.close(); // 1st PrintWriter Close
mbos.canClose();
pw.close(); // 2nd PrintWriter Close
//double check, probably not necessary
byte[] out = baos.toByteArray();
if (out.length != expected.length) {
throw new IOException("Failed");
}
for (int i = 0; i < out.length; i++) {
//System.out.printf("(byte)0x%x,", out[i] & 0xff);
if (out[i] != expected[i])
throw new IOException("Failed");
}
}
static class MyBufferedOutputStream extends BufferedOutputStream {
MyBufferedOutputStream(OutputStream os) {
super(os);
}
private boolean status;
public void dontClose() {
status = false;
}
public void canClose() {
status = true;
}
public void close() throws IOException {
if ( status == false ) {
throw new IOException("Can't close ");
}
super.close();
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4896454
@summary Check GB18030 surrogate encoding/decoding handling
*/
import java.nio.*;
import java.nio.charset.*;
public class SurrogateGB18030Test {
public static void main(String[] args) throws Exception {
SurrogateGB18030Test test = new SurrogateGB18030Test();
test.roundtripTest();
/**
* Valid Surrogate pair and 4 byte GB18030 representation
*/
String inputString = "\uD800\uDC00";
byte[] expectedBytes = { (byte)0x90,
(byte)0x30,
(byte)0x81,
(byte)0x30
};
test.encodeTest(inputString, expectedBytes);
/**
* Vice-versa : check that 4 byte GB18030 value encodes correctly
*/
String expectedStr = "\uDBFF\uDFFF";
byte[] inputBytes = { (byte)0xe3,
(byte)0x32,
(byte)0x9a,
(byte)0x35
};
test.decodeTest(inputBytes, expectedStr);
}
private void roundtripTest() throws Exception
{
byte[] ba;
char[] pair = new char[2];
for (char high = '\ud800'; high <= '\udbff'; high++) {
for (char low = '\udc00'; low <= '\udfff'; low++) {
pair[0] = high;
pair[1] = low;
String s = new String(pair);
if (!s.equals(new String(s.getBytes("gb18030"), "gb18030")))
throw new Exception ("GB18030 roundtrip failure");
}
}
}
private void encodeTest(String inputString, byte[] expectedBytes)
throws Exception
{
byte[] encoded = inputString.getBytes("GB18030");
CharBuffer cb = CharBuffer.wrap(inputString.toCharArray());
ByteBuffer bb = ByteBuffer.allocate(4);
CharsetEncoder encoder = Charset.forName("GB18030").newEncoder();
encoder.encode(cb, bb, true);
bb.flip();
for (int i = 0 ; i < expectedBytes.length; i++) {
if (encoded[i] != expectedBytes[i]
|| bb.get() != expectedBytes[i])
throw new Exception ("GB18030 encode failure");
}
}
private void decodeTest(byte[] inputBytes, String expectedStr)
throws Exception
{
String s2 = new String(inputBytes, "GB18030");
CharsetDecoder decoder = Charset.forName("GB18030").newDecoder();
ByteBuffer bb = ByteBuffer.wrap(inputBytes);
CharBuffer cb = CharBuffer.allocate(2);
decoder.decode(bb, cb, true);
cb.flip();
for (int i = 0 ; i < expectedStr.length(); i++) {
if (expectedStr.charAt(i) != cb.get()
|| s2.charAt(i) != expectedStr.charAt(i))
throw new Exception ("GB18030 encode failure");
}
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4847097
@summary Check surrogate coverage of EUC_TW
*/
/*
* Tests the full surrogate mapping roundtrip fidelity of the
* EUC-TW charset coder updated to support the additional
* planes 4,5,6,7,15
*
* byte->char mappings are contained in external files
* using plane{x}.surrogate as the convention for the input filenames
*
*/
import java.io.*;
public class SurrogateTestEUCTW {
private static final String testRootDir
= System.getProperty("test.src", ".");
public static void main(String[] args) throws Exception {
char[] surrogatePair = new char[2];
int[] expectBytes = new int[4];
// Iterate test over each supported CNS-11643 plane
// containing supplementary character mappings
String[] testPlane = { "3", "4", "5", "6" ,"7", "15" };
for (int i = 0 ; i < testPlane.length; i++) {
FileReader f = new FileReader(testRootDir +
System.getProperty("file.separator")
+ "SurrogateTestEUCTW.plane"
+ testPlane[i]
+ ".surrogates");
BufferedReader r = new BufferedReader(f);
String line;
while ((line = r.readLine()) != null) {
int charValue = Integer.parseInt(line.substring(9,14), 16);
surrogatePair[0] = (char) ((charValue - 0x10000) / 0x400
+ 0xd800);
surrogatePair[1] = (char) ((charValue - 0x10000) % 0x400
+ 0xdc00);
// Synthesize 4 byte expected byte values from CNS input values
expectBytes[0] = 0x8E;
expectBytes[1] = 0xA0 + Integer.parseInt(testPlane[i]);
expectBytes[2] = 0x80 | Integer.parseInt(line.substring(2,4), 16);
expectBytes[3] = 0x80 | Integer.parseInt(line.substring(4,6), 16);
String testStr = new String(surrogatePair);
byte[] encodedBytes = testStr.getBytes("EUC-TW");
for (int x = 0 ; x < 4 ; x++) {
if (encodedBytes[x] != (byte)(expectBytes[x] & 0xff)) {
throw new Exception("EUC_TW Surrogate Encoder error");
}
}
// Next: test round-trip fidelity
String decoded = new String(encodedBytes, "EUC-TW");
if (!decoded.equals(testStr)) {
throw new Exception("EUCTW Decoder error");
}
}
r.close();
f.close();
}
}
}
此差异已折叠。
0x2144 0x2000B
0x214F 0x2F817
0x216F 0x201A9
0x217C 0x2F850
0x2225 0x2F82A
0x227B 0x2F815
0x2329 0x2F82C
0x233C 0x2F83B
0x2359 0x21D46
0x2424 0x23C86
0x2429 0x2F835
0x2441 0x2F81B
0x2452 0x2F80E
0x257E 0x225D7
0x2627 0x2F8B2
0x272A 0x2F98F
0x274E 0x20209
0x2753 0x2F80F
0x2754 0x2F811
0x275C 0x206EC
0x2A39 0x2233F
0x2A45 0x2592E
0x2C40 0x2F87A
0x2C51 0x2F899
0x2D35 0x2F8DF
0x2D52 0x2F8FE
0x2E56 0x2F96A
0x2E5A 0x25133
0x3023 0x2F841
0x3053 0x2172E
0x315C 0x2F8E1
0x3350 0x2F983
0x3460 0x20DAE
0x3470 0x2F855
0x347E 0x21637
0x355F 0x2F88D
0x3565 0x2F89C
0x3628 0x2F8BA
0x3640 0x2F8CB
0x3675 0x2F907
0x3977 0x20564
0x3A26 0x2F833
0x3A4F 0x21364
0x3C3A 0x2F8E5
0x3D3F 0x2F934
0x3F6D 0x28CDD
0x4043 0x2F823
0x407E 0x2F87E
0x416E 0x2F8E8
0x4333 0x2F94E
0x4425 0x2F98D
0x446D 0x2F9DB
0x4670 0x2F8A6
0x4731 0x230BA
0x474B 0x235F3
0x4826 0x24A0F
0x486A 0x2F96C
0x5039 0x20B89
0x5460 0x2F8AF
0x553A 0x24039
0x5545 0x2F921
0x5678 0x2F9F5
0x5736 0x29937
0x584F 0x25CD1
0x5863 0x265DF
0x5A33 0x2F86A
0x5A36 0x2F870
0x5B26 0x2F9B6
0x5B2D 0x2F9C1
0x5C2F 0x2FA19
0x607C 0x2F9D6
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
#
# Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
# CA 95054 USA or visit www.sun.com if you need additional information or
# have any questions.
#
# @test
# @bug 4200310
# @summary make sure class files are not duplicated between rt.jar,
# charsets.jar, and localedata.jar
# @author Norbert Lindenberg
# @run shell Test4200310.sh
2>1 $TESTJAVA/bin/jar -tf "$TESTJAVA/jre/lib/rt.jar" > class-list
2>1 $TESTJAVA/bin/jar -tf "$TESTJAVA/jre/lib/charsets.jar" >> class-list
2>1 $TESTJAVA/bin/jar -tf "$TESTJAVA/jre/lib/ext/localedata.jar" >> class-list
duplicates=`grep '\.class$' class-list | sort | uniq -d`
rm -f class-list
if [ "$duplicates" != "" ]; then
echo FAILED: $duplicates are duplicated between rt.jar, charsets.jar, and localedata.jar
exit 1
fi
exit 0
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4206507
@summary verify that we can obtain and use a converter for encoding
ISO8859-9 in the Turkish locale.
*/
import java.util.Locale;
import java.io.UnsupportedEncodingException;
public class Test4206507 {
public static void main(String[] args) throws UnsupportedEncodingException {
Locale.setDefault(new Locale("tr", "TR"));
byte[] b = "".getBytes("ISO8859-9");
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 6254467
@summary Checks if Charset.forName() accepts legal IANA alias names
for charset IBM037.
*/
import java.nio.charset.Charset;
public class Test6254467 {
public static void main (String[] args) throws Exception {
Charset ebcdic = Charset.forName("ebcdic-cp-us");
ebcdic = Charset.forName("ebcdic-cp-ca");
ebcdic = Charset.forName("ebcdic-cp-wt");
ebcdic = Charset.forName("ebcdic-cp-nl");
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 6275027
@summary Check if StreamEncoder works correctly when fed with unpaired
surrogates.
*/
import java.io.*;
public class Test6275027 {
public static void main( String arg[] ) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
ps.print("\uda00");
ps.print("\uda01");
ps.close();
if (!"??".equals(baos.toString()))
throw new Exception("failed");
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
@bug 6392804
@summary Decoder fails to detect decoding error
*/
import java.nio.*;
import java.nio.charset.*;
public class Test6392804 {
public static void main(String[] args) throws Throwable {
test("ISO-2022-JP",
new byte[] {0x1b,(byte)0x8e, 0x24, 0x40, 0x0, 0x0});
test("ISO-2022-JP-2",
new byte[] {0x1b,(byte)0x8e, 0x24, 0x40, 0x0, 0x0});
test("x-windows-50220",
new byte[] {0x1b,(byte)0x8e, 0x24, 0x40, 0x0, 0x0});
test("x-windows-50221",
new byte[] {0x1b,(byte)0x8e, 0x24, 0x40, 0x0, 0x0});
test("x-windows-iso2022jp",
new byte[] {0x1b,(byte)0x8e, 0x24, 0x40, 0x0, 0x0});
test("EUC_TW",
new byte[] {(byte)0x8e, (byte)0xa8, (byte)0xad, (byte)0xe5});
//out of range second byte
test("EUC_TW",
new byte[] {(byte)0x8e, (byte)0x92, (byte)0xa1, (byte)0xa1});
test("EUC_TW",
new byte[] {(byte)0x8e, (byte)0x98, (byte)0xa1, (byte)0xa1});
}
static void test(String csn, byte[] bytes) throws Throwable {
CharsetDecoder dec = Charset.forName(csn).newDecoder();
CharBuffer cb = CharBuffer.allocate(1024);
CoderResult cr = dec.decode(ByteBuffer.wrap(bytes), cb, true);
if (cr.isUnderflow())
throw new RuntimeException(csn + " failed cr=" + cr);
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
* @bug 6427981
* @summary Check the Compound_Text's canonical name and its aliases
*/
import java.nio.charset.*;
import java.util.Set;
public class TestCompoundTest {
public static void main(String args[]) throws Exception
{
if (System.getProperty("os.name").startsWith("Windows"))
return;
Charset cs = Charset.forName("COMPOUND_TEXT");
if (!cs.name().startsWith("x-"))
throw new RuntimeException("FAILED: name does not start with x-");
Set<String> aliases = cs.aliases();
if (!aliases.contains("COMPOUND_TEXT") ||
!aliases.contains("x-compound-text") ||
!aliases.contains("x11-compound_text"))
throw new RuntimeException("FAILED: alias name is missing");
}
}
/*
* Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
@bug 4122961
@summary Verify that converters don't drop characters on buffer boundaries
This is a slightly modified version of the attachment supplied with the
bug report.
*/
import java.io.*;
public class TestConverterDroppedCharacters {
public static void main(String args[])
throws java.io.IOException, java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
{
/* Try misc. encodings, many are broken. */
tryEncoding("Big5");
tryEncoding("CNS11643");
tryEncoding("Cp1006");
tryEncoding("Cp1381");
tryEncoding("Cp33722");
tryEncoding("GB2312");
tryEncoding("KSC5601");
tryEncoding("SJIS");
tryEncoding("UTF8");
}
static void tryEncoding(String encoding)
throws java.io.IOException, java.io.UnsupportedEncodingException,
java.io.FileNotFoundException
{
String filename = "OUTPUT";
int goesBadAfter = 8193;
int i;
char data[] = new char[goesBadAfter+1];
System.out.println("Testing " + encoding);
/* Create some data */
for(i = 0; i < goesBadAfter; i++) {
data[i] = (char)((i % 0x7f) + 1);
}
/* Write the data out to a file. */
FileOutputStream fout = new FileOutputStream(filename);
OutputStreamWriter ow = new OutputStreamWriter(fout, encoding);
BufferedWriter fd = new BufferedWriter(ow);
fd.write(data,0,goesBadAfter);
fd.close();
/* Now read it back with the same encoding. */
char buf[] = new char[goesBadAfter+1];
FileInputStream fin = new FileInputStream("OUTPUT");
InputStreamReader ir = new InputStreamReader(fin, encoding);
ir.read(buf,0,goesBadAfter);
ir.close();
/* And check to see if what we wrote is what we got back. */
for(i = 0; i < goesBadAfter; i++) {
if (data[i] != buf[i]) {
System.out.println("ERROR with encoding " + encoding
+ ": Data wrong at position " + i + " "
+ "in: " + (int)data[i] + " "
+ "out: " + (int)buf[i]);
throw new RuntimeException();
}
}
System.out.println("Successfully tested " + encoding);
}
}
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册