-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathStreamExtensions.cs
More file actions
82 lines (74 loc) · 2.25 KB
/
Copy pathStreamExtensions.cs
File metadata and controls
82 lines (74 loc) · 2.25 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
using System.Runtime.InteropServices;
namespace BytecodeApi.Extensions;
/// <summary>
/// Provides a set of <see langword="static" /> methods for interaction with <see cref="Stream" /> objects.
/// </summary>
public static class StreamExtensions
{
extension(Stream stream)
{
/// <summary>
/// Reads a structure of the specified type from the stream.
/// </summary>
/// <typeparam name="T">The type of the structure to read.</typeparam>
/// <returns>
/// A new structure of the specified type that was read from the stream.
/// </returns>
public T ReadStructure<T>() where T : struct
{
Check.ArgumentNull(stream);
byte[] buffer = new byte[Marshal.SizeOf<T>()];
stream.ReadExactly(buffer);
return Convert.ToStructure<T>(buffer);
}
/// <summary>
/// Writes a structure of the specified type to the stream.
/// </summary>
/// <typeparam name="T">The type of the structure to write.</typeparam>
/// <param name="structure">The structure to write to this <see cref="Stream" />.</param>
public void WriteStructure<T>(T structure) where T : struct
{
Check.ArgumentNull(stream);
stream.Write(Convert.FromStructure(structure));
}
/// <summary>
/// Searches this <see cref="Stream" /> for the first occurrence of <paramref name="sequence" />. If not found, returns -1.
/// </summary>
/// <param name="sequence">The <see cref="byte" />[] to search for.</param>
/// <returns>
/// The index of the first occurrence of <paramref name="sequence" /> and -1, if not found.
/// </returns>
public long FindSequence(byte[] sequence)
{
Check.ArgumentNull(stream);
Check.ArgumentNull(sequence);
Check.ArgumentEx.ArrayElementsRequired(sequence);
long position = stream.Position;
try
{
int readByte;
int sequenceIndex = 0;
for (long index = 0; (readByte = stream.ReadByte()) != -1; index++)
{
if (readByte == sequence[sequenceIndex])
{
sequenceIndex++;
if (sequenceIndex == sequence.Length)
{
return index + 1 - sequence.Length;
}
}
else
{
sequenceIndex = 0;
}
}
}
finally
{
stream.Seek(position, SeekOrigin.Begin);
}
return -1;
}
}
}