2011-03-20 5 views
24

J'ai deux byte[] tableaux qui sont d'une longueur inconnue et je veux simplement ajouter un à la fin de l'autre, à savoir:Ajout d'un octet [] à la fin d'un autre octet []

byte[] ciphertext = blah; 
byte[] mac = blah; 
byte[] out = ciphertext + mac; 

I J'ai essayé d'utiliser arraycopy() mais je n'arrive pas à le faire fonctionner.

+1

double possible de [Easy Way pour concaténer deux tableaux d'octets] (http://stackoverflow.com/questions/5513152/easy-way-to-concatenate-two-byte-arrays) – Ubunfu

Répondre

49

En utilisant System.arraycopy(), quelque chose comme ce qui suit devrait fonctionner:

// create a destination array that is the size of the two arrays 
byte[] destination = new byte[ciphertext.length + mac.length]; 

// copy ciphertext into start of destination (from pos 0, copy ciphertext.length bytes) 
System.arraycopy(ciphertext, 0, destination, 0, ciphertext.length); 

// copy mac into end of destination (from pos ciphertext.length, copy mac.length bytes) 
System.arraycopy(mac, 0, destination, ciphertext.length, mac.length); 
+0

Excellent! Merci pour la solution rapide. Fonctionne comme un charme et semble si simple maintenant! – Mitch

+0

Je crois que cela va créer des difficultés si nous ne connaissons pas la taille du tableau final à l'avance. – Shashwat

12

Vous devez déclarer out comme un tableau d'octets avec une longueur égale à la longueur de ciphertext et mac additionnés, puis copier ciphertext sur le début de out et mac sur la fin, en utilisant arraycopy.

byte[] concatenateByteArrays(byte[] a, byte[] b) { 
    byte[] result = new byte[a.length + b.length]; 
    System.arraycopy(a, 0, result, 0, a.length); 
    System.arraycopy(b, 0, result, a.length, b.length); 
    return result; 
} 
+0

J'ai trouvé cela plus utile. – Sorter

5

Vous devez d'abord allouer un tableau de la longueur combinée, puis utilisez arraycopy pour le remplir à partir des deux sources.

byte[] ciphertext = blah; 
byte[] mac = blah; 
byte[] out = new byte[ciphertext.length + mac.length]; 


System.arraycopy(ciphertext, 0, out, 0, ciphertext.length); 
System.arraycopy(mac, 0, out, ciphertext.length, mac.length); 
7

Les autres solutions proposées sont super quand vous voulez ajouter seulement deux tableaux d'octets, mais si vous voulez garder annexant plusieurs octets [] morceaux pour faire un seul:

byte[] readBytes ; // Your byte array .... //for eg. readBytes = "TestBytes".getBytes(); 

ByteArrayBuffer mReadBuffer = new ByteArrayBuffer(0) ; // Instead of 0, if you know the count of expected number of bytes, nice to input here 

mReadBuffer.append(readBytes, 0, readBytes.length); // this copies all bytes from readBytes byte array into mReadBuffer 
// Any new entry of readBytes, you can just append here by repeating the same call. 

// Finally, if you want the result into byte[] form: 
byte[] result = mReadBuffer.buffer(); 
3

j'ai écrit la suivant la procédure pour la concaténation de plusieurs tableau:

static public byte[] concat(byte[]... bufs) { 
    if (bufs.length == 0) 
     return null; 
    if (bufs.length == 1) 
     return bufs[0]; 
    for (int i = 0; i < bufs.length - 1; i++) { 
     byte[] res = Arrays.copyOf(bufs[i], bufs[i].length+bufs[i + 1].length); 
     System.arraycopy(bufs[i + 1], 0, res, bufs[i].length, bufs[i + 1].length); 
     bufs[i + 1] = res; 
    } 
    return bufs[bufs.length - 1]; 
} 

Il utilise Arrays.copyOf

8

P eut-être le plus simple:

ByteArrayOutputStream output = new ByteArrayOutputStream(); 

output.write(ciphertext); 
output.write(mac); 

byte[] out = output.toByteArray(); 
+0

Le plus facile et peut-être mieux supporté comme ByteArrayBuffer semble obsolète. – pete