001    package org.bukkit.util.io;
002    
003    import java.io.IOException;
004    import java.io.ObjectOutputStream;
005    import java.io.OutputStream;
006    import java.io.Serializable;
007    
008    import org.bukkit.configuration.serialization.ConfigurationSerializable;
009    
010    /**
011     * This class is designed to be used in conjunction with the {@link
012     * ConfigurationSerializable} API. It translates objects to an internal
013     * implementation for later deserialization using {@link
014     * BukkitObjectInputStream}.
015     * <p>
016     * Behavior of implementations extending this class is not guaranteed across
017     * future versions.
018     */
019    public class BukkitObjectOutputStream extends ObjectOutputStream {
020    
021        /**
022         * Constructor provided to mirror super functionality.
023         *
024         * @throws IOException
025         * @throws SecurityException
026         * @see ObjectOutputStream#ObjectOutputStream()
027         */
028        protected BukkitObjectOutputStream() throws IOException, SecurityException {
029            super();
030            super.enableReplaceObject(true);
031        }
032    
033        /**
034         * Object output stream decoration constructor.
035         *
036         * @param out
037         * @throws IOException
038         * @see ObjectOutputStream#ObjectOutputStream(OutputStream)
039         */
040        public BukkitObjectOutputStream(OutputStream out) throws IOException {
041            super(out);
042            super.enableReplaceObject(true);
043        }
044    
045        @Override
046        protected Object replaceObject(Object obj) throws IOException {
047            if (!(obj instanceof Serializable) && (obj instanceof ConfigurationSerializable)) {
048                obj = Wrapper.newWrapper((ConfigurationSerializable) obj);
049            }
050    
051            return super.replaceObject(obj);
052        }
053    }