Skip to content

Commit

Permalink
Merge pull request #112 from kenjiuno/fix-buffering
Browse files Browse the repository at this point in the history
Add buffering to SaveCommand
  • Loading branch information
Xeeynamo authored May 24, 2020
2 parents dac0330 + 8d9811e commit 0cb541b
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions OpenKh.Tools.BarEditor/ViewModels/BarViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ public BarViewModel(ToolInvokeDesc desc) :
NewCommand = new RelayCommand(x => { }, x => false);
OpenCommand = new RelayCommand(x => { }, x => false);
SaveCommand = new RelayCommand(x =>
{
{
var memoryStream = new MemoryStream();
Bar.Write(memoryStream, Items.Select(item => item.Entry));
var stream = _toolInvokeDesc.SelectedEntry.Stream;
stream.Position = 0;
stream.SetLength(0);
Bar.Write(stream, Items.Select(item => item.Entry));
memoryStream.Position = 0;
memoryStream.CopyTo(stream);
});
SaveAsCommand = new RelayCommand(x => { }, x => false);
}
Expand Down Expand Up @@ -72,10 +77,7 @@ public BarViewModel(IEnumerable<BarEntryModel> list) :
{
if (!string.IsNullOrEmpty(FileName))
{
using (var stream = File.Open(FileName, FileMode.Create))
{
Bar.Write(stream, Items.Select(item => item.Entry));
}
SaveToFile(FileName);
}
else
{
Expand Down Expand Up @@ -180,8 +182,14 @@ public void OpenFileName(string fileName)

private void SaveToFile(string fileName)
{
using (var stream = File.Open(fileName, FileMode.Create))
Bar.Write(stream, Items.Select(item => item.Entry));
var memoryStream = new MemoryStream();
Bar.Write(memoryStream, Items.Select(item => item.Entry));

using (var stream = File.Open(fileName, FileMode.Create))
{
memoryStream.Position = 0;
memoryStream.CopyTo(stream);
}
}

private string GetTemporaryFileName(string actualFileName)
Expand Down

0 comments on commit 0cb541b

Please sign in to comment.