|
| 1 | +package com.baeldung.string.conversion; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | + |
| 6 | +import java.io.UnsupportedEncodingException; |
| 7 | +import java.nio.CharBuffer; |
| 8 | +import java.nio.charset.CharacterCodingException; |
| 9 | +import java.nio.charset.Charset; |
| 10 | +import java.nio.charset.CharsetEncoder; |
| 11 | +import java.nio.charset.CodingErrorAction; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.Arrays; |
| 14 | + |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +public class StringToByteArrayUnitTest { |
| 18 | + |
| 19 | + @Test |
| 20 | + public void whenGetBytesWithDefaultCharset_thenOK() { |
| 21 | + final String inputString = "Hello World!"; |
| 22 | + final String defaultCharSet = Charset.defaultCharset().displayName(); |
| 23 | + |
| 24 | + byte[] byteArrray = inputString.getBytes(); |
| 25 | + |
| 26 | + System.out.printf( |
| 27 | + "Using default charset:%s, Input String:%s, Output byte array:%s\n", |
| 28 | + defaultCharSet, inputString, Arrays.toString(byteArrray)); |
| 29 | + |
| 30 | + assertNotNull(byteArrray); |
| 31 | + assert (byteArrray.length >= inputString.length()); |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + public void whenGetBytesWithNamedCharset_thenOK() |
| 36 | + throws UnsupportedEncodingException { |
| 37 | + final String inputString = "Hello World!"; |
| 38 | + final String charsetName = "IBM01140"; |
| 39 | + |
| 40 | + byte[] byteArrray = inputString.getBytes("IBM01140"); |
| 41 | + |
| 42 | + System.out.printf( |
| 43 | + "Using named charset:%s, Input String:%s, Output byte array:%s\n", |
| 44 | + charsetName, inputString, Arrays.toString(byteArrray)); |
| 45 | + |
| 46 | + assertArrayEquals(new byte[] { -56, -123, -109, -109, -106, 64, -26, |
| 47 | + -106, -103, -109, -124, 90 }, |
| 48 | + byteArrray); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void whenGetBytesWithCharset_thenOK() { |
| 53 | + final String inputString = "Hello World!"; |
| 54 | + final Charset charset = Charset.forName("UTF-8"); |
| 55 | + |
| 56 | + byte[] byteArrray = inputString.getBytes(charset); |
| 57 | + |
| 58 | + System.out.printf( |
| 59 | + "Using Charset:%s, Input String:%s, Output byte array:%s\n", |
| 60 | + charset, inputString, Arrays.toString(byteArrray)); |
| 61 | + |
| 62 | + assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, |
| 63 | + 108, 100, 33 }, |
| 64 | + byteArrray); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void whenGetBytesWithStandardCharset_thenOK() { |
| 69 | + final String inputString = "Hello World!"; |
| 70 | + final Charset charset = StandardCharsets.UTF_16; |
| 71 | + |
| 72 | + byte[] byteArrray = inputString.getBytes(charset); |
| 73 | + |
| 74 | + System.out.printf( |
| 75 | + "Using Standard Charset:%s, Input String:%s, Output byte array:%s\n", |
| 76 | + charset, inputString, Arrays.toString(byteArrray)); |
| 77 | + |
| 78 | + assertArrayEquals(new byte[] { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0, |
| 79 | + 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 }, |
| 80 | + byteArrray); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void whenEncodeWithCharset_thenOK() { |
| 85 | + final String inputString = "Hello World!"; |
| 86 | + final Charset charset = StandardCharsets.UTF_8; |
| 87 | + |
| 88 | + byte[] byteArrray = charset.encode(inputString).array(); |
| 89 | + |
| 90 | + System.out.printf( |
| 91 | + "Using encode with Charset:%s, Input String:%s, Output byte array:%s\n", |
| 92 | + charset, inputString, Arrays.toString(byteArrray)); |
| 93 | + |
| 94 | + assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, |
| 95 | + 108, 100, 33, 0 }, |
| 96 | + byteArrray); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void whenUsingCharsetEncoder_thenOK() |
| 101 | + throws CharacterCodingException { |
| 102 | + final String inputString = "Hello World!"; |
| 103 | + CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder(); |
| 104 | + encoder.onMalformedInput(CodingErrorAction.IGNORE) |
| 105 | + .onUnmappableCharacter(CodingErrorAction.REPLACE) |
| 106 | + .replaceWith(new byte[] { 0 }); |
| 107 | + |
| 108 | + byte[] byteArrray = encoder.encode(CharBuffer.wrap(inputString)).array(); |
| 109 | + |
| 110 | + System.out.printf( |
| 111 | + "Using encode with CharsetEncoder:%s, Input String:%s, Output byte array:%s\n", |
| 112 | + encoder, inputString, Arrays.toString(byteArrray)); |
| 113 | + |
| 114 | + assertArrayEquals(new byte[] { 72, 101, 108, 108, 111, 32, 87, 111, 114, |
| 115 | + 108, 100, 33, 0 }, |
| 116 | + byteArrray); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments