Ejemplo n.º 1
0
    public static MemoryStream Serialize(Object o)
    {
        MemoryStream ms = new MemoryStream();

        formatter.Serialize(ms, o);
        return(ms);
    }
 /// <summary>
 /// Called when save data.
 /// </summary>
 /// <param name="Data">The data.</param>
 /// <returns>Returns true if succeeded.</returns>
 protected override bool OnSave(ref Stream DataStream, ref System.Runtime.Serialization.IFormatter DataFormater)
 {
     if (DataSerializableRef != null)
     {
         DataFormater.Serialize(DataStream, DataSerializableRef);
     }
     return(true);
 }
Ejemplo n.º 3
0
        public static String Serialize(object obj)
        {
            MemoryStream ms = new MemoryStream();

            formatter.Serialize(ms, obj);
            String Result = System.Convert.ToBase64String(ms.ToArray());

            ms.Close();
            return(Result);
        }
Ejemplo n.º 4
0
        void ICloudFileDataTransfer.Serialize(System.Runtime.Serialization.IFormatter dataFormatter, object objectGraph)
        {
            using (var cache = new MemoryStream())
            {
                // serialize into the cache
                dataFormatter.Serialize(cache, objectGraph);

                // go to start
                cache.Position = 0;

                // transfer the cache
                _fsEntry.GetDataTransferAccessor().Transfer(cache, nTransferDirection.nUpload);
            }
        }
Ejemplo n.º 5
0
    /// <summary>
    /// Sets keybindings to match temp_keybindings, and saves to file as well.
    /// </summary>
    public void ApplyKeybinds()
    {
        if (temp_keybindings == null)
        {
            temp_keybindings = new Keybindings();
        }
        keybindings = temp_keybindings.Copy();

        //Debug.Log("Saving to location: \"" + Application.persistentDataPath + "\\Keybindings.txt\"");
        Stream stream = new FileStream(Application.persistentDataPath + "\\Keybindings.txt", FileMode.Create, FileAccess.Write);

        formatter.Serialize(stream, keybindings);
        stream.Close();
    }
 public void Serializer(object _object)
 {
     if (_BinaryFormatter == null)
     {
         _BinaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
     }
     using (var _MemoryStream = new System.IO.MemoryStream())
     {
         _BinaryFormatter.Serialize(_MemoryStream, _object);
         var bytes = _MemoryStream.ToArray();
         Put(bytes.Length);
         Put(bytes);
     }
 }
Ejemplo n.º 7
0
        public static bool ObjToFile(object ObjectToSave, string filename, SerializationMode SerializeMode, string Password, bool Compression)
        {
            lock (ThreadLock)
            {
                Exception err = null;
                System.Security.Cryptography.SymmetricAlgorithm EE = null;
                Stream FinalStream = null;
                System.Runtime.Serialization.IFormatter SR = default(System.Runtime.Serialization.IFormatter);
                byte[] IV         = null;
                byte[] CypherKey  = null;
                string tmpfile    = null;
                string backupfile = null;

                try
                {
                    filename   = System.IO.Path.Combine(LaserGRBL.GrblCore.DataPath, filename);
                    tmpfile    = Path.GetDirectoryName(filename) + Path.DirectorySeparatorChar + "tmp_" + System.IO.Path.GetRandomFileName();
                    backupfile = filename + ".bak";

                    if (SerializeMode == SerializationMode.Auto)
                    {
                        SerializeMode = ModeFromFname(filename);
                    }
                    SR = CreateFormatterForMode(SerializeMode);
                    //CREATE FORMATTER

                    FinalStream = new FileStream(tmpfile, FileMode.CreateNew, FileAccess.Write, FileShare.None);
                    //Open a stream on the file for writing and lock the file

                    if ((Password != null))
                    {
                        EE        = System.Security.Cryptography.SymmetricAlgorithm.Create();
                        IV        = EE.IV;
                        CypherKey = GenerateKey(Password, Convert.ToInt32(EE.KeySize / 8));
                    }

                    WriteSerializerTag(FinalStream, SerializerVersion, SerializeMode, CypherKey, IV, Compression);

                    if ((Password != null))
                    {
                        FinalStream = new System.Security.Cryptography.CryptoStream(FinalStream, EE.CreateEncryptor(CypherKey, EE.IV), System.Security.Cryptography.CryptoStreamMode.Write);
                    }
                    if (Compression)
                    {
                        FinalStream = new System.IO.Compression.DeflateStream(FinalStream, System.IO.Compression.CompressionMode.Compress);
                    }


                    SR.Serialize(FinalStream, ObjectToSave);                     //WRITE DATA
                    FinalStream.Flush();
                    //If TypeOf (SS) Is System.Security.Cryptography.CryptoStream Then DirectCast(SS, System.Security.Cryptography.CryptoStream).FlushFinalBlock()
                    FinalStream.Close();
                    //CLOSE STREAM

                    if ((System.IO.File.Exists(filename)))
                    {
                        System.IO.File.Replace(tmpfile, filename, backupfile, true);
                        System.IO.File.Delete(backupfile);
                    }
                    else
                    {
                        System.IO.File.Move(tmpfile, filename);
                    }

                    return(true);
                }
                catch (Exception ex)
                {
                    err = ex;
                    try
                    {
                        FinalStream?.Close();
                    }
                    catch { }
                    try { ManageWriteError(ObjectToSave, filename, ex); }
                    catch { }
                }
                finally
                {
                    //evita di lasciare in giro file temporanei
                    if ((tmpfile != null) && System.IO.File.Exists(tmpfile))
                    {
                        try { System.IO.File.Delete(tmpfile); }
                        catch { }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 8
0
        public void SerializeObj(System.Runtime.Serialization.IFormatter formatter, System.IO.Stream os)
        {
            List <ProgState> prg = GetProgList();

            formatter.Serialize(os, prg);
        }