您如何看待在System.Stream中找到给定字节序列开始(第一次出现)的位置的最佳方法是什么:
public static long FindPosition(Stream stream, byte[] byteSequence) { long position = -1; /// ??? return position; }
PS提供最简单但最快的解决方案。:)
我已经找到了解决方案。
我做了一些基准测试与是一个ASCII文件3.050 KB和38803 lines。与搜索byte array的22 bytes在文件的最后一行,我得到了在有关结果2.28秒(在慢/旧机)。
3.050 KB
38803 lines
byte
array
22 bytes
2.28
public static long FindPosition(Stream stream, byte[] byteSequence) { if (byteSequence.Length > stream.Length) return -1; byte[] buffer = new byte[byteSequence.Length]; using (BufferedStream bufStream = new BufferedStream(stream, byteSequence.Length)) { int i; while ((i = bufStream.Read(buffer, 0, byteSequence.Length)) == byteSequence.Length) { if (byteSequence.SequenceEqual(buffer)) return bufStream.Position - byteSequence.Length; else bufStream.Position -= byteSequence.Length - PadLeftSequence(buffer, byteSequence); } } return -1; } private static int PadLeftSequence(byte[] bytes, byte[] seqBytes) { int i = 1; while (i < bytes.Length) { int n = bytes.Length - i; byte[] aux1 = new byte[n]; byte[] aux2 = new byte[n]; Array.Copy(bytes, i, aux1, 0, n); Array.Copy(seqBytes, aux2, n); if (aux1.SequenceEqual(aux2)) return i; i++; } return i; }