* BAEL-7060: Converting String to ByteBuffer

* BAEL-7060: Converting String to ByteBuffer (format code)
This commit is contained in:
ACHRAF TAITAI 2023-11-20 22:28:10 +01:00 committed by GitHub
parent d0d42269f0
commit 111dcccb92
1 changed files with 12 additions and 3 deletions

View File

@ -1,12 +1,12 @@
package com.baeldung.bytebuffertostring; package com.baeldung.bytebuffertostring;
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.Test;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
public class ByteArrayToStringUnitTest { public class ByteArrayToStringUnitTest {
private static Charset charset = StandardCharsets.UTF_8; private static Charset charset = StandardCharsets.UTF_8;
@ -41,4 +41,13 @@ public class ByteArrayToStringUnitTest {
assertEquals(content, newContent); assertEquals(content, newContent);
} }
@Test
public void convertStringToByteBuffer_thenOk() {
byte[] expectedBytes = content.getBytes(Charset.forName(charset.toString()));
ByteBuffer byteBuffer = ByteBuffer.wrap(expectedBytes);
// Test the conversion from string to ByteBuffer
assertEquals(expectedBytes, byteBuffer.array());
}
} }