Newer
Older
ultramine_bukkit / src / main / java / org / bukkit / util / io / BukkitObjectOutputStream.java
@vlad20012 vlad20012 on 24 Feb 2017 1 KB initial
package org.bukkit.util.io;

import org.bukkit.configuration.serialization.ConfigurationSerializable;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

/**
 * This class is designed to be used in conjunction with the {@link
 * ConfigurationSerializable} API. It translates objects to an internal
 * implementation for later deserialization using {@link
 * BukkitObjectInputStream}.
 * <p>
 * Behavior of implementations extending this class is not guaranteed across
 * future versions.
 */
public class BukkitObjectOutputStream extends ObjectOutputStream
{

	/**
	 * Constructor provided to mirror super functionality.
	 *
	 * @throws IOException
	 * @throws SecurityException
	 * @see ObjectOutputStream#ObjectOutputStream()
	 */
	protected BukkitObjectOutputStream() throws IOException, SecurityException
	{
		super();
		super.enableReplaceObject(true);
	}

	/**
	 * Object output stream decoration constructor.
	 *
	 * @param out
	 * @throws IOException
	 * @see ObjectOutputStream#ObjectOutputStream(OutputStream)
	 */
	public BukkitObjectOutputStream(OutputStream out) throws IOException
	{
		super(out);
		super.enableReplaceObject(true);
	}

	@Override
	protected Object replaceObject(Object obj) throws IOException
	{
		if(!(obj instanceof Serializable) && (obj instanceof ConfigurationSerializable))
		{
			obj = Wrapper.newWrapper((ConfigurationSerializable) obj);
		}

		return super.replaceObject(obj);
	}
}