1
Answer

very fast binary search in files

haerry

haerry

20y
3.1k
1
I have to search fast for patterns in a binary file. Does anybody know how to find a byte sequence like "%%EOF\r\n" fast in a binary file ? Textreader does not work, use of Encoding class makes the probleme even worser and therefore string.Find does not work. ArrayLists are cool but there i can only search for a single byte. Additionaly the extra performance cost for boxing/unboxing is not acceptable. Any hints highly appreciated.
Answers (1)
0
spgilmore

spgilmore

NA 591 0 20y
You can do this in a single scan. Use a filestream and read one byte at a time. Just search for the first byte in teh sequence. When you find it, compare the rest of sequence to the contents of the file at that location. If not a match, continue. Reading only one byte at a time is not efficient, but the filestream buffers the reads, I think. If it's still too slow, just modify the code to read and test 512 bytes at a time (which is good for NTFS) or 2048 at a time (which is good for CDs or FAT).