Obsoleted the DataRequestEventArgs.Data property in favor of Reader/Offset/Length
This commit is contained in:
parent
a3fba29ef9
commit
e21f04facc
@ -9,7 +9,9 @@ namespace UniversalEditor.ObjectModels.FileSystem
|
||||
public delegate void DataRequestEventHandler(object sender, DataRequestEventArgs e);
|
||||
public class DataRequestEventArgs : EventArgs
|
||||
{
|
||||
|
||||
private byte[] mvarData = null;
|
||||
[Obsolete("Please use Reader, Offset, and Length to construct a DataRequest")]
|
||||
public byte[] Data { get { return mvarData; } set { mvarData = value; } }
|
||||
|
||||
private Reader mvarReader = null;
|
||||
|
||||
@ -0,0 +1,275 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace UniversalEditor.ObjectModels.FileSystem
|
||||
{
|
||||
public class File : IFileSystemObject
|
||||
{
|
||||
public class FileCollection
|
||||
: System.Collections.ObjectModel.Collection<File>
|
||||
{
|
||||
public File Add(string FileName)
|
||||
{
|
||||
File file = new File();
|
||||
file.Name = FileName;
|
||||
base.Add(file);
|
||||
return file;
|
||||
}
|
||||
public File Add(string FileName, byte[] FileData)
|
||||
{
|
||||
File file = new File();
|
||||
file.Name = FileName;
|
||||
file.SetDataAsByteArray(FileData);
|
||||
base.Add(file);
|
||||
return file;
|
||||
}
|
||||
public File Add(string FileName, string SourceFileName)
|
||||
{
|
||||
File file = new File();
|
||||
file.Name = FileName;
|
||||
file.SetDataAsByteArray(System.IO.File.ReadAllBytes(SourceFileName));
|
||||
base.Add(file);
|
||||
return file;
|
||||
}
|
||||
|
||||
#if ENABLE_FILESYSTEM_INHERENT_COMPRESSION
|
||||
public CompressedFile Add(string FileName, byte[] FileData, Compression.CompressionMethod CompressionMethod)
|
||||
{
|
||||
CompressedFile file = new CompressedFile();
|
||||
file.Name = FileName;
|
||||
file.SetDataAsByteArray(FileData);
|
||||
file.CompressionMethod = CompressionMethod;
|
||||
base.Add(file);
|
||||
return file;
|
||||
}
|
||||
public CompressedFile Add(string FileName, string SourceFileName, Compression.CompressionMethod CompressionMethod)
|
||||
{
|
||||
CompressedFile file = new CompressedFile();
|
||||
file.Name = FileName;
|
||||
file.SetDataAsByteArray(System.IO.File.ReadAllBytes(SourceFileName));
|
||||
file.CompressionMethod = CompressionMethod;
|
||||
base.Add(file);
|
||||
return file;
|
||||
}
|
||||
#endif
|
||||
|
||||
public File this[string Name]
|
||||
{
|
||||
get
|
||||
{
|
||||
foreach (File file in this)
|
||||
{
|
||||
if (file.Name == Name)
|
||||
{
|
||||
return file;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(string Name)
|
||||
{
|
||||
return (this[Name] != null);
|
||||
}
|
||||
}
|
||||
|
||||
public event DataRequestEventHandler DataRequest;
|
||||
|
||||
private FileAttributes mvarAttributes = FileAttributes.None;
|
||||
public FileAttributes Attributes { get { return mvarAttributes; } set { mvarAttributes = value; } }
|
||||
|
||||
private string mvarName = String.Empty;
|
||||
/// <summary>
|
||||
/// The name of this file.
|
||||
/// </summary>
|
||||
public string Name { get { return mvarName; } set { mvarName = value; } }
|
||||
|
||||
private byte[] mvarData = null;
|
||||
public byte[] GetDataAsByteArray()
|
||||
{
|
||||
if (mvarData == null && DataRequest == null)
|
||||
{
|
||||
// throw new InvalidOperationException("Data is not represented as a byte array and data request is not handled");
|
||||
Console.WriteLine("DataRequest: " + mvarName + ": Data is not represented as a byte array and data request is not handled");
|
||||
return mvarData;
|
||||
}
|
||||
else if (mvarData == null && DataRequest != null)
|
||||
{
|
||||
DataRequestEventArgs e = new DataRequestEventArgs();
|
||||
DataRequest(this, e);
|
||||
if (e.Data == null && e.Reader == null)
|
||||
{
|
||||
Console.WriteLine("DataRequest: " + mvarName + ": Data is not represented as a byte array or Reader");
|
||||
}
|
||||
else if (e.Data != null)
|
||||
{
|
||||
mvarData = e.Data;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("DataRequest: " + mvarName + ": Data is not represented as a byte array; please use GetData() method");
|
||||
}
|
||||
}
|
||||
return mvarData;
|
||||
}
|
||||
public void SetDataAsByteArray(byte[] data)
|
||||
{
|
||||
mvarStream = null;
|
||||
mvarData = data;
|
||||
}
|
||||
|
||||
public byte[] GetData(long offset, long length)
|
||||
{
|
||||
DataRequestEventArgs e = new DataRequestEventArgs();
|
||||
DataRequest(this, e);
|
||||
if (e.Data != null)
|
||||
{
|
||||
long realLength = Math.Min(length, e.Data.Length);
|
||||
byte[] data = new byte[realLength];
|
||||
Array.Copy(e.Data, 0, data, 0, realLength);
|
||||
return data;
|
||||
}
|
||||
else if (e.Reader != null)
|
||||
{
|
||||
long realLength = Math.Min(length, e.Length);
|
||||
e.Reader.Seek(e.Offset + offset, IO.SeekOrigin.Begin);
|
||||
byte[] data = e.Reader.ReadBytes(length);
|
||||
return data;
|
||||
}
|
||||
|
||||
Console.WriteLine("DataRequest: " + mvarName + ": Data is not represented as a byte array or Reader");
|
||||
return null;
|
||||
}
|
||||
|
||||
private System.IO.Stream mvarStream = null;
|
||||
public System.IO.Stream GetDataAsStream()
|
||||
{
|
||||
if (mvarStream == null) throw new InvalidOperationException("Data is not represented as a stream");
|
||||
return mvarStream;
|
||||
}
|
||||
public void SetDataAsStream(System.IO.Stream stream)
|
||||
{
|
||||
mvarStream = stream;
|
||||
mvarData = null;
|
||||
}
|
||||
|
||||
public string GetDataAsString()
|
||||
{
|
||||
return GetDataAsString(Encoding.Default);
|
||||
}
|
||||
public string GetDataAsString(Encoding encoding)
|
||||
{
|
||||
if (mvarData == null) throw new InvalidOperationException("Data is not represented as a byte array");
|
||||
return encoding.GetString(mvarData);
|
||||
}
|
||||
public void SetDataAsString(string value)
|
||||
{
|
||||
SetDataAsString(value, Encoding.Default);
|
||||
}
|
||||
public void SetDataAsString(string value, Encoding encoding)
|
||||
{
|
||||
SetDataAsByteArray(encoding.GetBytes(value));
|
||||
}
|
||||
|
||||
public object Clone()
|
||||
{
|
||||
File clone = new File();
|
||||
clone.Name = mvarName;
|
||||
clone.DataRequest = DataRequest;
|
||||
if (mvarData == null && mvarStream != null)
|
||||
{
|
||||
clone.SetDataAsStream(mvarStream);
|
||||
}
|
||||
else if (mvarData != null && mvarStream == null)
|
||||
{
|
||||
clone.SetDataAsByteArray(GetDataAsByteArray());
|
||||
}
|
||||
foreach (KeyValuePair<string, object> kvp in mvarProperties)
|
||||
{
|
||||
clone.Properties.Add(kvp.Key, kvp.Value);
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
string strSize = "*";
|
||||
try
|
||||
{
|
||||
byte[] data = this.GetDataAsByteArray();
|
||||
if (data != null)
|
||||
{
|
||||
strSize = data.Length.ToString();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
strSize = "?";
|
||||
}
|
||||
return mvarName + " [" + strSize + "]";
|
||||
}
|
||||
|
||||
public void Save()
|
||||
{
|
||||
Save(Name);
|
||||
}
|
||||
public void Save(string FileName)
|
||||
{
|
||||
string FileDirectory = System.IO.Path.GetDirectoryName(FileName);
|
||||
if (!System.IO.Directory.Exists(FileDirectory))
|
||||
{
|
||||
System.IO.Directory.CreateDirectory(FileDirectory);
|
||||
}
|
||||
System.IO.File.WriteAllBytes(FileName, GetDataAsByteArray());
|
||||
}
|
||||
|
||||
private long? mvarSize = null;
|
||||
public long Size
|
||||
{
|
||||
get
|
||||
{
|
||||
if (mvarSize != null)
|
||||
{
|
||||
return mvarSize.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mvarData != null)
|
||||
{
|
||||
return mvarData.Length;
|
||||
}
|
||||
else if (mvarStream != null)
|
||||
{
|
||||
return mvarStream.Length;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
set
|
||||
{
|
||||
mvarSize = value;
|
||||
}
|
||||
}
|
||||
|
||||
public void ResetSize()
|
||||
{
|
||||
mvarSize = null;
|
||||
}
|
||||
|
||||
#region Metadata
|
||||
private string mvarTitle = null;
|
||||
public string Title { get { return mvarTitle; } set { mvarTitle = value; } }
|
||||
|
||||
private string mvarDescription = null;
|
||||
public string Description { get { return mvarDescription; } set { mvarDescription = value; } }
|
||||
#endregion
|
||||
|
||||
private Dictionary<string, object> mvarProperties = new Dictionary<string,object>();
|
||||
public Dictionary<string, object> Properties { get { return mvarProperties; } }
|
||||
|
||||
private DateTime mvarModificationTimestamp = DateTime.Now;
|
||||
public DateTime ModificationTimestamp { get { return mvarModificationTimestamp; } set { mvarModificationTimestamp = value; } }
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user