Skip to content

Commit bf428d1

Browse files
committed
Convert the StartsWith(byte[] src, byte[] pattern) method to an extension method
1 parent 0a6fdca commit bf428d1

2 files changed

Lines changed: 24 additions & 21 deletions

File tree

Source/HttpMultipartParser/Extensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,24 @@ public static IEnumerable<string> GetParameterValues(this IMultipartFormDataPars
4949
.Where(p => p.Name.Equals(name, comparisonType))
5050
.Select(p => p.Data);
5151
}
52+
53+
/// <summary>
54+
/// Determines if the source byte array starts with the specified pattern.
55+
/// </summary>
56+
/// <param name="src">The source byte array.</param>
57+
/// <param name="pattern">The pattern.</param>
58+
/// <returns>True if the source byte array starts with the specified pattern, false otherwise.</returns>
59+
internal static bool StartsWith(this byte[] src, byte[] pattern)
60+
{
61+
if (src == null || pattern == null) return false;
62+
if (src.Length < pattern.Length) return false;
63+
64+
for (int i = 0; i < pattern.Length - 1; i++)
65+
{
66+
if (src[i] != pattern[i]) return false;
67+
}
68+
69+
return true;
70+
}
5271
}
5372
}

Source/HttpMultipartParser/RebufferableBinaryReader.cs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public class RebufferableBinaryReader
6262
/// </summary>
6363
private readonly BinaryStreamStack streamStack;
6464

65+
/// <summary>
66+
/// The BOM (AKA preamble) for the encoding.
67+
/// </summary>
6568
private readonly byte[] preamble;
6669

6770
#endregion
@@ -290,7 +293,7 @@ public string ReadLine()
290293
byte[] data = ReadByteLine();
291294

292295
if (data == null) return null;
293-
else if (StartsWith(data, preamble)) return encoding.GetString(data.Skip(preamble.Length).ToArray());
296+
else if (data.StartsWith(preamble)) return encoding.GetString(data.Skip(preamble.Length).ToArray());
294297
else return encoding.GetString(data);
295298
}
296299

@@ -456,33 +459,14 @@ public async Task<string> ReadLineAsync(CancellationToken cancellationToken = de
456459
byte[] data = await ReadByteLineAsync(cancellationToken).ConfigureAwait(false);
457460

458461
if (data == null) return null;
459-
else if (StartsWith(data, preamble)) return encoding.GetString(data.Skip(preamble.Length).ToArray());
462+
else if (data.StartsWith(preamble)) return encoding.GetString(data.Skip(preamble.Length).ToArray());
460463
else return encoding.GetString(data);
461464
}
462465

463466
#endregion
464467

465468
#region Methods
466469

467-
/// <summary>
468-
/// Determines if the source byte array starts with the specified pattern.
469-
/// </summary>
470-
/// <param name="src">The source byte array.</param>
471-
/// <param name="pattern">The pattern.</param>
472-
/// <returns>True if the source byte array starts with the specified pattern, false otherwise.</returns>
473-
private static bool StartsWith(byte[] src, byte[] pattern)
474-
{
475-
if (src == null || pattern == null) return false;
476-
if (src.Length < pattern.Length) return false;
477-
478-
for (int i = 0; i < pattern.Length - 1; i++)
479-
{
480-
if (src[i] != pattern[i]) return false;
481-
}
482-
483-
return true;
484-
}
485-
486470
/// <summary>
487471
/// Reads more data from the stream into the stream stack.
488472
/// </summary>

0 commit comments

Comments
 (0)