From 9918d614e643c403391a29f8f8e3d79491979c01 Mon Sep 17 00:00:00 2001 From: sdhiray7 Date: Thu, 8 Jun 2023 22:55:28 +0530 Subject: [PATCH] BAEL-5621 Combining two byte arrays (#14129) * Initial commit for Object copy in Java * review comments commit for Object copy in Java * Initial commit for parseInt vs valueOf java * Review comments commit for parseInt vs valueOf java * Modify readme * review comments * build failure * build failure retry * build failure retry remove parseInt(java.lang.String,int,int,int) * build failure add comment * change examples * review comments * review comments 2 * review comments 3 * Initial commit for get current stacktrace * Remove old files * Name updates * Jenkins error * changes to file name * Review comments * Create unit test file * Remove unnecessary files * Update package name * BAEL-5321 Initial commit * BAEL-5321 Initial commit 2 * [BAEL-5321] review comments * [BAEL-5321] constructor * [BAEL-5321] inline * BAEL-6396 Initial commit * BAEL-6396 Files reorg --- .../arrayconcat/CombiningByteArrays.java | 49 +++++++++++++ .../CombiningByteArraysUnitTest.java | 73 +++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arrayconcat/CombiningByteArrays.java create mode 100644 core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arrayconcat/CombiningByteArraysUnitTest.java diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arrayconcat/CombiningByteArrays.java b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arrayconcat/CombiningByteArrays.java new file mode 100644 index 0000000000..d05cafc677 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/main/java/com/baeldung/arrayconcat/CombiningByteArrays.java @@ -0,0 +1,49 @@ +package com.baeldung.arrayconcat; + +import com.google.common.primitives.Bytes; +import org.apache.commons.lang3.ArrayUtils; + +import java.nio.ByteBuffer; + +public class CombiningByteArrays { + + public byte[] combineArraysUsingArrayCopy(byte[] first, byte[] second) { + byte[] combined = new byte[first.length + second.length]; + + System.arraycopy(first, 0, combined, 0, first.length); + System.arraycopy(second, 0, combined, first.length, second.length); + return combined; + } + + public byte[] combineArraysUsingByteBuffer(byte[] first, byte[] second, byte[] third) { + byte[] combined = new byte[first.length + second.length + third.length]; + + ByteBuffer buffer = ByteBuffer.wrap(combined); + buffer.put(first); + buffer.put(second); + buffer.put(third); + return buffer.array(); + } + + public byte[] combineArraysUsingCustomMethod(byte[] first, byte[] second) { + byte[] combined = new byte[first.length + second.length]; + + for (int i = 0; i < combined.length; ++i) { + combined[i] = i < first.length ? first[i] : second[i - first.length]; + } + return combined; + } + + public byte[] combineArraysUsingGuava(byte[] first, byte[] second, byte[] third) { + byte[] combined = Bytes.concat(first, second, third); + + return combined; + } + + public byte[] combineArraysUsingApacheCommons(byte[] first, byte[] second) { + byte[] combined = ArrayUtils.addAll(first, second); + + return combined; + } + +} diff --git a/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arrayconcat/CombiningByteArraysUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arrayconcat/CombiningByteArraysUnitTest.java new file mode 100644 index 0000000000..2aa06b7570 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced/src/test/java/com/baeldung/arrayconcat/CombiningByteArraysUnitTest.java @@ -0,0 +1,73 @@ +package com.baeldung.arrayconcat; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; + +public class CombiningByteArraysUnitTest { + + CombiningByteArrays combiningByteArrays; + + @Before + public void init() { + combiningByteArrays = new CombiningByteArrays(); + } + + @Test + public void givenTwoArrays_whenCombinedUsingArraysCopy_thenArrayContainingAllElementsMustBeReturned() { + byte[] first = {69, 121, 101, 45, 62, 118, 114}; + byte[] second = {58, 120, 100, 46, 64, 114, 103, 117}; + + byte[] combined = combiningByteArrays.combineArraysUsingArrayCopy(first, second); + + byte[] expectedArray = {69, 121, 101, 45, 62, 118, 114, 58, 120, 100, 46, 64, 114, 103, 117}; + assertArrayEquals(expectedArray, combined); + } + + @Test + public void givenTwoArrays_whenCombinedUsingByteBuffer_thenArrayContainingAllElementsMustBeReturned() { + byte[] first = {69, 121, 101, 45}; + byte[] second = {64, 114, 103, 117}; + byte[] third = {11, 22, 33}; + + byte[] combined = combiningByteArrays.combineArraysUsingByteBuffer(first, second, third); + + byte[] expectedArray = {69, 121, 101, 45, 64, 114, 103, 117, 11, 22, 33}; + assertArrayEquals(expectedArray, combined); + } + + @Test + public void givenTwoArrays_whenCombinedUsingCustomMethod_thenArrayContainingAllElementsMustBeReturned() { + byte[] first = {101, 45, 62 }; + byte[] second = {58, 120, 100, 46, 64, 114}; + + byte[] combined = combiningByteArrays.combineArraysUsingCustomMethod(first, second); + + byte[] expectedArray = {101, 45, 62, 58, 120, 100, 46, 64, 114}; + assertArrayEquals(expectedArray, combined); + } + + @Test + public void givenTwoArrays_whenCombinedUsingGuava_thenArrayContainingAllElementsMustBeReturned() { + byte[] first = {69, 121, 101, 118, 114}; + byte[] second = {58, 120, 114, 103, 117}; + byte[] third = {11, 22, 33}; + + byte[] combined = combiningByteArrays.combineArraysUsingGuava(first, second, third); + + byte[] expectedArray = {69, 121, 101, 118, 114, 58, 120, 114, 103, 117, 11, 22, 33}; + assertArrayEquals(expectedArray, combined); + } + + @Test + public void givenTwoArrays_whenCombinedUsingApacheCommons_thenArrayContainingAllElementsMustBeReturned() { + byte[] first = {45, 62, 114}; + byte[] second = {58, 120, 100}; + + byte[] combined = combiningByteArrays.combineArraysUsingApacheCommons(first, second); + + byte[] expectedArray = {45, 62, 114, 58, 120, 100}; + assertArrayEquals(expectedArray, combined); + } +}