Skip to content

Instantly share code, notes, and snippets.

@chaojian-zhang
Last active March 1, 2023 13:29
Show Gist options
  • Save chaojian-zhang/22e8b6b33e7594ae12591999e3189902 to your computer and use it in GitHub Desktop.
Save chaojian-zhang/22e8b6b33e7594ae12591999e3189902 to your computer and use it in GitHub Desktop.
Quick C# bin Data. #C#, #LZ4
// <PackageReference Include="K4os.Compression.LZ4.Streams" Version="1.3.5" />
public abstract class BaseNotifyPropertyChanged: INotifyPropertyChanged
{
#region Data Binding
public event PropertyChangedEventHandler PropertyChanged;
public virtual void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
protected bool SetField<TType>(ref TType field, TType value, [CallerMemberName] string propertyName = null)
{
if (EqualityComparer<TType>.Default.Equals(field, value)) return false;
field = value;
NotifyPropertyChanged(propertyName);
return true;
}
#endregion
}
public class ApplicationData: BaseNotifyPropertyChanged
{
#region Metadata
private string _Name = "New Docment";
private string _Description = string.Empty;
private DateTime _CreationTime = DateTime.Now;
private ObservableCollection<string> _Variables = new();
#endregion
#region Data Bindings
public string Name { get => _Name; set => SetField(ref _Name, value); }
public string Description { get => _Description; set => SetField(ref _Description, value); }
public DateTime CreationTime { get => _CreationTime; set => SetField(ref _CreationTime, value); }
public ObservableCollection<string> Variables { get => _Variables; set => SetField(ref _Variables, value); }
#endregion
#region Methods
public static void Save(string filepath, ApplicationData data, bool compressed = true)
{
if (compressed)
{
using LZ4EncoderStream stream = LZ4Stream.Encode(File.Create(filepath));
using BinaryWriter writer = new(stream, Encoding.UTF8, false);
WriteToStream(writer, data);
}
else
{
using FileStream stream = File.Open(filepath, FileMode.Create);
using BinaryWriter writer = new(stream, Encoding.UTF8, false);
WriteToStream(writer, data);
}
}
public static ApplicationData Load(string filepath, bool compressed = true)
{
if (compressed)
{
using LZ4DecoderStream source = LZ4Stream.Decode(File.OpenRead(filepath));
using BinaryReader reader = new(source, Encoding.UTF8, false);
return ReadFromStream(reader);
}
else
{
using FileStream stream = File.Open(filepath, FileMode.Open);
using BinaryReader reader = new(stream, Encoding.UTF8, false);
return ReadFromStream(reader);
}
}
#endregion
#region Routines
private static void WriteToStream(BinaryWriter writer, ApplicationData data)
{
writer.Write(data.Name);
writer.Write(data.Description);
writer.Write(data.CreationTime.ToString("yyyy-MM-dd"));
writer.Write(data.Variables.Count);
foreach (string variable in data.Variables)
{
writer.Write(variable);
}
}
private static ApplicationData ReadFromStream(BinaryReader reader)
{
ApplicationData applicationData = new()
{
Name = reader.ReadString(),
Description = reader.ReadString(),
CreationTime = DateTime.Parse(reader.ReadString())
};
{
var variableCount = reader.ReadInt32();
for (int i = 0; i < variableCount; i++)
{
applicationData.Variables.Add(reader.ReadString());
}
}
return applicationData;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment