Esempio n. 1
0
        /// <summary>
        /// Saves the specified value. A list of external writers can optionally be passed
        /// to either override the default writers or add new ones.
        /// </summary>
        /// <param name="value">Object to serialize</param>
        /// <param name="filePath">Full path to the file</param>
        /// <param name="writers">External writers to be used with the savable writer.</param>
        public void Save(ISavable value, String filePath, params ExternalWriter[] writers)
        {
            filePath = Path.ChangeExtension(filePath, Extension);
            FileStream output = File.Create(filePath);

            //Write header
            WriteHeader(output);

            //Read in content
            BinarySavableWriter writer = new BinarySavableWriter(output, filePath);

            writer.WriteExternals = _writeExternals;

            //Register externals, but if we're not writing any don't bother
            if (_writeExternals)
            {
                if (writers != null || writers.Length > 0)
                {
                    foreach (ExternalWriter extWr in writers)
                    {
                        writer.RegisterExternalWriter(extWr);
                    }
                }
            }
            writer.WriteSavable <ISavable>(null, value);
            writer.Flush();

            //Dispose of resources
            writer.Dispose();
        }
Esempio n. 2
0
        /// <summary>
        /// Saves the specified value. Writing external savables will be disabled.
        /// </summary>
        /// <param name="value">Object to serialize</param>
        /// <param name="output">Stream to serialize the object into</param>
        public override void Save(ISavable value, Stream output)
        {
            //Write header
            WriteHeader(output);

            //Read in content
            MemoryStream        ms     = new MemoryStream();
            BinarySavableWriter writer = new BinarySavableWriter(ms);

            writer.WriteSavable <ISavable>(null, value);
            writer.WriteExternals = false;
            writer.Flush();

            //Write content to output stream
            ms.Position = 0;
            output.Write(ms.GetBuffer(), 0, (int)ms.Length);

            writer.Dispose();
        }