Easy way to concatenate two byte arrays

前端 未结 12 1526
一个人的身影
一个人的身影 2020-12-02 04:25

What is the easy way to concatenate two byte arrays?

Say,

byte a[];
byte b[];

How do I concatenate two byte

相关标签:
12条回答
  • 2020-12-02 04:59

    This is my way to do it!

    public static byte[] concatByteArrays(byte[]... inputs) {
        int i = inputs.length - 1, len = 0;
        for (; i >= 0; i--) {
            len += inputs[i].length;
        }
        byte[] r = new byte[len];
        for (i = inputs.length - 1; i >= 0; i--) {
            System.arraycopy(inputs[i], 0, r, len -= inputs[i].length, inputs[i].length);
        }
        return r;
    }
    

    Features:

    • Use varargs (...) to be called with any number of byte[].
    • Use System.arraycopy() that is implemented with machine specific native code, to ensure high speed operation.
    • Create a new byte[] with the exact size that is need it.
    • Allocate little less int variables by reusing the i and len variables.
    • Faster comparison with constants.

    Keep in mind:

    The better way to do this, is by copy the @Jonathan code. The issue comes from the native variable arrays, because Java create new variables when this data type is passed to another function.

    0 讨论(0)
  • 2020-12-02 05:01

    Another possibility is using java.nio.ByteBuffer.

    Something like

    ByteBuffer bb = ByteBuffer.allocate(a.length + b.length + c.length);
    bb.put(a);
    bb.put(b);
    bb.put(c);
    byte[] result = bb.array();
    
    // or using method chaining:
    
    byte[] result = ByteBuffer
            .allocate(a.length + b.length + c.length)
            .put(a).put(b).put(c)
            .array();
    

    Note that the array must be appropriately sized to start with, so the allocation line is required (as array() simply returns the backing array, without taking the offset, position or limit into account).

    0 讨论(0)
  • 2020-12-02 05:03

    For two or multiple arrays, this simple and clean utility method can be used:

    /**
     * Append the given byte arrays to one big array
     *
     * @param arrays The arrays to append
     * @return The complete array containing the appended data
     */
    public static final byte[] append(final byte[]... arrays) {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        if (arrays != null) {
            for (final byte[] array : arrays) {
                if (array != null) {
                    out.write(array, 0, array.length);
                }
            }
        }
        return out.toByteArray();
    }
    
    0 讨论(0)
  • 2020-12-02 05:04

    Merge two PDF byte arrays

    If you are merging two byte arrays which contain PDF, this logic will not work. We need to use a third-party tool like PDFbox from Apache:

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    mergePdf.addSource(new ByteArrayInputStream(a));
    mergePdf.addSource(new ByteArrayInputStream(b));
    mergePdf.setDestinationStream(byteArrayOutputStream);
    mergePdf.mergeDocuments();
    c = byteArrayOutputStream.toByteArray();
    
    0 讨论(0)
  • 2020-12-02 05:14

    If you don't want to mess with arrays' sizes, just use the magic of string concatenation:

    byte[] c = (new String(a, "l1") + new String(b, "l1")).getBytes("l1");
    

    Or define somewhere in your code

    // concatenation charset
    static final java.nio.charset.Charset cch = java.nio.charset.StandardCharsets.ISO_8859_1;
    

    and use

    byte[] c = (new String(a, cch) + new String(b, cch)).getBytes(cch);
    

    This, of course, also works with more than two string concatenations using the + addition operator.


    Both "l1" and ISO_8859_1 indicate the Western Latin 1 character set that encodes each character as a single byte. As no multi-byte translations are performed the characters in the string will have the same values as the bytes (except that they will always be interpreted as positive values, as char is unsigned). At least for the Oracle provided runtime, any byte will therefore be correctly "decoded" and then "encoded" again.

    Beware that strings do expand the byte array considerately, requiring additional memory. Strings may also be interned and will therefore not easy be removed. Strings are also immutable, so the values inside them cannot be destroyed. You should therefore not concatenate sensitive arrays this way nor should you use this method for larger byte arrays. Giving a clear indication of what you are doing would also be required, as this method of array concatenation is not a common solution.

    0 讨论(0)
  • 2020-12-02 05:15

    Another way is to use a utility function (you could make this a static method of a generic utility class if you like):

    byte[] concat(byte[]...arrays)
    {
        // Determine the length of the result array
        int totalLength = 0;
        for (int i = 0; i < arrays.length; i++)
        {
            totalLength += arrays[i].length;
        }
    
        // create the result array
        byte[] result = new byte[totalLength];
    
        // copy the source arrays into the result array
        int currentIndex = 0;
        for (int i = 0; i < arrays.length; i++)
        {
            System.arraycopy(arrays[i], 0, result, currentIndex, arrays[i].length);
            currentIndex += arrays[i].length;
        }
    
        return result;
    }
    

    Invoke like so:

    byte[] a;
    byte[] b;
    byte[] result = concat(a, b);
    

    It will also work for concatenating 3, 4, 5 arrays, etc.

    Doing it this way gives you the advantage of fast arraycopy code which is also very easy to read and maintain.

    0 讨论(0)
提交回复
热议问题