public int Push(byte[] bytes,int count)
{
int termLen = m_TerminaTor.Length;
if(bytes.Length > termLen){
throw new Exception("bytes.Length is too big, can't be more than terminator.length !");
}
if(count > termLen){
throw new Exception("count is too big, can't be more than terminator.length !");
}
// Move stack bytes which will stay and append new ones
// Array.Copy(m_SackList,count,m_SackList,0,m_SackList.Length - count);
// Array.Copy(bytes,0,m_SackList,m_SackList.Length - count,count);
// Code above is slower than code below, when message size > 5 mb
// Append new bytes to end and remove first bytes
if(count != termLen){
byte[] newStack = new byte[termLen];
for(int i=0;i<termLen;i++){
// Write old bytes
if(termLen - count > i){
newStack[i] = m_SackList[count + i];
}
// Write new bytes
else{
newStack[i] = bytes[i - (termLen - count)];
}
}
m_SackList = newStack;
}
// Push count is equal to stack, just set is as new stack
else{
m_SackList = bytes;
}
// int index = Array.IndexOf(m_SackList,m_TerminaTor[0]);
// Code above is slower than code below, when message size > 5 mb
int index = -1;
for(int i=0;i<termLen;i++){
if(m_SackList[i] == m_TerminaTor[0]){
index = i;
break;
}
}
if(index > -1){
if(index == 0){
// Check if contains full terminator
for(int i=0;i<m_SackList.Length;i++){
if(m_SackList[i] != m_TerminaTor[i]){
return 1;
}
}
return 0; // If reaches so far, contains terminator
}
return 1;
}
else{
return termLen;
}
}