-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathBlob.cs
More file actions
117 lines (109 loc) · 4.03 KB
/
Blob.cs
File metadata and controls
117 lines (109 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using BytecodeApi.Extensions;
using System.Diagnostics;
namespace BytecodeApi.Data;
/// <summary>
/// Represents an entity composed of a name and binary content in form or a <see cref="byte" />[].
/// </summary>
[DebuggerDisplay($"{nameof(Blob)}: Name = {{Name}}, Content = {{Content}}")]
public class Blob
{
/// <summary>
/// Gets or sets the name of the <see cref="Blob" />.
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets the binary content of the <see cref="Blob" />.
/// </summary>
public byte[] Content { get; set; }
/// <summary>
/// Gets or sets the object that contains data about the <see cref="Blob" />.
/// </summary>
public object? Tag { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="Blob" /> class.
/// </summary>
public Blob()
{
Name = "";
Content = Array.Empty<byte>();
}
/// <summary>
/// Initializes a new instance of the <see cref="Blob" /> class with the specified name.
/// </summary>
/// <param name="name">The name of the <see cref="Blob" />.</param>
public Blob(string name) : this()
{
Name = name;
}
/// <summary>
/// Initializes a new instance of the <see cref="Blob" /> class with the specified name and content.
/// </summary>
/// <param name="name">The name of the <see cref="Blob" />.</param>
/// <param name="content">The binary content of the <see cref="Blob" />.</param>
public Blob(string name, byte[] content) : this(name)
{
Content = content;
}
/// <summary>
/// Initializes a new instance of the <see cref="Blob" /> class with the specified name, content and a tag.
/// </summary>
/// <param name="name">The name of the <see cref="Blob" />.</param>
/// <param name="content">The binary content of the <see cref="Blob" />.</param>
/// <param name="tag">the object that contains data about the <see cref="Blob" />.</param>
public Blob(string name, byte[] content, object? tag) : this(name, content)
{
Tag = tag;
}
/// <summary>
/// Creates a <see cref="Blob" /> from the specified file.
/// </summary>
/// <param name="path">A <see cref="string" /> specifying the path of a file from which to create the <see cref="Blob" />.</param>
/// <returns>
/// The <see cref="Blob" /> this method creates.
/// </returns>
public static Blob FromFile(string path)
{
Check.ArgumentNull(path);
Check.FileNotFound(path);
return new(Path.GetFileName(path), File.ReadAllBytes(path));
}
/// <summary>
/// Compares this <see cref="Blob" /> agains another <see cref="Blob" />, including binary content. Returns <see langword="true" />, if both objects contain the exact same set of data.
/// </summary>
/// <param name="other">A <see cref="Blob" /> to compare to this instance to.</param>
/// <returns>
/// <see langword="true" />, if both objects contain the exact same set of data;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Compare([NotNullWhen(true)] Blob? other)
{
return other != null && (this == other || Name == other.Name && Content.Compare(other.Content) && Equals(Tag, other.Tag));
}
/// <summary>
/// Writes the contents of <see cref="Content" /> to a binary file.
/// </summary>
/// <param name="path">A <see cref="string" /> specifying the path of a file to which <see cref="Content" /> is written to.</param>
public void Save(string path)
{
Check.ArgumentNull(path);
File.WriteAllBytes(path, Content);
}
/// <summary>
/// Returns the name of this <see cref="Blob" />.
/// </summary>
/// <returns>
/// The name of this <see cref="Blob" />.
/// </returns>
public override string ToString()
{
return Name;
}
internal static Exception CreateIllegalFilenameException(Blob blob)
{
return Throw.InvalidOperation($"Blob with the name '{blob.Name}' has illegal filename characters.");
}
internal static Exception CreateIllegalFilenameException(BlobTreeNode node)
{
return Throw.InvalidOperation($"Blob tree node with the name '{node.Name}' has illegal filename characters.");
}
}