SonicAudioTools/Source/SonicAudioLib/FileBases/FileBase.cs

64 lines
1.6 KiB
C#
Raw Normal View History

2016-11-12 17:02:48 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace SonicAudioLib.FileBases
2016-11-12 17:02:48 +01:00
{
public abstract class FileBase
2016-11-12 17:02:48 +01:00
{
protected int bufferSize = 4096;
2016-11-12 17:02:48 +01:00
public abstract void Read(Stream source);
public abstract void Write(Stream destination);
2017-04-17 18:06:29 +02:00
public virtual void Load(string sourceFileName, int bufferSize)
2016-11-12 17:02:48 +01:00
{
2017-04-17 18:06:29 +02:00
using (Stream source = new FileStream(sourceFileName, FileMode.Open, FileAccess.Read, FileShare.None, bufferSize))
2016-11-12 17:02:48 +01:00
{
Read(source);
}
this.bufferSize = bufferSize;
2016-11-12 17:02:48 +01:00
}
2017-04-17 18:06:29 +02:00
public virtual void Load(string sourceFileName)
{
Load(sourceFileName, 4096);
}
2016-11-12 17:02:48 +01:00
public virtual void Load(byte[] sourceByteArray)
{
using (Stream source = new MemoryStream(sourceByteArray))
{
Read(source);
}
}
public virtual void Save(string destinationFileName)
{
2017-04-17 18:06:29 +02:00
Save(destinationFileName, 4096);
}
public virtual void Save(string destinationFileName, int bufferSize)
{
using (Stream destination = File.Create(destinationFileName, bufferSize))
2016-11-12 17:02:48 +01:00
{
Write(destination);
}
this.bufferSize = bufferSize;
2016-11-12 17:02:48 +01:00
}
public virtual byte[] Save()
{
using (MemoryStream destination = new MemoryStream())
{
Write(destination);
return destination.ToArray();
}
}
}
}