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
This commit is contained in:
parent
36f28871ce
commit
9918d614e6
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue