Esempio n. 1
0
        private void PersistToDisk(object theObject)
        {
            try
            {
                FileStream        stream    = new FileStream(this.tempFileName, FileMode.Create, FileAccess.Write, FileShare.Read);
                BinaryFormatter   formatter = new BinaryFormatter();
                DeferredFormatter deferred  = new DeferredFormatter(false, null);
                StreamingContext  context   = new StreamingContext(formatter.Context.State, deferred);

                formatter.Context = context;
                formatter.Serialize(stream, theObject);
                deferred.FinishSerialization(stream);
                stream.Flush();
                stream.Close();
            }

            finally
            {
                this.theObjectSaved.Set();
                this.theObjectSaved = null;
            }
        }
Esempio n. 2
0
        public void Save(
            Document input,
            Stream output,
            SaveConfigToken token,
            Surface scratchSurface,
            ProgressEventHandler callback,
            bool rememberToken)
        {
            Tracing.LogFeature("Save(" + GetType().FullName + ")");

            if (!this.SupportsSaving)
            {
                throw new NotImplementedException("Saving is not supported by this FileType");
            }
            else
            {
                Surface disposeMe = null;

                if (scratchSurface == null)
                {
                    disposeMe      = new Surface(input.Size);
                    scratchSurface = disposeMe;
                }
                else if (scratchSurface.Size != input.Size)
                {
                    throw new ArgumentException("scratchSurface.Size must equal input.Size");
                }

                if (rememberToken)
                {
                    Type   ourType        = this.GetType();
                    string savedTokenName = "SaveConfigToken." + ourType.Namespace + "." + ourType.Name + ".BinaryFormatter";

                    MemoryStream ms = new MemoryStream();

                    BinaryFormatter   formatter         = new BinaryFormatter();
                    DeferredFormatter deferredFormatter = new DeferredFormatter(false, null);
                    StreamingContext  streamingContext  = new StreamingContext(formatter.Context.State, deferredFormatter);
                    formatter.Context = streamingContext;

                    object tokenSubset = GetSerializablePortionOfSaveConfigToken(token);

                    formatter.Serialize(ms, tokenSubset);
                    deferredFormatter.FinishSerialization(ms);

                    byte[] bytes       = ms.GetBuffer();
                    string base64Bytes = Convert.ToBase64String(bytes);

                    Settings.CurrentUser.SetString(savedTokenName, base64Bytes);
                }

                try
                {
                    OnSave(input, output, token, scratchSurface, callback);
                }

                catch (OnSaveNotImplementedException)
                {
                    OldOnSaveTrampoline(input, output, token, callback);
                }

                if (disposeMe != null)
                {
                    disposeMe.Dispose();
                    disposeMe = null;
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Saves the Document to the given Stream with the default and given headers, and
        /// using the given IO completion callback.
        /// </summary>
        /// <param name="stream">The Stream to serialize the Document to.</param>
        /// <param name="callback">
        /// This can be used to keep track of the number of uncompressed bytes that are written. The 
        /// values reported through the IOEventArgs.Count+Offset will vary from 1 to approximately 
        /// Layers.Count*Width*Height*sizeof(ColorBgra). The final number will actually be higher 
        /// because of hierarchical overhead, so make sure to cap any progress reports to 100%. This
        /// callback will be wired to the IOFinished event of a SiphonStream. Events may be raised
        /// from any thread. May be null.
        /// </param>
        public void SaveToStream(Stream stream, IOEventHandler callback)
        {
            InitializeDpu();

            PrepareHeader();
            string headerText = this.HeaderXml.OuterXml;

            // Write the header
            byte[] magicBytes = Document.MagicBytes;
            stream.Write(magicBytes, 0, magicBytes.Length);
            byte[] headerBytes = Encoding.UTF8.GetBytes(headerText);
            stream.WriteByte((byte)(headerBytes.Length & 0xff));
            stream.WriteByte((byte)((headerBytes.Length & 0xff00) >> 8));
            stream.WriteByte((byte)((headerBytes.Length & 0xff0000) >> 16));
            stream.Write(headerBytes, 0, headerBytes.Length);
            stream.Flush();

            // Copy version info
            this.savedWith = PdnInfo.GetVersion();

            // Write 0x00, 0x01 to indicate normal .NET serialized data
            stream.WriteByte(0x00);
            stream.WriteByte(0x01);

            // Write the remainder of the file (gzip compressed)
            SiphonStream siphonStream = new SiphonStream(stream);
            BinaryFormatter formatter = new BinaryFormatter();
            DeferredFormatter deferred = new DeferredFormatter(true, null);
            SaveProgressRelay relay = new SaveProgressRelay(deferred, callback);
            formatter.Context = new StreamingContext(formatter.Context.State, deferred);
            formatter.Serialize(siphonStream, this);
            deferred.FinishSerialization(siphonStream);

            stream.Flush();
        }
Esempio n. 4
0
        public void Save(
            Document input, 
            Stream output, 
            SaveConfigToken token, 
            Surface scratchSurface, 
            ProgressEventHandler callback, 
            bool rememberToken)
        {
            Tracing.LogFeature("Save(" + GetType().FullName + ")");

            if (!this.SupportsSaving)
            {
                throw new NotImplementedException("Saving is not supported by this FileType");
            }
            else
            {
                Surface disposeMe = null;

                if (scratchSurface == null)
                {
                    disposeMe = new Surface(input.Size);
                    scratchSurface = disposeMe;
                }
                else if (scratchSurface.Size != input.Size)
                {
                    throw new ArgumentException("scratchSurface.Size must equal input.Size");
                }

                if (rememberToken)
                {
                    Type ourType = this.GetType();
                    string savedTokenName = "SaveConfigToken." + ourType.Namespace + "." + ourType.Name + ".BinaryFormatter";

                    MemoryStream ms = new MemoryStream();

                    BinaryFormatter formatter = new BinaryFormatter();
                    DeferredFormatter deferredFormatter = new DeferredFormatter(false, null);
                    StreamingContext streamingContext = new StreamingContext(formatter.Context.State, deferredFormatter);
                    formatter.Context = streamingContext;

                    object tokenSubset = GetSerializablePortionOfSaveConfigToken(token);

                    formatter.Serialize(ms, tokenSubset);
                    deferredFormatter.FinishSerialization(ms);

                    byte[] bytes = ms.GetBuffer();
                    string base64Bytes = Convert.ToBase64String(bytes);

                    Settings.CurrentUser.SetString(savedTokenName, base64Bytes);
                }

                try
                {
                    OnSave(input, output, token, scratchSurface, callback);
                }

                catch (OnSaveNotImplementedException)
                {
                    OldOnSaveTrampoline(input, output, token, callback);
                }

                if (disposeMe != null)
                {
                    disposeMe.Dispose();
                    disposeMe = null;
                }
            }
        }