Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Resources.cs
1using System;
2using System.Reflection;
3using System.IO;
4using System.Security.Cryptography.X509Certificates;
6using System.Threading.Tasks;
7using System.Text;
8
9namespace Waher.Content
10{
14 public static class Resources
15 {
22 public static Assembly GetAssembly(string ResourceName)
23 {
24 string[] Parts = ResourceName.Split('.');
25 string ParentNamespace;
26 int i, c;
27 Assembly A;
28
29 if (!Types.IsRootNamespace(Parts[0]))
30 A = null;
31 else
32 {
33 ParentNamespace = Parts[0];
34 i = 1;
35 c = Parts.Length;
36
37 while (i < c)
38 {
39 if (!Types.IsSubNamespace(ParentNamespace, Parts[i]))
40 break;
41
42 ParentNamespace += "." + Parts[i];
43 i++;
44 }
45
47 }
48
49 if (A is null)
50 throw new ArgumentException("Assembly not found for resource " + ResourceName + ".", nameof(ResourceName));
51
52 return A;
53 }
54
61 public static byte[] LoadResource(string ResourceName)
62 {
63 return LoadResource(ResourceName, GetAssembly(ResourceName));
64 }
65
73 public static byte[] LoadResource(string ResourceName, Assembly Assembly)
74 {
75 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
76 {
77 if (f is null)
78 throw new ArgumentException("Resource not found: " + ResourceName, nameof(ResourceName));
79
80 if (f.Length > int.MaxValue)
81 throw new ArgumentException("Resource size exceeds " + int.MaxValue.ToString() + " bytes.", nameof(ResourceName));
82
83 int Len = (int)f.Length;
84 byte[] Result = new byte[Len];
85 f.ReadAll(Result, 0, Len);
86 return Result;
87 }
88 }
89
96 public static string LoadResourceAsText(string ResourceName)
97 {
98 return LoadResourceAsText(ResourceName, GetAssembly(ResourceName));
99 }
100
108 public static string LoadResourceAsText(string ResourceName, Assembly Assembly)
109 {
110 using (Stream f = Assembly.GetManifestResourceStream(ResourceName))
111 {
112 if (f is null)
113 throw new ArgumentException("Resource not found: " + ResourceName, nameof(ResourceName));
114
115 if (f.Length > int.MaxValue)
116 throw new ArgumentException("Resource size exceeds " + int.MaxValue.ToString() + " bytes.", nameof(ResourceName));
117
118 int Len = (int)f.Length;
119 byte[] Result = new byte[Len];
120 f.ReadAll(Result, 0, Len);
121
122 return CommonTypes.GetString(Result, Encoding.UTF8);
123 }
124 }
125
132 public static X509Certificate2 LoadCertificate(string ResourceName)
133 {
134 return (LoadCertificate(ResourceName, GetAssembly(ResourceName)));
135 }
136
144 public static X509Certificate2 LoadCertificate(string ResourceName, Assembly Assembly)
145 {
146 return LoadCertificate(ResourceName, null, Assembly);
147 }
148
156 public static X509Certificate2 LoadCertificate(string ResourceName, string Password)
157 {
158 return LoadCertificate(ResourceName, Password, GetAssembly(ResourceName));
159 }
160
169 public static X509Certificate2 LoadCertificate(string ResourceName, string Password, Assembly Assembly)
170 {
171 byte[] Data = LoadResource(ResourceName, Assembly);
172 if (Password is null)
173 return new X509Certificate2(Data);
174 else
175 return new X509Certificate2(Data, Password);
176 }
177
183 public static async Task<byte[]> ReadAllBytesAsync(string FileName)
184 {
185 using (FileStream fs = File.OpenRead(FileName))
186 {
187 long l = fs.Length;
188 if (l > int.MaxValue)
189 throw new NotSupportedException("File too large.");
190
191 int Len = (int)l;
192 byte[] Bin = new byte[Len];
193
194 await fs.ReadAsync(Bin, 0, Len);
195
196 return Bin;
197 }
198 }
199
205 public static async Task<string> ReadAllTextAsync(string FileName)
206 {
207 byte[] Bin = await ReadAllBytesAsync(FileName);
208 return CommonTypes.GetString(Bin, Encoding.UTF8);
209 }
210
216 public static Task WriteAllBytesAsync(string FileName, byte[] Data)
217 {
218 return WriteAllBytesAsync(FileName, Data, 0, Data.Length);
219 }
220
228 public static async Task WriteAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
229 {
230 using (FileStream fs = File.Create(FileName))
231 {
232 await fs.WriteAsync(Data, Offset, Length);
233 }
234 }
235
241 public static Task AppendAllBytesAsync(string FileName, byte[] Data)
242 {
243 return AppendAllBytesAsync(FileName, Data, 0, Data.Length);
244 }
245
253 public static async Task AppendAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
254 {
255 using (FileStream fs = File.OpenWrite(FileName))
256 {
257 fs.Position = fs.Length;
258 await fs.WriteAsync(Data, Offset, Length);
259 }
260 }
261
267 public static Task WriteAllTextAsync(string FileName, string Text)
268 {
269 return WriteAllTextAsync(FileName, Text, Encoding.UTF8);
270 }
271
278 public static async Task WriteAllTextAsync(string FileName, string Text, Encoding Encoding)
279 {
280 using (FileStream fs = File.Create(FileName))
281 {
282 byte[] Preamble = Encoding.GetPreamble();
283 byte[] Data = Encoding.GetBytes(Text);
284 int i, c = Preamble.Length;
285
286 if (c > 0)
287 {
288 if (c > Data.Length)
289 i = 0;
290 else
291 {
292 for (i = 0; i < c; i++)
293 {
294 if (Preamble[i] != Data[i])
295 break;
296 }
297 }
298
299 if (i < c)
300 await fs.WriteAsync(Preamble, 0, c);
301 }
302
303 await fs.WriteAsync(Data, 0, Data.Length);
304 }
305 }
306
307 }
308}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string GetString(byte[] Data, Encoding DefaultEncoding)
Gets a string from its binary representation, taking any Byte Order Mark (BOM) into account.
Static class managing loading of resources stored as embedded resources or in content files.
Definition: Resources.cs:15
static byte[] LoadResource(string ResourceName, Assembly Assembly)
Loads a resource from an embedded resource.
Definition: Resources.cs:73
static Assembly GetAssembly(string ResourceName)
Gets the assembly corresponding to a given resource name.
Definition: Resources.cs:22
static async Task< byte[]> ReadAllBytesAsync(string FileName)
Reads a binary file asynchronously.
Definition: Resources.cs:183
static async Task AppendAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
Appends a binary file asynchronously.
Definition: Resources.cs:253
static X509Certificate2 LoadCertificate(string ResourceName)
Loads a certificate from an embedded resource.
Definition: Resources.cs:132
static byte[] LoadResource(string ResourceName)
Loads a resource from an embedded resource.
Definition: Resources.cs:61
static string LoadResourceAsText(string ResourceName)
Loads a text resource from an embedded resource.
Definition: Resources.cs:96
static Task WriteAllBytesAsync(string FileName, byte[] Data)
Creates a binary file asynchronously.
Definition: Resources.cs:216
static async Task WriteAllBytesAsync(string FileName, byte[] Data, int Offset, int Length)
Creates a binary file asynchronously.
Definition: Resources.cs:228
static async Task< string > ReadAllTextAsync(string FileName)
Reads a text file asynchronously.
Definition: Resources.cs:205
static Task AppendAllBytesAsync(string FileName, byte[] Data)
Appends a binary file asynchronously.
Definition: Resources.cs:241
static X509Certificate2 LoadCertificate(string ResourceName, string Password)
Loads a certificate from an embedded resource.
Definition: Resources.cs:156
static X509Certificate2 LoadCertificate(string ResourceName, string Password, Assembly Assembly)
Loads a certificate from an embedded resource.
Definition: Resources.cs:169
static async Task WriteAllTextAsync(string FileName, string Text, Encoding Encoding)
Creates a text file asynchronously.
Definition: Resources.cs:278
static string LoadResourceAsText(string ResourceName, Assembly Assembly)
Loads a text resource from an embedded resource.
Definition: Resources.cs:108
static Task WriteAllTextAsync(string FileName, string Text)
Creates a text file asynchronously.
Definition: Resources.cs:267
static X509Certificate2 LoadCertificate(string ResourceName, Assembly Assembly)
Loads a certificate from an embedded resource.
Definition: Resources.cs:144
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static bool IsSubNamespace(string Namespace, string LocalName)
Checks if a local name in LocalName represents a subnamespace from the point of view of the namespac...
Definition: Types.cs:223
static Assembly GetFirstAssemblyReferenceInNamespace(string Namespace)
Gets the assembly reference of the first type found in a namespace.
Definition: Types.cs:142
static bool IsRootNamespace(string Name)
Checks if a name is a root namespace.
Definition: Types.cs:184