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 und die Messung sind noch experimentell.