Minggu, 01 Februari 2015

Fungsi Byte Array di c#

Ini adalah fungsi untuk manipulasi byte array, dibuat di c#. class bertipe static. Fungsi ini terdiri atas,
  1. PushByteA
  2. PushByteABulk
  3. ShiftByteA
  4. ShiftByteALen
  5. SerializeToPushByteA
  6. DeserializeFromShiftByteA
public static class ByteAProcess
{
    private static byte[] getByteA4BytesLen(ref byte[] byteA)
    {
        byte[] result = new byte[4];
        int byteALen = byteA.Length;
        result[0] = (byte)(byteALen >> 24);
        result[1] = (byte)(byteALen >> 16);
        result[2] = (byte)(byteALen >> 8);
        result[3] = (byte)byteALen;
        return result;
    }

    public static void PushByteA(byte[] byteA2Push, ref byte[] toPush)
    {
        if (toPush == null) toPush = new byte[0];
        byte[] temp = null;
        if (byteA2Push == null || byteA2Push.Length == 0)
            temp = new byte[4] { 0x00, 0x00, 0x00, 0x00 };
        else
        {
            temp = new byte[4 + byteA2Push.Length];
            Buffer.BlockCopy(ByteAProcess.getByteA4BytesLen(ref byteA2Push), 0, temp, 0, 4);
            Buffer.BlockCopy(byteA2Push, 0, temp, 4, byteA2Push.Length);
        }
        ByteAProcess.PushByteABulk(temp, ref toPush);
        temp = null;
    }

    public static void PushByteABulk(byte[] byteA2Push, ref byte[] toPush)
    {
        if (byteA2Push == null || byteA2Push.Length == 0) return;
        if (toPush == null) toPush = new byte[0];

        if (toPush.Length == 0)
        {
            Array.Resize(ref toPush, byteA2Push.Length);
            Buffer.BlockCopy(byteA2Push, 0, toPush, 0, toPush.Length);
        }
        else
        {
            int lastToPushSize = toPush.Length;
            Array.Resize(ref toPush, lastToPushSize + byteA2Push.Length);
            Buffer.BlockCopy(byteA2Push, 0, toPush, lastToPushSize, byteA2Push.Length);
        }
    }

    public static byte[] ShiftByteA(ref byte[] toShift)
    {
        if (toShift == null || toShift.Length == 0)
        {
            toShift = null;
            return null;
        }

        byte[] byteALen = new byte[4];
        Buffer.BlockCopy(toShift, 0, byteALen, 0, 4);
        int iByteALen = ((byteALen[0] << 24) | (byteALen[1] << 16) | (byteALen[2] << 8) | (byteALen[3]));
        if (iByteALen == 0) {
            ByteAProcess.ShiftByteALen(4, ref toShift);
            return null;
        }
        else
        {
            byteALen = new byte[iByteALen];
            Buffer.BlockCopy(toShift, 4, byteALen, 0, iByteALen);
            ByteAProcess.ShiftByteALen(iByteALen + 4, ref toShift);
            return byteALen;
        }
    }

    public static byte[] ShiftByteALen(int byteALen, ref byte[] toShift)
    {
        if (toShift == null || toShift.Length == 0) return null;
        if (byteALen == 0) return null;
    
        byte[] result = null;
        if (byteALen == toShift.Length)
        {
            result = new byte[byteALen];
            Buffer.BlockCopy(toShift, 0, result, 0, byteALen);

            toShift = null;
        }
        else if (byteALen < toShift.Length)
        {
            result = new byte[byteALen];
            byte[] temp = new byte[toShift.Length - byteALen];

            Buffer.BlockCopy(toShift, 0, result, 0, result.Length);
            Buffer.BlockCopy(toShift, byteALen, temp, 0, temp.Length);

            Array.Resize(ref toShift, temp.Length);
            Buffer.BlockCopy(temp, 0, toShift, 0, temp.Length);

            temp = null;
        }
        else
        {
            result = new byte[toShift.Length];
            Buffer.BlockCopy(toShift, 0, result, 0, result.Length);
            
            toShift = null;
        }
        return result;
    }

    public static void SerializeToPushByteA(object toSerialize, ref byte[] toPush)
    {
        byte[] bytesObject = null;
        if (toSerialize != null)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            (new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()).Serialize(

                ms, toSerialize);
            bytesObject = ms.ToArray();
            ms.Dispose();
            ByteAProcess.PushByteA(bytesObject, ref toPush);
        }
        else ByteAProcess.PushByteABulk(new byte[4] { 0x00, 0x00, 0x00, 0x00 }, ref toPush);
    }

    public static object DeserializeFromShiftByteA(ref byte[] toShift)
    {
        if (toShift == null || toShift.Length == 0) return null;
        // if (ByteAProcess.isShiftedBAIsNull(ref toShift)) return null;
        byte[] shiftedBytes = null;
        if ((shiftedBytes = ByteAProcess.ShiftByteA(ref toShift)) != null && shiftedBytes.Length > 0)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream(shiftedBytes);
            object result = (object)

                (new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()).Deserialize(ms);
            ms.Dispose();
            return result;
        }
        else return null;
    }

    private static bool isShiftedBAIsNull(ref byte[] toShift)
    {
        if (toShift.Length < 4) {
            toShift = null;
            return true;
        }
        byte[] temp = new byte[4];
        Buffer.BlockCopy(toShift, 0, temp, 0, 4);
        if (temp.SequenceEqual(new byte[4] { 0x00, 0x00, 0x00, 0x00 }))
        {
            ByteAProcess.ShiftByteALen(4, ref toShift);
            return true;
        }
        else return false;
    }
}

Tidak ada komentar:

Posting Komentar

Jika ada kritik dan saran, komentari Artikel ini.