improve FileSources

This commit is contained in:
Michael Becker 2023-06-25 00:25:51 -04:00
parent 41055ff056
commit 2fee4ec567
2 changed files with 22 additions and 3 deletions

View File

@ -359,11 +359,27 @@ namespace UniversalEditor.ObjectModels.FileSystem
/// <returns>The number of bytes written to the <see cref="Writer" />.</returns>
public long WriteTo(Writer bw, FileSourceTransformation[] transformations = null)
{
FileSource source = null;
if (Source != null)
{
source = Source;
}
else if (DataRequest != null)
{
DataRequestEventArgs dre = new DataRequestEventArgs();
DataRequest.Invoke(this, dre);
source = new MemoryFileSource(dre.Data);
}
else
{
throw new InvalidOperationException("neither Source nor DataRequest for file data");
}
long count = 0;
long blockSize = (System.Environment.SystemPageSize / BLOCK_FRACTION);
long offset = 0;
double dbl = ((double)Source.GetLength() / (double)blockSize);
double dbl = ((double)source.GetLength() / (double)blockSize);
long blockCount = (long)Math.Ceiling(dbl);
if (transformations != null)
@ -373,8 +389,8 @@ namespace UniversalEditor.ObjectModels.FileSystem
for (long i = 0; i < blockCount; i++)
{
byte[] data = Source.GetDataInternal(offset, blockSize);
offset += blockSize;
byte[] data = source.GetDataInternal(offset, blockSize);
offset += data.Length;
bw.WriteBytes(data);
count += data.Length;

View File

@ -44,6 +44,9 @@ namespace UniversalEditor.ObjectModels.FileSystem.FileSources
{
long realLength = Math.Min(length, Data.Length);
byte[] realData = Data.ToArray();
long remaining = realData.Length - offset;
realLength = Math.Min(realLength, remaining);
byte[] data = new byte[realLength];
Array.Copy(realData, offset, data, 0, realLength);
return data;