I'm new in C#, I want to count up a byte pattern inside of file. Everything is ok when I try to read a small file (8MB) and got 1907 in result, but when I try to read a large one (50MB) the app is freezing and return nothing. This is my code so far ..
public long chunkMethodCW()
{
int incomingOffset = 0;
byte[] outboundBuffer = new byte[1024];
long CW = 0;
KMP kmp = new KMP(header); // header = pattern in hex
while(incomingOffset < data.Length)
{
int length = Math.Min(outboundBuffer.Length,data.Length - incomingOffset);
Buffer.BlockCopy(data,incomingOffset,outboundBuffer,0,length);
incomingOffset += length;
//CW += kmp.match(outboundBuffer);
CW++;
}
return CW;
}
// KMP Class
public class KMP
{
private int[] F;
private byte[] pat;
private int m;
public KMP(byte[] pattern)
{
pat = pattern;
m = pattern.Length;
F = new int[m + 1];
for (int i = 2, j; i <= m; i++)
{
j = F[i - 1];
if (pattern[j] == pattern[i - 1])
{
F[i] = j + i;
continue;
}
while(j > 0 && pat[j] != pat[i-1])
{
j = F[j];
}
F[i] = pat[j] != pat[i - 1] ? 0 : j + 1;
}
}
public int match(byte[] data)
{
int n = data.Length, pi = 0, ti = 0;
int matches = 0;
while (ti < n)
{
if (pi == m)
{
matches++;
pi = 0;
pi = F[pi];
}
if (data[ti] == pat[pi])
{
pi++;
ti++;
}
else if (pi > 0)
{
pi = F[pi];
}
else
{
ti++;
}
}
if (pi == m)
{
matches++;
pi = 0;
pi = F[pi];
}
return matches;
}
}
Aucun commentaire:
Enregistrer un commentaire