2014-12-15 22:58:49 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
|
|
|
using System.Text;
|
2014-12-21 20:44:31 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2014-12-15 22:58:49 +08:00
|
|
|
|
|
|
|
|
|
namespace Wox.Infrastructure.Storage
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stroage object using binary data
|
|
|
|
|
/// Normally, it has better performance, but not readable
|
2014-12-18 19:22:47 +08:00
|
|
|
|
/// You MUST mark implement class as Serializable
|
2014-12-15 22:58:49 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
[Serializable]
|
|
|
|
|
public abstract class BinaryStorage<T> : BaseStorage<T> where T : class, IStorage, new()
|
|
|
|
|
{
|
|
|
|
|
protected override string FileSuffix
|
|
|
|
|
{
|
|
|
|
|
get { return ".dat"; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadInternal()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2014-12-21 20:44:31 +08:00
|
|
|
|
FileStream fileStream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
2014-12-15 22:58:49 +08:00
|
|
|
|
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
|
|
|
|
serializedObject = binaryFormatter.Deserialize(fileStream) as T;
|
|
|
|
|
fileStream.Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
serializedObject = LoadDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void SaveInternal()
|
|
|
|
|
{
|
2014-12-21 20:44:31 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
FileStream fileStream = new FileStream(ConfigPath, FileMode.Create);
|
|
|
|
|
BinaryFormatter binaryFormatter = new BinaryFormatter();
|
|
|
|
|
binaryFormatter.Serialize(fileStream, serializedObject);
|
|
|
|
|
fileStream.Close();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Log.Error(e.Message);
|
|
|
|
|
#if (DEBUG)
|
|
|
|
|
{
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
2014-12-15 22:58:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|