Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Testing QName Serialisation
/*
* QNameTester.java
* created at 2020-06-10 by Bernd Eckenfels
* Copyright (c) SEEBURGER AG, Germany. All Rights Reserved.
* This code is under the Apache Software License (ASL2.0).
*/
package test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import java.util.Base64;
import javax.xml.namespace.QName;
public class QNameTester
{
static String compatB64 = "rO0ABXNyABlqYXZheC54bWwubmFtZXNwYWNlLlFOYW1lPVIaMLx2_f8CAANMAAlsb2NhbFBhcnR0ABJMamF2YS9sYW5nL1N0cmluZztMAAxuYW1lc3BhY2VVUklxAH4AAUwABnByZWZpeHEAfgABeHB0AAViZXJuZHQAFGh0dHA6Ly9lY2tlbmZlbHMubmV0dAAA";
static String defaultB64= "rO0ABXNyABlqYXZheC54bWwubmFtZXNwYWNlLlFOYW1lgW2oLfw73WwCAANMAAlsb2NhbFBhcnR0ABJMamF2YS9sYW5nL1N0cmluZztMAAxuYW1lc3BhY2VVUklxAH4AAUwABnByZWZpeHEAfgABeHB0AAViZXJuZHQAFGh0dHA6Ly9lY2tlbmZlbHMubmV0dAAA";
public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException
{
System.setProperty("com.sun.xml.namespace.QName.useCompatibleSerialVersionUID", "1.0");
System.out.printf("Runtime: %s %s/%s on %s %s%n", System.getProperty("java.vm.name"),
System.getProperty("java.runtime.version"),
System.getProperty("java.vm.version"),
System.getProperty("os.name"),
System.getProperty("os.version"));
System.out.printf(" com.sun.xml.namespace.QName.useCompatibleSerialVersionUID=%s%n", System.getProperty("com.sun.xml.namespace.QName.useCompatibleSerialVersionUID", "N/A"));
long uid = ObjectStreamClass.lookup(QName.class).getSerialVersionUID();
QName qname = new QName("http://eckenfels.net", "bernd");
System.out.printf("Created QName: %s with UID %d%n", String.valueOf(qname), uid);
// serialize current object version
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(qname);
oos.close();
System.out.printf("Serialized: %s%n", Base64.getUrlEncoder().encodeToString(bos.toByteArray()));
// test the recorded object versions
System.out.printf("%nDeserialize Old%n");
deser(compatB64);
System.out.printf("%nDeserialize Default%n");
deser(defaultB64);
}
/** Deserialize given base64 string with default and custom method. */
private static void deser(String b64) throws IOException
{
byte[] serBytes = Base64.getUrlDecoder().decode(b64);
// default deserialisation
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serBytes)))
{
Object o = ois.readObject();
System.out.println(" Default OIS read: " + o.toString());
} catch (Exception e) {
System.out.println(" Default OIS failed: " + e);
}
// extended deserialization
try (ObjectInputStream ois = new MyObjectInputStream(new ByteArrayInputStream(serBytes)))
{
Object o = ois.readObject();
System.out.println(" My OIS read: " + o.toString());
} catch (Exception e) {
System.out.println(" My OIS failed: " + e);
e.printStackTrace();
}
}
}
class MyObjectInputStream extends ObjectInputStream
{
public MyObjectInputStream(InputStream is) throws IOException
{
super(is);
}
@Override
protected ObjectStreamClass readClassDescriptor()
throws IOException, ClassNotFoundException
{
ObjectStreamClass d = super.readClassDescriptor();
// overwrite any QName descriptor with the current local one
if ("javax.xml.namespace.QName".equals(d.getName()))
return ObjectStreamClass.lookup(QName.class);
else
return d;
}
}
Runtime: OpenJDK 64-Bit Server VM 11.0.1+13-LTS/11.0.1+13-LTS on Windows 10 10.0
com.sun.xml.namespace.QName.useCompatibleSerialVersionUID=N/A
Created QName: {http://eckenfels.net}bernd with UID -9120448754896609940
Serialized: rO0ABXNyABlqYXZheC54bWwubmFtZXNwYWNlLlFOYW1lgW2oLfw73WwCAANMAAlsb2NhbFBhcnR0ABJMamF2YS9sYW5nL1N0cmluZztMAAxuYW1lc3BhY2VVUklxAH4AAUwABnByZWZpeHEAfgABeHB0AAViZXJuZHQAFGh0dHA6Ly9lY2tlbmZlbHMubmV0dAAA
Deserialize Old
Default OIS failed: java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible: stream classdesc serialVersionUID = 4418622981026545151, local class serialVersionUID = -9120448754896609940
My OIS read: {http://eckenfels.net}bernd
Deserialize Default
Default OIS read: {http://eckenfels.net}bernd
My OIS read: {http://eckenfels.net}bernd
Runtime: OpenJDK 64-Bit Server VM 11.0.1+13-LTS/11.0.1+13-LTS on Windows 10 10.0
com.sun.xml.namespace.QName.useCompatibleSerialVersionUID=1.0
Created QName: {http://eckenfels.net}bernd with UID -9120448754896609940
Serialized: rO0ABXNyABlqYXZheC54bWwubmFtZXNwYWNlLlFOYW1lgW2oLfw73WwCAANMAAlsb2NhbFBhcnR0ABJMamF2YS9sYW5nL1N0cmluZztMAAxuYW1lc3BhY2VVUklxAH4AAUwABnByZWZpeHEAfgABeHB0AAViZXJuZHQAFGh0dHA6Ly9lY2tlbmZlbHMubmV0dAAA
Deserialize Old
Default OIS failed: java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible: stream classdesc serialVersionUID = 4418622981026545151, local class serialVersionUID = -9120448754896609940
My OIS read: {http://eckenfels.net}bernd
Deserialize Default
Default OIS read: {http://eckenfels.net}bernd
My OIS read: {http://eckenfels.net}bernd
Runtime: OpenJDK 64-Bit Server VM 1.8.0_212-b04/25.212-b04 on Windows 10 10.0
com.sun.xml.namespace.QName.useCompatibleSerialVersionUID=N/A
Created QName: {http://eckenfels.net}bernd with UID -9120448754896609940
Serialized: rO0ABXNyABlqYXZheC54bWwubmFtZXNwYWNlLlFOYW1lgW2oLfw73WwCAANMAAlsb2NhbFBhcnR0ABJMamF2YS9sYW5nL1N0cmluZztMAAxuYW1lc3BhY2VVUklxAH4AAUwABnByZWZpeHEAfgABeHB0AAViZXJuZHQAFGh0dHA6Ly9lY2tlbmZlbHMubmV0dAAA
Deserialize Old
Default OIS failed: java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible: stream classdesc serialVersionUID = 4418622981026545151, local class serialVersionUID = -9120448754896609940
My OIS read: {http://eckenfels.net}bernd
Deserialize Default
Default OIS read: {http://eckenfels.net}bernd
My OIS read: {http://eckenfels.net}bernd
Runtime: OpenJDK 64-Bit Server VM 1.8.0_212-b04/25.212-b04 on Windows 10 10.0
com.sun.xml.namespace.QName.useCompatibleSerialVersionUID=1.0
Created QName: {http://eckenfels.net}bernd with UID 4418622981026545151
Serialized: rO0ABXNyABlqYXZheC54bWwubmFtZXNwYWNlLlFOYW1lPVIaMLx2_f8CAANMAAlsb2NhbFBhcnR0ABJMamF2YS9sYW5nL1N0cmluZztMAAxuYW1lc3BhY2VVUklxAH4AAUwABnByZWZpeHEAfgABeHB0AAViZXJuZHQAFGh0dHA6Ly9lY2tlbmZlbHMubmV0dAAA
Deserialize Old
Default OIS read: {http://eckenfels.net}bernd
My OIS read: {http://eckenfels.net}bernd
Deserialize Default
Default OIS failed: java.io.InvalidClassException: javax.xml.namespace.QName; local class incompatible: stream classdesc serialVersionUID = -9120448754896609940, local class serialVersionUID = 4418622981026545151
My OIS read: {http://eckenfels.net}bernd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.