Inserção de bytes

Boa tarde,

Fiz uma função que insere bytes de “flag”, gostaria de saber se teria como melhorar esse código. Ele está funcionando perfeitamente, mas haveria uma forma melhor para se fazer?

O byte 0x01 deve virar 0x10,0x21.
O byte 0x04 deve virar 0x10,0x24.
e etc.

Segue código que criei:

[code]public static String byteStuffing(byte[] bytearray){
int count = 0;
//Contando quantos bytes a mais terei no novo array
for (int i = 0; i < bytearray.length; i++) {
if(bytearray[i] == 0x01){
count++;
}else if(bytearray[i] == 0x04){
count++;
}else if(bytearray[i] == 0x10){
count++;
}else if(bytearray[i] == 0x11){
count++;
}else if(bytearray[i] == 0x13){
count++;
}
}
//Criando array secundario que terá os bytes “Codificados” com o tamanho exato
byte[] stuffing = new byte[bytearray.length+count];
count = 0;
//inserindo os bytes “Codificados” no array
for (int i = 0; i < bytearray.length; i++) {

		   	if(bytearray[i] == 0x01){
		   		stuffing[count] = (byte)0x10;
    			stuffing[count+1]=(byte)0x21;
    			count++;
    		}else if(bytearray[i]  == 0x04){
    			stuffing[count] = (byte)0x10;
    			stuffing[count+1]=(byte)0x24;
    			count++;
    		}else if(bytearray[i]  == 0x10){
    			stuffing[count] = (byte)0x10;
    			stuffing[count+1]=(byte)0x30;
    			count++;
    		}else if(bytearray[i]  == 0x11){
    			stuffing[count] = (byte)0x10;
    			stuffing[count+1]=(byte)0x31;
    			count++;
    		}else if(bytearray[i]  == 0x13){
    			stuffing[count] = (byte)0x10;
    			stuffing[count+1]=(byte)0x33;
    			count++;
    		}else{
    			stuffing[count] = bytearray[i];
    	    }
		   	count++;
    	}
       	return toHexString(stuffing);
    }[/code]

Pode usar um switch/case para evitar referências repetidas a bytearray[i]

Exemplo:

            for (int i = 0; i < bytearray.length; i++) {  
                switch (byteArray[i]) {
                case 0x01: case 0x04: case 0x10: case 0x11: case 0x13:
                     count++;
                }
            }

O switch/case é implementado, nesse caso, com uma instrução especial e uma tabela de jumps, e costuma ser mais rápido que um monte de ifs encadeados.

Olhando com mais atenção, seu código está errado. Deveria ser algo como:

                    stuffing[count++] = (byte)0x10;  
                    stuffing[count++]=(byte)0x33;  

EDIT - desculpe, não vi o count++ que é executado para todos os casos. Minha correção está incorreta :frowning:

Utilizei a sua dica para o primeiro for e funcionou beleza!

Tive que usar o count++ dentro dos ifs pq eu to inserindo 2 bytes.

Oi.

Acho que isso resume bem.

        public static byte[] ToByteStuffing(byte[] dados)
        {
            int count = 0;
            int count2 = 0;
            byte[] stuffing = null;
            do
            {
                if ((dados[count] == 0x01) ^ 
                    (dados[count] == 0x04) ^ 
                    (dados[count] == 0x10) ^ 
                    (dados[count] == 0x11) ^ 
                    (dados[count] == 0x13))
                {
                    stuffing[count2] = 0x10;
                    count2++;
                    stuffing[count2] = dados[count];
                    stuffing[count2] += 0x20;
                }
                else stuffing[count2] = dados[count];
                count++;
                count2++;
            } while (count != dados[dados.Length]);
	        return stuffing;  
	    }