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); + } +}