考慮下面的代碼:爲什麼US-ASCII編碼接受非US-ASCII字符?
public class ReadingTest {
public void readAndPrint(String usingEncoding) throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[]{(byte) 0xC2, (byte) 0xB5}); // 'micro' sign UTF-8 representation
InputStreamReader isr = new InputStreamReader(bais, usingEncoding);
char[] cbuf = new char[2];
isr.read(cbuf);
System.out.println(cbuf[0]+" "+(int) cbuf[0]);
}
public static void main(String[] argv) throws Exception {
ReadingTest w = new ReadingTest();
w.readAndPrint("UTF-8");
w.readAndPrint("US-ASCII");
}
}
觀察輸出:
µ 181
? 65533
爲什麼的readAndPrint()
(使用US-ASCII的一個)的第二個呼叫成功嗎?我期望它會拋出一個錯誤,因爲在這種編碼中輸入不是合適的字符。 Java API或JLS中要求這種行爲的地方是什麼?
感謝您的回答。問題是我無法輕易更改創建`InputStreamReader`的代碼,因爲它不是我的``org.apache.tools.ant.taskdefs.SQLExec.Transaction.runTransaction(PrintStream)`。我很驚訝地發現Ant的``任務的encoding屬性並不能防止錯誤的輸入。 –
2011-02-03 14:06:52