Selasa, 02 November 2010

Byte Process

(Maaf kalo ga bersesuaian nama nama fungsinya) Ini adalah kumpulan fungsi-fungsi byte di C# yang kubuat sendiri. Fungsi2 dibawah ini tidak untuk dipakai dalam jumlah byte pada byte array (byte[]) lebih dari 32000, karena proses akan terasa lambat.

BytePush: menambahkan byte array (byte[]) ke belakang byte array yg sudah ada.

public static void BytesPush(ref byte[] toAdd, byte[] addBytes)
{
  if (addBytes.Length == 0) return; 
  byte[] bytesTemp = new byte[toAdd.Length + addBytes.Length]; 
  if (toAdd.Length > 0)
    Buffer.BlockCopy(toAdd, 0, bytesTemp, 0, toAdd.Length); 
  Buffer.BlockCopy(addBytes, 0, bytesTemp, toAdd.Length,
    addBytes.Length); 
  toAdd = bytesTemp;
}

ByteShift: mengambil byte array terdepan yang dibatasi oleh byte array separator/pembatas tertentu.

public static byte[] BytesShift(ref byte[] toShift, byte[] splitter)

  byte[] bytesShift, bytesTemp; 
  if (toShift.Length > 0) 
  { 
    BytesTrim(ref toShift, splitter); 
    int offset = BytesIndexOf(ref toShift, splitter, 0); 
    if (offset > -1) 
    { 
      bytesShift = new byte[offset]; 
      Buffer.BlockCopy(toShift, 0, bytesShift, 0, bytesShift.Length); 
      if ((toShift.Length - bytesShift.Length) > splitter.Length) 
      { 
        bytesTemp = new byte[toShift.Length - bytesShift.Length - 
          splitter.Length]; 
        Buffer.BlockCopy(toShift, bytesShift.Length + splitter.Length, 
          bytesTemp, 0, bytesTemp.Length); 
      } 
      else bytesTemp = new byte[0]; 
      toShift = bytesTemp; 
      return bytesShift; 
    } 
    else 
    { 
      bytesTemp = toShift; 
      toShift = new byte[0]; 
      return bytesTemp; 
    } 
  } 
  else return new byte[0];
}

public static int BytesIndexOf(ref byte[] toSearch, byte[] searchFor, int startOffset)

  byte[] temp = new byte[searchFor.Length]; 
  int rv = -1; 
  for (int i = startOffset; i < toSearch.Length - searchFor.Length; i++) 
  { 
    Buffer.BlockCopy(toSearch, i, temp, 0, searchFor.Length); 
    if (BytesEqual(temp, searchFor)) 
    { 
      rv = i; 
      break; 
    } 
  } 
  temp = null; 
  return rv;
}

public static int BytesLastIndexOf(ref byte[] toSearch, byte[] searchFor)
{
  byte[] temp = new byte[searchFor.Length];
  int rv = -1;
  for (int i = (toSearch.Length - searchFor.Length); i > rv; i—)
  {
    Buffer.BlockCopy(toSearch, i, temp, 0, searchFor.Length);
    if (BytesEqual(temp, searchFor))
    {
      rv = i;
      break;
    }
  }
  temp = null;
  return rv;
}

public static bool BytesEqual(byte[] bytesA, byte[] bytesB)
{
  if (bytesA.Length != bytesB.Length) return false; 
  int pos = 0; 
  foreach (byte a in bytesA) 
  { 
    if (!a.Equals(bytesB[pos])) return false; 
    pos++; 
  } 
  return true;
}

public static void BytesTrim(ref byte[] toTrim, byte[] trim)
{
  int offset = 0;
  byte[] bytesTemp;
  bool moreTrim = false;
  while (true)
  {
    offset = BytesIndexOf(ref toTrim, trim, 0);
    if (offset == 0)
    {
      bytesTemp = new byte[toTrim.Length - trim.Length];
      Buffer.BlockCopy(toTrim, trim.Length, bytesTemp, 0,
        bytesTemp.Length);
      toTrim = bytesTemp;
      moreTrim = true;
    }
    else moreTrim = false;
    offset = BytesLastIndexOf(ref toTrim, trim);
    if (offset == (toTrim.Length - trim.Length))
    {
      bytesTemp = new byte[toTrim.Length - trim.Length];
      Buffer.BlockCopy(toTrim, 0, bytesTemp, 0, bytesTemp.Length);
      toTrim = bytesTemp;
      moreTrim = moreTrim || true;
    }
    else moreTrim = moreTrim || false;
    if (!moreTrim) break;
  }
}

Tidak ada komentar:

Posting Komentar

Jika ada kritik dan saran, komentari Artikel ini.