Thursday 13 February 2020

Java: Concatenate two byte arrays

Approach 1: Traversing to each element of array and copy.
public static byte[] concat_approach1(byte[] arr1, byte[] arr2) {
 if (arr1 == null || arr1.length == 0) {
  return arr2;
 }

 if (arr2 == null || arr2.length == 0) {
  return arr1;
 }

 int arr1Length = arr1.length;
 int arr2Length = arr2.length;
 int totalElements = arr1Length + arr2Length;

 byte[] arr3 = new byte[totalElements];

 int counter = 0;

 for (int i = 0; i < arr1Length; i++) {
  arr3[counter++] = arr1[i];
 }

 for (int i = 0; i < arr2Length; i++) {
  arr3[counter++] = arr2[i];
 }

 return arr3;
}

Approach 2: Using System.arraycopy
public static byte[] concat_approach2(byte[] arr1, byte[] arr2) {
 if (arr1 == null || arr1.length == 0) {
  return arr2;
 }

 if (arr2 == null || arr2.length == 0) {
  return arr1;
 }

 int arr1Length = arr1.length;
 int arr2Length = arr2.length;
 int totalElements = arr1Length + arr2Length;

 byte[] arr3 = new byte[totalElements];
 System.arraycopy(arr1, 0, arr3, 0, arr1Length);
 System.arraycopy(arr2, 0, arr3, arr1Length, arr2Length);

 return arr3;
}

Approach 3: Using ByteArrayOutputStream
public static byte[] concat_approach3(byte[] arr1, byte[] arr2) throws IOException {
 if (arr1 == null || arr1.length == 0) {
  return arr2;
 }

 if (arr2 == null || arr2.length == 0) {
  return arr1;
 }

 ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 outputStream.write(arr1);
 outputStream.write(arr2);

 return outputStream.toByteArray();
}

Approach 4: Using ByteBuffer.
public static byte[] concat_approach4(byte[] arr1, byte[] arr2) throws IOException {
 if (arr1 == null || arr1.length == 0) {
  return arr2;
 }

 if (arr2 == null || arr2.length == 0) {
  return arr1;
 }
 
 int arr1Length = arr1.length;
 int arr2Length = arr2.length;
 
 ByteBuffer byteBuffer = ByteBuffer.allocate(arr1Length + arr2Length);
 byteBuffer.put(arr1);
 byteBuffer.put(arr2);
 
 return byteBuffer.array();
}

Approach 5: Using String concatenation.
public static byte[] concat_approach5(byte[] arr1, byte[] arr2) {
 if (arr1 == null || arr1.length == 0) {
  return arr2;
 }

 if (arr2 == null || arr2.length == 0) {
  return arr1;
 }

 String s1 = new String(arr1);
 String s2 = new String(arr2);

 String result = s1 + s2;
 return result.getBytes();
}

Find the below working application.

App.java
package com.sample.app;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class App {

 public static void printArray(byte[] arr) {
  if (arr == null || arr.length == 0) {
   return;
  }

  for (byte ele : arr) {
   System.out.print(ele + " ");
  }
  System.out.println();
 }

 public static byte[] concat_approach1(byte[] arr1, byte[] arr2) {
  if (arr1 == null || arr1.length == 0) {
   return arr2;
  }

  if (arr2 == null || arr2.length == 0) {
   return arr1;
  }

  int arr1Length = arr1.length;
  int arr2Length = arr2.length;
  int totalElements = arr1Length + arr2Length;

  byte[] arr3 = new byte[totalElements];

  int counter = 0;

  for (int i = 0; i < arr1Length; i++) {
   arr3[counter++] = arr1[i];
  }

  for (int i = 0; i < arr2Length; i++) {
   arr3[counter++] = arr2[i];
  }

  return arr3;
 }

 public static byte[] concat_approach2(byte[] arr1, byte[] arr2) {
  if (arr1 == null || arr1.length == 0) {
   return arr2;
  }

  if (arr2 == null || arr2.length == 0) {
   return arr1;
  }

  int arr1Length = arr1.length;
  int arr2Length = arr2.length;
  int totalElements = arr1Length + arr2Length;

  byte[] arr3 = new byte[totalElements];
  System.arraycopy(arr1, 0, arr3, 0, arr1Length);
  System.arraycopy(arr2, 0, arr3, arr1Length, arr2Length);

  return arr3;
 }

 public static byte[] concat_approach3(byte[] arr1, byte[] arr2) throws IOException {
  if (arr1 == null || arr1.length == 0) {
   return arr2;
  }

  if (arr2 == null || arr2.length == 0) {
   return arr1;
  }

  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  outputStream.write(arr1);
  outputStream.write(arr2);

  return outputStream.toByteArray();
 }

 public static byte[] concat_approach4(byte[] arr1, byte[] arr2) throws IOException {
  if (arr1 == null || arr1.length == 0) {
   return arr2;
  }

  if (arr2 == null || arr2.length == 0) {
   return arr1;
  }

  int arr1Length = arr1.length;
  int arr2Length = arr2.length;

  ByteBuffer byteBuffer = ByteBuffer.allocate(arr1Length + arr2Length);
  byteBuffer.put(arr1);
  byteBuffer.put(arr2);

  return byteBuffer.array();
 }

 public static byte[] concat_approach5(byte[] arr1, byte[] arr2){
  if (arr1 == null || arr1.length == 0) {
   return arr2;
  }

  if (arr2 == null || arr2.length == 0) {
   return arr1;
  }

  String s1 = new String(arr1);
  String s2 = new String(arr2);

  String result = s1 + s2;
  return result.getBytes();
 }

 public static void main(String[] args) throws IOException {
  byte[] arr1 = { 2, 3, 5, 7 };
  byte[] arr2 = { 2, 4, 6, 8, 10 };

  byte[] arr3 = concat_approach1(arr1, arr2);
  byte[] arr4 = concat_approach2(arr1, arr2);
  byte[] arr5 = concat_approach3(arr1, arr2);
  byte[] arr6 = concat_approach4(arr1, arr2);
  byte[] arr7 = concat_approach5(arr1, arr2);
  

  printArray(arr3);
  printArray(arr4);
  printArray(arr5);
  printArray(arr6);
  printArray(arr7);
 }

}

Output

2 3 5 7 2 4 6 8 10 
2 3 5 7 2 4 6 8 10 
2 3 5 7 2 4 6 8 10 
2 3 5 7 2 4 6 8 10 
2 3 5 7 2 4 6 8 10


You may like

No comments:

Post a Comment