Skip to content

Commit f838094

Browse files
authored
Merge pull request #3 from TechieGuy12/memoryusagefix
Memory usage fix and additional functionality
2 parents 2c7f6a7 + 0485ab5 commit f838094

31 files changed

+1947
-495
lines changed

FileVerification/CheckSumFile.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum ChecksumFileLayout
2020
/// <summary>
2121
/// The string representation of the hash algorithm.
2222
/// </summary>
23-
HASH_ALGORITHM,
23+
HASHALGORITHM,
2424
/// <summary>
2525
/// The hash of the file.
2626
/// </summary>
@@ -32,7 +32,7 @@ public class ChecksumFile
3232
/// <summary>
3333
/// The default checksum file name.
3434
/// </summary>
35-
public const string DEFAULT_CHECKSUM_FILENAME = "__fv.txt";
35+
public const string DEFAULTCHECKSUMFILENAME = "__fv.txt";
3636

3737
/// <summary>
3838
/// Gets the directory where the checksum file is located.
@@ -138,7 +138,7 @@ public void Read()
138138
HashInfo info =
139139
new HashInfo(
140140
fileName,
141-
values[(int)ChecksumFileLayout.HASH_ALGORITHM],
141+
values[(int)ChecksumFileLayout.HASHALGORITHM],
142142
values[(int)ChecksumFileLayout.HASH]);
143143

144144
// Get the full path to the file to use as the key to make
@@ -285,8 +285,10 @@ public void Write()
285285
try
286286
{
287287
// Write the file hash information to the checksum file
288-
using StreamWriter sw = new StreamWriter(FullPath);
289-
sw.Write(string.Join("", info));
288+
using (StreamWriter sw = new StreamWriter(FullPath))
289+
{
290+
sw.Write(string.Join("", info));
291+
}
290292
}
291293
catch (DirectoryNotFoundException)
292294
{

FileVerification/Configuration/Notifications/Data.cs renamed to FileVerification/Configuration/Data.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
using System.Text;
55
using System.Threading.Tasks;
66
using System.Xml.Serialization;
7+
using TE.FileVerification.Net;
78

8-
namespace TE.FileVerification.Configuration.Notifications
9+
namespace TE.FileVerification.Configuration
910
{
1011
/// <summary>
1112
/// Contains the data used to send the request.
@@ -19,13 +20,13 @@ public class Data
1920
/// Gets or sets the headers for the request.
2021
/// </summary>
2122
[XmlElement("headers")]
22-
public Headers Headers { get; set; }
23+
public Headers? Headers { get; set; }
2324

2425
/// <summary>
2526
/// Gets or sets the body for the request.
2627
/// </summary>
2728
[XmlElement("body")]
28-
public string Body { get; set; }
29+
public string? Body { get; set; }
2930

3031
/// <summary>
3132
/// Gets or sets the MIME type string value.
@@ -39,7 +40,7 @@ public string MimeTypeString
3940
}
4041
set
4142
{
42-
_mimeType = (value == Request.JSON_NAME || value == Request.XML_NAME) ? value : Request.JSON_NAME;
43+
_mimeType = value == Request.JSON_NAME || value == Request.XML_NAME ? value : Request.JSON_NAME;
4344
}
4445
}
4546

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using TE.FileVerification.IO;
3+
4+
namespace TE.FileVerification.Configuration
5+
{
6+
/// <summary>
7+
/// An exclusions node in the XML file.
8+
/// </summary>
9+
public class Exclusions : MatchBase
10+
{
11+
/// <summary>
12+
/// Returns the flag indicating if the file/folder is to be ignored.
13+
/// </summary>
14+
/// <<param name="fullPath">
15+
/// The full path to the file or folder.
16+
/// </param>
17+
/// <returns>
18+
/// True if the file/folder is to be ignored, otherwise false.
19+
/// </returns>
20+
public bool Exclude(string fullPath)
21+
{
22+
FilterTypeName = "Exclude";
23+
return IsMatchFound(fullPath);
24+
}
25+
}
26+
}

FileVerification/Configuration/Notifications/Header.cs renamed to FileVerification/Configuration/Header.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
using System.Threading.Tasks;
66
using System.Xml.Serialization;
77

8-
namespace TE.FileVerification.Configuration.Notifications
8+
namespace TE.FileVerification.Configuration
99
{
1010
public class Header
1111
{
1212
[XmlElement("name")]
13-
public string Name { get; set; }
13+
public string? Name { get; set; }
1414

1515
[XmlElement("value")]
16-
public string Value { get; set; }
16+
public string? Value { get; set; }
1717
}
1818
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using System.Linq;
5+
using System.Net.Http;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using System.Xml.Serialization;
9+
10+
namespace TE.FileVerification.Configuration
11+
{
12+
public class Headers
13+
{
14+
[XmlElement("header")]
15+
public Collection<Header>? HeaderList { get; set; }
16+
17+
/// <summary>
18+
/// Sets the headers for a request.
19+
/// </summary>
20+
/// <param name="request">
21+
/// The request that will include the headers.
22+
/// </param>
23+
public void Set(HttpRequestMessage request)
24+
{
25+
if (request == null)
26+
{
27+
return;
28+
}
29+
30+
if (HeaderList == null || HeaderList.Count <= 0)
31+
{
32+
return;
33+
}
34+
35+
foreach (Header header in HeaderList)
36+
{
37+
if (!string.IsNullOrWhiteSpace(header.Name))
38+
{
39+
request.Headers.Add(header.Name, header.Value);
40+
}
41+
}
42+
}
43+
}
44+
}

FileVerification/Configuration/Notifications/Notification.cs renamed to FileVerification/Configuration/Notification.cs

Lines changed: 72 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,27 @@
77
using System.Threading.Tasks;
88
using System.Xml.Serialization;
99
using System.Text.Json;
10+
using System.Globalization;
11+
using TE.FileVerification.Net;
1012

11-
namespace TE.FileVerification.Configuration.Notifications
13+
namespace TE.FileVerification.Configuration
1214
{
1315
public class Notification
1416
{
1517
// The message to send with the request.
16-
private StringBuilder _message;
18+
private readonly StringBuilder _message;
1719

1820
/// <summary>
1921
/// Gets or sets the URL of the request.
2022
/// </summary>
2123
[XmlElement("url")]
22-
public string Url { get; set; }
24+
public string? Url { get; set; }
2325

2426
/// <summary>
2527
/// Gets the URI value of the string URL.
2628
/// </summary>
2729
[XmlIgnore]
28-
public Uri Uri
30+
public Uri? Uri
2931
{
3032
get
3133
{
@@ -46,12 +48,12 @@ public Uri Uri
4648
}
4749
}
4850
}
49-
51+
5052
/// <summary>
5153
/// Gets or sets the string representation of the request method.
5254
/// </summary>
5355
[XmlElement("method")]
54-
public string MethodString { get; set; }
56+
public string MethodString { get; set; } = "Post";
5557

5658
/// <summary>
5759
/// Gets the request method.
@@ -69,7 +71,7 @@ public HttpMethod Method
6971

7072
try
7173
{
72-
method = (HttpMethod)Enum.Parse(typeof(HttpMethod), MethodString.ToUpper(), true);
74+
method = (HttpMethod)Enum.Parse(typeof(HttpMethod), MethodString.ToUpper(CultureInfo.CurrentCulture), true);
7375
}
7476
catch (Exception ex)
7577
when (ex is ArgumentNullException || ex is ArgumentException || ex is OverflowException)
@@ -85,7 +87,7 @@ public HttpMethod Method
8587
/// Gets or sets the data to send for the request.
8688
/// </summary>
8789
[XmlElement("data")]
88-
public Data Data { get; set; }
90+
public Data? Data { get; set; }
8991

9092
/// <summary>
9193
/// Returns a value indicating if there is a message waiting to be sent
@@ -121,36 +123,44 @@ public Notification()
121123
/// </param>
122124
internal void QueueRequest(string message)
123125
{
124-
//_message.Append(CleanMessage(message) + @"\n");
125-
_message.Append(message);
126+
_message.Append(CleanMessage(message) + @"\n");
126127
}
127128

128129
/// <summary>
129130
/// Send the notification request.
130131
/// </summary>
131-
/// <exception cref="NullReferenceException">
132+
/// <exception cref="InvalidOperationException">
132133
/// Thrown when the URL is null or empty.
133134
/// </exception>
134-
internal HttpResponseMessage Send()
135+
internal Response? Send()
135136
{
136137
// If there isn't a message to be sent, then just return
137-
if (_message?.Length <= 0)
138+
if (_message == null || _message.Length <= 0)
138139
{
139140
return null;
140141
}
141142

142-
if (Uri == null)
143+
if (GetUri() == null)
144+
{
145+
throw new InvalidOperationException("The URL is null or empty.");
146+
}
147+
148+
if (Data == null)
143149
{
144-
throw new NullReferenceException("The URL is null or empty.");
150+
throw new InvalidOperationException("Data for the request was not provided.");
145151
}
146152

147-
string content = Data.Body.Replace("[message]", cleanForJSON(_message.ToString()));
153+
string content = string.Empty;
154+
if (Data.Body != null)
155+
{
156+
content = Data.Body.Replace("[message]", _message.ToString(), StringComparison.OrdinalIgnoreCase);
157+
}
148158

149-
HttpResponseMessage response =
159+
Response response =
150160
Request.Send(
151161
Method,
152-
Uri,
153-
Data.Headers.HeaderList,
162+
GetUri(),
163+
Data.Headers,
154164
content,
155165
Data.MimeType);
156166

@@ -161,37 +171,46 @@ internal HttpResponseMessage Send()
161171
/// <summary>
162172
/// Send the notification request.
163173
/// </summary>
164-
/// <exception cref="NullReferenceException">
174+
/// <exception cref="InvalidOperationException">
165175
/// Thrown when the URL is null or empty.
166176
/// </exception>
167-
internal async Task<HttpResponseMessage> SendAsync()
177+
internal async Task<Response?> SendAsync()
168178
{
169179
// If there isn't a message to be sent, then just return
170-
if (_message?.Length <= 0)
180+
if (_message == null || _message.Length <= 0)
171181
{
172182
return null;
173183
}
174184

175-
if (Uri == null)
185+
if (GetUri() == null)
186+
{
187+
throw new InvalidOperationException("The URL is null or empty.");
188+
}
189+
190+
if (Data == null)
191+
{
192+
throw new InvalidOperationException("Data for the request was not provided.");
193+
}
194+
195+
string content = string.Empty;
196+
if (Data.Body != null)
176197
{
177-
throw new NullReferenceException("The URL is null or empty.");
198+
content = Data.Body.Replace("[message]", _message.ToString(), StringComparison.OrdinalIgnoreCase);
178199
}
179-
180-
string content = Data.Body.Replace("[message]", _message.ToString());
181200

182-
HttpResponseMessage response =
201+
Response response =
183202
await Request.SendAsync(
184203
Method,
185-
Uri,
186-
Data.Headers.HeaderList,
204+
GetUri(),
205+
Data.Headers,
187206
content,
188-
Data.MimeType);
207+
Data.MimeType).ConfigureAwait(false);
189208

190209
_message.Clear();
191-
return response;
210+
return response;
192211
}
193212

194-
public static string cleanForJSON(string s)
213+
public static string CleanMessage(string s)
195214
{
196215
if (s == null || s.Length == 0)
197216
{
@@ -201,8 +220,8 @@ public static string cleanForJSON(string s)
201220
char c = '\0';
202221
int i;
203222
int len = s.Length;
204-
StringBuilder sb = new StringBuilder(len + 4);
205-
String t;
223+
StringBuilder sb = new(len + 4);
224+
string t;
206225

207226
for (i = 0; i < len; i += 1)
208227
{
@@ -236,8 +255,8 @@ public static string cleanForJSON(string s)
236255
default:
237256
if (c < ' ')
238257
{
239-
t = "000" + String.Format("X", c);
240-
sb.Append("\\u" + t.Substring(t.Length - 4));
258+
t = "000" + string.Format(CultureInfo.CurrentCulture, "{0:X}", c);
259+
sb.Append(string.Concat("\\u", t.AsSpan(t.Length - 4)));
241260
}
242261
else
243262
{
@@ -248,5 +267,22 @@ public static string cleanForJSON(string s)
248267
}
249268
return sb.ToString();
250269
}
270+
271+
/// <summary>
272+
/// Gets the URI value of the string URL.
273+
/// </summary>
274+
/// <exception cref="UriFormatException">
275+
/// Thrown if the URL is not in a valid format.
276+
/// </exception>
277+
private Uri GetUri()
278+
{
279+
if (string.IsNullOrWhiteSpace(Url))
280+
{
281+
throw new UriFormatException();
282+
}
283+
284+
Uri uri = new(Url);
285+
return uri;
286+
}
251287
}
252288
}

0 commit comments

Comments
 (0)