I have this following code:
var file = //Memory stream with a file in it
var bytes = file.ToArray();
I need to search the bytes for the first occurrence (if any) of the specified byte sequence: 0xff, 0xd8. (The purpose of this is to find images embedded in files)
So if for example bytes[6501] contains 0xff and bytes[6502] contains 0xd8, thats a match and I need either the index of the position returned (6501), or a new array, which is a copy of the bytes array, except it doesn't have the keys below 6501 from the old array.
My current solution is to loop:
for (var index = 0; index < bytes.Length; index++)
{
if((new byte[] {0xff, 0xd8}).SequenceEqual(bytes.Skip(index).Take(2))
...
But it's pretty slow when it's handling bigger files.
Is there some more efficient way to handle this?
byte[]in each iteration of the loop?