Saturday, May 30, 2009

Compress/decompress byte array

// Compress/decompress byte array

using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.IO;
using System.Collections;

namespace Utilities
{
class Compression
{
public static byte[] Compress(byte[] data)
{
MemoryStream ms = new MemoryStream();
DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
ds.Write(data, 0, data.Length);
ds.Flush();
ds.Close();
return ms.ToArray();
}
public static byte[] Decompress(byte[] data)
{
const int BUFFER_SIZE = 256;
byte[] tempArray = new byte[BUFFER_SIZE];
List tempList = new List();
int count = 0, length = 0;

MemoryStream ms = new MemoryStream(data);
DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);

while ((count = ds.Read(tempArray, 0, BUFFER_SIZE)) > 0)
{
if (count == BUFFER_SIZE)
{
tempList.Add(tempArray);
tempArray = new byte[BUFFER_SIZE];
}
else
{
byte[] temp = new byte[count];
Array.Copy(tempArray, 0, temp, 0, count);
tempList.Add(temp);
}
length += count;
}

byte[] retVal = new byte[length];

count = 0;
foreach (byte[] temp in tempList)
{
Array.Copy(temp, 0, retVal, count, temp.Length);
count += temp.Length;
}

return retVal;
}
}
}

Remove some items from ArrayList

// Remove some items from ArrayList

int count = myArrayList.Count;
for(int i=0; i someValue)
{
myArrayList.RemoveAt(i);
count--;
} else {
i++;
}

}

Find content of a byte array inside other byte array

// Find content of a byte array inside other byte array

// --------
// Method 1
// --------
public static int IndexOf(byte[] array, byte[] pattern)
{
bool found = false;

if (pattern.Length > array.Length)
return -1;

int i, j;

for (i = 0, j = 0; i < array.Length;)
{
if (array[i++] != pattern[j++])
{
j = 0;
continue;
}

if (j == pattern.Length)
{
found = true;
break;
}
}

if (!found)
return -1;
else
return i - pattern.Length;
}



// --------
// Method 2
// --------
private int IndexOf(byte[] array, byte[] pattern)
{
if ((array != null) && (array != null))
{
if (pattern.Length > array.Length) return 0;
for (int i = 0; i < array.Length; i++)
{
int startIndex = i;
bool match = true;
for (int j = 0; j < pattern.Length; j++)
{
if (array[startIndex] != pattern[j])
{
match = false;
break;
}
else if (startIndex < array.Length)
startIndex++;

}
if (match)
return startIndex - pattern.Length;
}
}
return -1;
}
}

Convert object to byte array and vice versa (serialization)

// Convert object to byte array and vice versa (serialization)
//
// Note: for custom classes add [Serializable] attribute to
// enable serialization

// Convert an object to a byte array
private byte[] ObjectToByteArray(Object obj)
{
if(obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}

// Convert a byte array to an Object
private Object ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
Object obj = (Object) binForm.Deserialize(memStream);
return obj;
}

Copy Arrays with Array.Copy

No Explation Only Code..
using System;

public class CopyArray {

public static void Main() {
int[] integers = new int[] {5, 10, 15};
double[] doubles = new double[3];
Array.Copy (integers, doubles, 2);
Console.Write ("integers array elements:" );
foreach (int integer in integers) {
Console.Write("{0,3}", integer);
}
Console.Write ("\ndoubles array elements:" );
foreach (double duble in doubles) {
Console.Write ("{0,3}", duble);
}
Console.WriteLine();
}
}