The simplest method (built-in, assuming that a and b are two given arrays):
byte[] c = (new String(a, cch) + new String(b, cch)).getBytes(cch);
This, of course, works with more than two terms and uses the character set defined somewhere in your code:
static final java.nio.charset.Charset cch = java.nio.charset.StandardCharsets.ISO_8859_1;
Or, in a simpler form, without this encoding:
byte[] c = (new String(a, "l1") + new String(b, "l1")).getBytes("l1");
But you need to suppress the UnsupportedEncodingException which is unlikely to be thrown.
The fastest method:
public static byte[] concat(byte[] a, byte[] b) { int lenA = a.length; int lenB = b.length; byte[] c = Arrays.copyOf(a, lenA + lenB); System.arraycopy(b, 0, c, lenA, lenB); return c; }
John McClane Nov 28 '18 at 20:14 2018-11-28 20:14
source share