Merge pull request #15666 from sIvanovKonstantyn/master

BAEL-7453 - Convert byte[] to Byte[] and Vice Versa in Java
This commit is contained in:
Vini 2024-01-22 13:53:23 +01:00 committed by GitHub
commit c8f486aff6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package com.baeldung.array.conversions;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;
public class ByteArrayToPrimitiveByteArrayUnitTest {
private static final byte[] EXPECTED_ARRAY_VALUES = {65, 66, 67, 68};
private static final Byte[] BYTE_ARRAY = {65, 66, 67, 68};
@Test
public void givenByteArray_whenConvertingUsingByteValue_thenGiveExpectedResult() {
byte[] newByteArray = new byte[BYTE_ARRAY.length];
for (int i = 0; i < BYTE_ARRAY.length; i++) {
newByteArray[i] = BYTE_ARRAY[i].byteValue();
}
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
}
@Test
public void givenByteArray_whenConvertingUsingUnboxing_thenGiveExpectedResult() {
byte[] newByteArray = new byte[BYTE_ARRAY.length];
for (int i = 0; i < BYTE_ARRAY.length; i++) {
newByteArray[i] = BYTE_ARRAY[i];
}
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
}
@Test
public void givenByteArray_whenConvertingArrayUtils_thenGiveExpectedResult() {
byte[] newByteArray = ArrayUtils.toPrimitive(BYTE_ARRAY);
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.array.conversions;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;
public class PrimitiveByteArrayToByteArrayUnitTest {
private static final byte[] PRIMITIVE_BYTE_ARRAY = {65, 66, 67, 68};
private static final Byte[] EXPECTED_ARRAY_VALUES = {65, 66, 67, 68};
@Test
public void givenPrimitiveByteArray_whenConvertingUsingByteValueOf_thenGiveExpectedResult() {
Byte[] newByteArray = new Byte[PRIMITIVE_BYTE_ARRAY.length];
for (int i = 0; i < PRIMITIVE_BYTE_ARRAY.length; i++) {
newByteArray[i] = Byte.valueOf(PRIMITIVE_BYTE_ARRAY[i]);
}
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
}
@Test
public void givenPrimitiveByteArray_whenConvertingUsingAutoboxing_thenGiveExpectedResult() {
Byte[] newByteArray = new Byte[PRIMITIVE_BYTE_ARRAY.length];
for (int i = 0; i < PRIMITIVE_BYTE_ARRAY.length; i++) {
newByteArray[i] = PRIMITIVE_BYTE_ARRAY[i];
}
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
}
@Test
public void givenPrimitiveByteArray_whenConvertingUsingAutoboxingAndArraysSetAll_thenGiveExpectedResult() {
Byte[] newByteArray = new Byte[PRIMITIVE_BYTE_ARRAY.length];
Arrays.setAll(newByteArray, n -> PRIMITIVE_BYTE_ARRAY[n]);
assertThat(newByteArray)
.containsExactly(EXPECTED_ARRAY_VALUES);
}
@Test
public void givenPrimitiveByteArray_whenConvertingUsingArrayUtils_thenGiveExpectedResult() {
Byte[] newByteArray = ArrayUtils.toObject(PRIMITIVE_BYTE_ARRAY);
assertThat(newByteArray).containsExactly(EXPECTED_ARRAY_VALUES);
}
}