Exemplo n.º 1
0
 /// <summary>
 ///   Saves an object to a stream.
 /// </summary>
 ///
 /// <param name="obj">The object to be serialized.</param>
 /// <param name="bytes">The sequence of bytes to which the object has been serialized.</param>
 /// <param name="compression">The type of compression to use. Default is None.</param>
 ///
 public static void Save <T>(this T obj, out byte[] bytes, SerializerCompression compression = DEFAULT_COMPRESSION)
 {
     using (var fs = new MemoryStream())
     {
         Save(obj, fs, compression);
         fs.Seek(0, SeekOrigin.Begin);
         bytes = fs.ToArray();
     }
 }
Exemplo n.º 2
0
        /// <summary>
        ///   Saves an array of dense samples to a stream.
        /// </summary>
        ///
        /// <param name="stream">The stream where the file will be saved.</param>
        /// <param name="samples">The samples that have been read from the file.</param>
        /// <param name="outputs">The output labels associated with each sample in <paramref name="samples"/>.</param>
        /// <param name="compression">The type of compression to use. Default is None.</param>
        ///
        public static void Save(double[][] samples, bool[] outputs, Stream stream, SerializerCompression compression = DEFAULT_COMPRESSION)
        {
            if (compression == SerializerCompression.GZip)
            {
                using (var gzip = new GZipStream(stream, CompressionMode.Compress))
                {
                    Save(samples, outputs, gzip, SerializerCompression.None);
                    return;
                }
            }

            using (SparseWriter writer = new SparseWriter(stream))
                writer.Write(samples, outputs);
        }
Exemplo n.º 3
0
        static T Load <T>(Stream stream, BinaryFormatter formatter, SerializerCompression compression = DEFAULT_COMPRESSION)
        {
            lock (lockObj)
            {
                try
                {
                    if (formatter.Binder == null)
                    {
                        formatter.Binder = GetBinder(typeof(T));
                    }

                    AppDomain.CurrentDomain.AssemblyResolve += resolve;

                    if (formatter.SurrogateSelector == null)
                    {
                        formatter.SurrogateSelector = GetSurrogate(typeof(T));
                    }

                    object obj;
                    if (compression == SerializerCompression.GZip)
                    {
                        using (var gzip = new GZipStream(stream, CompressionMode.Decompress, leaveOpen: true))
                            obj = formatter.Deserialize(gzip);
                    }
                    else if (compression == SerializerCompression.None)
                    {
                        obj = formatter.Deserialize(stream);
                    }
                    else
                    {
                        throw new ArgumentException("compression");
                    }

                    if (obj is T)
                    {
                        return((T)obj);
                    }
                    return(obj.To <T>());
                }
                finally
                {
                    AppDomain.CurrentDomain.AssemblyResolve -= resolve;
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        ///   Saves an object to a stream.
        /// </summary>
        ///
        /// <param name="obj">The object to be serialized.</param>
        /// <param name="stream">The stream to which the object is to be serialized.</param>
        /// <param name="compression">The type of compression to use. Default is None.</param>
        ///
        public static void Save <T>(this T obj, Stream stream, SerializerCompression compression = DEFAULT_COMPRESSION)
        {
            if (compression == SerializerCompression.GZip)
            {
#if NET35 || NET40
                using (var gzip = new GZipStream(stream, CompressionMode.Compress, leaveOpen: true))
#else
                using (var gzip = new GZipStream(stream, CompressionLevel.Optimal, leaveOpen: true))
#endif
                    new BinaryFormatter().Serialize(gzip, obj);
            }
            else if (compression == SerializerCompression.None)
            {
                new BinaryFormatter().Serialize(stream, obj);
            }
            else
            {
                throw new ArgumentException("compression");
            }
        }
Exemplo n.º 5
0
        /// <summary>
        ///   Saves an object to a stream.
        /// </summary>
        ///
        /// <param name="obj">The object to be serialized.</param>
        /// <param name="path">The path to the file to which the object is to be serialized.</param>
        /// <param name="compression">The type of compression to use. Default is None.</param>
        ///
        public static void Save <T>(this T obj, string path, SerializerCompression compression = DEFAULT_COMPRESSION)
        {
            path = Path.GetFullPath(path);

            var dir = Path.GetDirectoryName(path);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            if (compression == SerializerCompression.GZip)
            {
                if (!path.EndsWith(".gz"))
                {
                    path = path + ".gz";
                }
            }

            using (var fs = new FileStream(path, FileMode.Create))
            {
                Save(obj, fs, compression);
            }
        }
Exemplo n.º 6
0
 /// <summary>
 ///   Loads an object from a stream, represented as an array of bytes.
 /// </summary>
 ///
 /// <param name="bytes">The byte stream containing the object to be deserialized.</param>
 /// <param name="value">The object to be read. This parameter can be used to avoid the
 ///   need of specifying a generic argument to this function.</param>
 /// <param name="compression">The type of compression to use. Default is None.</param>
 ///
 /// <returns>The deserialized object.</returns>
 ///
 public static T Load <T>(byte[] bytes, out T value, SerializerCompression compression = DEFAULT_COMPRESSION)
 {
     return(value = Load <T>(bytes, compression));
 }
Exemplo n.º 7
0
 /// <summary>
 ///   Loads an object from a file.
 /// </summary>
 ///
 /// <param name="path">The path to the file from which the object is to be deserialized.</param>
 /// <param name="compression">The type of compression to use. Default is None.</param>
 /// <param name="value">The object to be read. This parameter can be used to avoid the
 ///   need of specifying a generic argument to this function.</param>
 ///
 /// <returns>The deserialized object.</returns>
 ///
 public static T Load <T>(string path, out T value, SerializerCompression compression)
 {
     return(value = Load <T>(path, compression));
 }
Exemplo n.º 8
0
 /// <summary>
 ///   Loads an object from a stream.
 /// </summary>
 ///
 /// <param name="stream">The stream from which the object is to be deserialized.</param>
 /// <param name="compression">The type of compression to use. Default is None.</param>
 /// <param name="value">The object to be read. This parameter can be used to avoid the
 ///   need of specifying a generic argument to this function.</param>
 ///
 /// <returns>The deserialized machine.</returns>
 ///
 public static T Load <T>(Stream stream, out T value, SerializerCompression compression = DEFAULT_COMPRESSION)
 {
     return(value = Load <T>(stream, compression));
 }
Exemplo n.º 9
0
 /// <summary>
 ///   Loads an object from a file.
 /// </summary>
 ///
 /// <param name="path">The path to the file from which the object is to be deserialized.</param>
 /// <param name="compression">The type of compression to use. Default is None.</param>
 ///
 /// <returns>The deserialized object.</returns>
 ///
 public static T Load <T>(string path, SerializerCompression compression = DEFAULT_COMPRESSION)
 {
     path = Path.GetFullPath(path);
     return(load <T>(path, compression));
 }
Exemplo n.º 10
0
 /// <summary>
 ///   Loads an object from a stream.
 /// </summary>
 ///
 /// <param name="stream">The stream from which the object is to be deserialized.</param>
 /// <param name="compression">The type of compression to use. Default is None.</param>
 ///
 /// <returns>The deserialized machine.</returns>
 ///
 public static T Load <T>(Stream stream, SerializerCompression compression = DEFAULT_COMPRESSION)
 {
     return(Load <T>(stream, new BinaryFormatter(), compression));
 }
Exemplo n.º 11
0
 /// <summary>
 ///   Saves an object to a stream, represented as an array of bytes.
 /// </summary>
 ///
 /// <param name="obj">The object to be serialized.</param>
 /// <param name="compression">The type of compression to use. Default is None.</param>
 ///
 public static byte[] Save <T>(this T obj, SerializerCompression compression = DEFAULT_COMPRESSION)
 {
     byte[] bytes;
     Save(obj, out bytes, compression);
     return(bytes);
 }
Exemplo n.º 12
0
 /// <summary>
 ///   Saves an array of sparse samples to a file in the disk.
 /// </summary>
 ///
 /// <param name="path">The disk path under which the file will be saved.</param>
 /// <param name="samples">The samples that have been read from the file.</param>
 /// <param name="outputs">The output labels associated with each sample in <paramref name="samples"/>.</param>
 /// <param name="compression">The type of compression to use. Default is None.</param>
 ///
 public static void Save(Sparse <double>[] samples, bool[] outputs, string path, SerializerCompression compression = DEFAULT_COMPRESSION)
 {
     using (Stream stream = new FileStream(path, FileMode.Create))
         Save(samples, outputs, stream, compression);
 }
Exemplo n.º 13
0
        /// <summary>
        ///   Loads an array of sparse samples from a stream.
        /// </summary>
        ///
        /// <param name="stream">The stream from where the file will be loaded.</param>
        /// <param name="samples">The samples that have been read from the file.</param>
        /// <param name="outputs">The output labels associated with each sample in <paramref name="samples"/>.</param>
        /// <param name="compression">The type of compression to use. Default is None.</param>
        ///
        public static void Load(Stream stream, out Sparse <double>[] samples, out bool[] outputs, SerializerCompression compression = DEFAULT_COMPRESSION)
        {
            if (compression == SerializerCompression.GZip)
            {
                using (var gzip = new GZipStream(stream, CompressionMode.Decompress))
                {
                    Load(gzip, out samples, out outputs, compression = SerializerCompression.None);
                    return;
                }
            }

            using (SparseReader reader = new SparseReader(stream))
                reader.ReadToEnd(out samples, out outputs);
        }
Exemplo n.º 14
0
 /// <summary>
 ///   Loads an array of sparse samples from a file in the disk.
 /// </summary>
 ///
 /// <param name="path">The path to the file containing the samples to be loaded.</param>
 /// <param name="samples">The samples that have been read from the file.</param>
 /// <param name="outputs">The output labels associated with each sample in <paramref name="samples"/>.</param>
 /// <param name="compression">The type of compression to use. Default is None.</param>
 ///
 public static void Load(string path, out Sparse <double>[] samples, out bool[] outputs, SerializerCompression compression = DEFAULT_COMPRESSION)
 {
     using (FileStream stream = new FileStream(path, FileMode.Open))
         Load(stream, out samples, out outputs, compression);
 }
Exemplo n.º 15
0
 /// <summary>
 ///   Saves an object to a stream.
 /// </summary>
 ///
 /// <param name="obj">The object to be serialized.</param>
 /// <param name="stream">The stream to which the object is to be serialized.</param>
 /// <param name="compression">The type of compression to use. Default is None.</param>
 ///
 public static void Save <T>(this T obj, Stream stream, SerializerCompression compression = DEFAULT_COMPRESSION)
 {
     Save(obj, new BinaryFormatter(), stream, compression);
 }