Bukkit/src/main/java/org/bukkit/util/io/BukkitObjectInputStream.java
Wesley Wolfe e5a14e2dcc Add ConfigurationSerializable-Serializable compatibility. Adds BUKKIT-4662
This commit adds a comaptibility layer for use between
ConfigurationSerializable and Java Serializable, such that when using the
Bukkit object streams, any ConfigurationSerializable acts as if it
implements Serializable for purposes of that wrapped stream.

Included are a set of unit tests for the stream with a check for backward
compatibility across versions.
2013-08-06 18:19:15 -05:00

64 lines
1.9 KiB
Java

package org.bukkit.util.io;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
/**
* This class is designed to be used in conjunction with the {@link
* ConfigurationSerializable} API. It translates objects back to their
* original implementation after being serialized by {@link
* BukkitObjectInputStream}.
* <p>
* Behavior of implementations extending this class is not guaranteed across
* future versions.
*/
public class BukkitObjectInputStream extends ObjectInputStream {
/**
* Constructor provided to mirror super functionality.
*
* @throws IOException
* @throws SecurityException
* @see {@link ObjectInputStream#ObjectInputStream()}
*/
protected BukkitObjectInputStream() throws IOException, SecurityException {
super();
super.enableResolveObject(true);
}
/**
* Object input stream decoration constructor.
*
* @param in
* @throws IOException
* @see {@link ObjectInputStream#ObjectInputStream(InputStream)}
*/
public BukkitObjectInputStream(InputStream in) throws IOException {
super(in);
super.enableResolveObject(true);
}
@Override
protected Object resolveObject(Object obj) throws IOException {
if (obj instanceof Wrapper) {
try {
(obj = ConfigurationSerialization.deserializeObject(((Wrapper<?>) obj).map)).getClass(); // NPE
} catch (Throwable ex) {
throw newIOException("Failed to deserialize object", ex);
}
}
return super.resolveObject(obj);
}
private static IOException newIOException(String string, Throwable cause) {
IOException exception = new IOException(string);
exception.initCause(cause);
return exception;
}
}