/* * Copyright (c) 1999, 2006, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* @test * @bug 4085160 4139951 5005831 * @summary Test that required character encodings are supported
*/
staticboolean equals(byte[] a, byte[] b) { if (a.length != b.length) returnfalse; for (int i = 0; i < a.length; i++) if (a[i] != b[i]) returnfalse; returntrue;
}
/* InputStreamReader */
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
InputStreamReader r = new InputStreamReader(bi, enc);
String inEnc = r.getEncoding(); int n = str.length(); char[] cs = newchar[n]; for (int i = 0; i < n;) { int m; if ((m = r.read(cs, i, n - i)) < 0) thrownew Exception(enc + ": EOF on InputStreamReader");
i += m;
} if (!(new String(cs).equals(str))) thrownew Exception(enc + ": InputStreamReader failed");
// Calls to String.getBytes(Charset) shouldn't automatically // use the cached thread-local encoder. if (charset.name().equals("UTF-16BE")) {
String s = new String(bytes, charset); // Replace the thread-local encoder with this one. byte[] bb = s.getBytes(Charset.forName("UTF-16LE")); if (bytes.length != bb.length) { // Incidental test. thrownew RuntimeException("unequal length: "
+ bytes.length + " != "
+ bb.length);
} else { boolean diff = false; // Expect different byte[] between UTF-16LE and UTF-16BE // even though encoder was previously cached by last call // to getBytes(). for (int i = 0; i < bytes.length; i++) { if (bytes[i] != bb[i])
diff = true;
} if (!diff) thrownew RuntimeException("byte arrays equal");
}
}
/* OutputStreamWriter */
ByteArrayOutputStream bo = new ByteArrayOutputStream();
OutputStreamWriter w = new OutputStreamWriter(bo, enc);
String outEnc = w.getEncoding();
w.write(str);
w.close();
bs = bo.toByteArray(); if (!equals(bs, bytes)) thrownew Exception(enc + ": OutputStreamWriter failed");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.