publicstaticvoid main(String[] args) throws Exception { if (args[0].equals("check")) {
String jv = System.getProperty("java.version"); // java.version format: $VNUM\-$PRE
String [] va = (jv.split("-")[0]).split("\\.");
String v = (va.length == 1 || !va[0].equals("1")) ? va[0] : va[1]; int version = Integer.valueOf(v);
System.out.println("version is " + version); if (version >= 7) { for (String oid: SMALL) { // 7 -> 7
check(out(oid), oid); // 6 -> 7
check(out6(oid), oid);
} for (String oid: HUGE) { // 7 -> 7
check(out(oid), oid);
}
} else { for (String oid: SMALL) { // 6 -> 6
check(out(oid), oid); // 7 -> 6
check(out7(oid), oid);
} for (String oid: HUGE) { // 7 -> 6 fails for HUGE oids boolean ok = false; try {
check(out7(oid), oid);
ok = true;
} catch (Exception e) {
System.out.println(e);
} if (ok) { thrownew Exception();
}
}
}
} else { // Generates the JDK6 serialized string inside this test, call // $JDK7/bin/java S11N dump7 // $JDK6/bin/java S11N dump6 // and paste the output at the end of this test.
dump(args[0], SMALL); // For jdk6, the following line will throw an exception, ignore it
dump(args[0], HUGE);
}
}
// Gets the serialized form for jdk6 privatestaticbyte[] out6(String oid) throws Exception { return decode(dump6.get(oid));
}
// Gets the serialized form for jdk7 privatestaticbyte[] out7(String oid) throws Exception { return decode(dump7.get(oid));
}
// Gets the serialized form for this java privatestaticbyte[] out(String oid) throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream(); new ObjectOutputStream(bout).writeObject(ObjectIdentifier.of(oid)); return bout.toByteArray();
}
// Makes sure serialized form can be deserialized privatestaticvoid check(byte[] in, String oid) throws Exception {
ObjectIdentifier o = (ObjectIdentifier) ( new ObjectInputStream(new ByteArrayInputStream(in)).readObject()); if (!o.toString().equals(oid)) { thrownew Exception("Read Fail " + o + ", not " + oid);
}
}
// dump serialized form to java code style text privatestaticvoid dump(String title, String[] oids) throws Exception { for (String oid: oids) {
String hex = encode(out(oid));
System.out.println(" " + title + ".put(\"" + oid + "\","); for (int i = 0; i<hex.length(); i+= 64) { int end = i + 64; if (end > hex.length()) {
end = hex.length();
}
System.out.print(" \"" + hex.substring(i, end) + "\""); if (end == hex.length()) {
System.out.println(");");
} else {
System.out.println(" +");
}
}
}
}
privatestatic String encode(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2); for (byte b: bytes) {
sb.append(String.format("%02x", b & 0xff));
} return sb.toString();
}
privatestaticbyte[] decode(String var) { byte[] data = newbyte[var.length()/2]; for (int i=0; i<data.length; i++) {
data[i] = Integer.valueOf(var.substring(2 * i, 2 * i + 2), 16).byteValue();
} return data;
}
// Do not use diamond operator, this test is also meant to run in jdk6 privatestatic Map<String,String> dump7 = new HashMap<String,String>(); privatestatic Map<String,String> dump6 = new HashMap<String,String>();
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.