2011-03-20 12 views
24

मैं दो byte[] सरणियों अज्ञात लंबाई के होते हैं जो है और मैं बस अन्य अर्थात के अंत तक एक संलग्न करना चाहते हैं:एक और बाइट के अंत में एक बाइट [] जोड़ना []

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

मैं arraycopy() का उपयोग करने का प्रयास किया है लेकिन इसे काम पर नहीं लग रहा है।

+1

के संभावित डुप्लिकेट [आसान तरीका दो बाइट सरणी जुटना के लिए] (http://stackoverflow.com/questions/5513152/easy-way-to-concatenate-two-byte-arrays) – Ubunfu

उत्तर

49

का उपयोग करना System.arraycopy(), निम्नलिखित की तरह कुछ काम करना चाहिए:

// 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

बढ़िया! त्वरित समाधान के लिए धन्यवाद। एक आकर्षण की तरह काम करता है और अब इतना आसान लगता है! – Mitch

+0

मुझे विश्वास है कि अगर हम पहले से अंतिम सरणी के आकार को नहीं जानते हैं तो यह कठिनाइयों का निर्माण करेगा। – Shashwat

12

आप लंबाई ciphertext और mac एक साथ जोड़ की लंबाई के बराबर के साथ एक बाइट सरणी के रूप में out घोषित करने के लिए की जरूरत है, और फिर कॉपी ciphertextout और mac की शुरुआत से अधिक अंत में, 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

मुझे यह और अधिक उपयोगी मिला। – Sorter

5

सबसे पहले आप संयुक्त लंबाई की एक सरणी आवंटित करने के लिए, तो दोनों स्रोतों से इसे भरने की arraycopy का उपयोग की जरूरत है।

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

अन्य प्रदान की समाधान महान जब आप केवल 2 बाइट सरणियों जोड़ना चाहते हैं, लेकिन एक भी गोल करने के लिए आप कई बाइट [] मात्रा जोड़कर रखना चाहते हैं:

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

मैंने लिखा

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]; 
} 

यह Arrays.copyOf

8

पी का उपयोग करता है: कई सरणी के संयोजन के लिए प्रक्रिया का पालन erhaps सबसे आसान तरीका:

ByteArrayOutputStream output = new ByteArrayOutputStream(); 

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

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

बाइटएरे बफर के रूप में सबसे आसान और शायद सबसे अच्छा समर्थित लगता है। – pete

संबंधित मुद्दे