Merge pull request #5652 from chandra1123/BAEL-2313-String-to-byte-array

Bael 2313 string to byte array
This commit is contained in:
Loredana Crusoveanu 2018-12-16 16:31:10 +02:00 committed by GitHub
commit ea13a1e794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 214 additions and 1 deletions

View File

@ -35,6 +35,7 @@
- [String Not Empty Test Assertions in Java](https://www.baeldung.com/java-assert-string-not-empty)
- [String Performance Hints](https://www.baeldung.com/java-string-performance)
- [Using indexOf to Find All Occurrences of a Word in a String](https://www.baeldung.com/java-indexOf-find-string-occurrences)
- [Convert String to Byte Array and Reverse in Java](https://www.baeldung.com/java-string-to-byte-array)
- [Java Base64 Encoding and Decoding](https://www.baeldung.com/java-base64-encode-and-decode)
- [Generate a Secure Random Password in Java](https://www.baeldung.com/java-generate-secure-password)
- [Removing Repeated Characters from a String](https://www.baeldung.com/java-remove-repeated-char)
@ -42,4 +43,4 @@
- [Convert String to Byte Array and Reverse in Java](https://www.baeldung.com/java-string-to-byte-array)
- [Pad a String with Zeros or Spaces in Java](https://www.baeldung.com/java-pad-string)
- [Adding a Newline Character to a String in Java](https://www.baeldung.com/java-string-newline)
- [Remove or Replace part of a String in Java](https://www.baeldung.com/java-remove-replace-string-part)
- [Remove or Replace part of a String in Java](https://www.baeldung.com/java-remove-replace-string-part)

View File

@ -0,0 +1,90 @@
package com.baeldung.string.conversion;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import org.junit.Test;
public class ByteArrayToStringUnitTest {
@Test
public void whenStringConstructorWithDefaultCharset_thenOK() {
final byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114,
108, 100, 33 };
String string = new String(byteArrray);
System.out.println(string);
assertNotNull(string);
}
@Test
public void whenStringConstructorWithNamedCharset_thenOK()
throws UnsupportedEncodingException {
final String charsetName = "IBM01140";
final byte[] byteArrray = { -56, -123, -109, -109, -106, 64, -26, -106,
-103, -109, -124, 90 };
String string = new String(byteArrray, charsetName);
assertEquals("Hello World!", string);
}
@Test
public void whenStringConstructorWithCharSet_thenOK() {
final Charset charset = Charset.forName("UTF-8");
final byte[] byteArrray = { 72, 101, 108, 108, 111, 32, 87, 111, 114,
108, 100, 33 };
String string = new String(byteArrray, charset);
assertEquals("Hello World!", string);
}
@Test
public void whenStringConstructorWithStandardCharSet_thenOK() {
final Charset charset = StandardCharsets.UTF_16;
final byte[] byteArrray = { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0,
111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 };
String string = new String(byteArrray, charset);
assertEquals("Hello World!", string);
}
@Test
public void whenDecodeWithCharset_thenOK() {
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, -10, 111, 114, 108, -63, 33 };
final Charset charset = StandardCharsets.US_ASCII;
String string = charset.decode(ByteBuffer.wrap(byteArrray)).toString();
System.out.println(string);
assertEquals("Hello <20>orl<72>!", string);
}
@Test
public void whenUsingCharsetDecoder_thenOK()
throws CharacterCodingException {
byte[] byteArrray = { 72, 101, 108, 108, 111, 32, -10, 111, 114, 108, -63, 33};
CharsetDecoder decoder = StandardCharsets.US_ASCII.newDecoder();
decoder.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith("?");
String string = decoder.decode(ByteBuffer.wrap(byteArrray)).toString();
assertEquals("Hello ?orl?!", string);
}
}

View File

@ -0,0 +1,122 @@
package com.baeldung.string.conversion;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.UnsupportedEncodingException;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import org.junit.Test;
public class StringToByteArrayUnitTest {
@Test
public void whenGetBytesWithDefaultCharset_thenOK() {
final String inputString = "Hello World!";
final String defaultCharSet = Charset.defaultCharset()
.displayName();
byte[] byteArrray = inputString.getBytes();
System.out.printf(
"Using default charset:%s, Input String:%s, Output byte array:%s\n",
defaultCharSet, inputString, Arrays.toString(byteArrray));
assertNotNull(byteArrray);
assert (byteArrray.length >= inputString.length());
}
@Test
public void whenGetBytesWithNamedCharset_thenOK()
throws UnsupportedEncodingException {
final String inputString = "Hello World!";
final String charsetName = "IBM01140";
byte[] byteArrray = inputString.getBytes("IBM01140");
System.out.printf(
"Using named charset:%s, Input String:%s, Output byte array:%s\n",
charsetName, inputString, Arrays.toString(byteArrray));
assertArrayEquals(new byte[] { -56, -123, -109, -109, -106, 64, -26,
-106, -103, -109, -124, 90 },
byteArrray);
}
@Test
public void whenGetBytesWithCharset_thenOK() {
final String inputString = "Hello ਸੰਸਾਰ!";
final Charset charset = Charset.forName("ASCII");
byte[] byteArrray = inputString.getBytes(charset);
System.out.printf(
"Using Charset:%s, Input String:%s, Output byte array:%s\n",
charset, inputString, Arrays.toString(byteArrray));
assertArrayEquals(
new byte[] { 72, 101, 108, 108, 111, 32, 63, 63, 63, 63, 63, 33 },
byteArrray);
}
@Test
public void whenGetBytesWithStandardCharset_thenOK() {
final String inputString = "Hello World!";
final Charset charset = StandardCharsets.UTF_16;
byte[] byteArrray = inputString.getBytes(charset);
System.out.printf(
"Using Standard Charset:%s, Input String:%s, Output byte array:%s\n",
charset, inputString, Arrays.toString(byteArrray));
assertArrayEquals(new byte[] { -2, -1, 0, 72, 0, 101, 0, 108, 0, 108, 0,
111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0, 33 },
byteArrray);
}
@Test
public void whenEncodeWithCharset_thenOK() {
final String inputString = "Hello ਸੰਸਾਰ!";
final Charset charset = StandardCharsets.US_ASCII;
byte[] byteArrray = charset.encode(inputString)
.array();
System.out.printf(
"Using encode with Charset:%s, Input String:%s, Output byte array:%s\n",
charset, inputString, Arrays.toString(byteArrray));
assertArrayEquals(
new byte[] { 72, 101, 108, 108, 111, 32, 63, 63, 63, 63, 63, 33 },
byteArrray);
}
@Test
public void whenUsingCharsetEncoder_thenOK()
throws CharacterCodingException {
final String inputString = "Hello ਸੰਸਾਰ!";
CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
encoder.onMalformedInput(CodingErrorAction.IGNORE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith(new byte[] { 0 });
byte[] byteArrray = encoder.encode(CharBuffer.wrap(inputString))
.array();
System.out.printf(
"Using encode with CharsetEncoder:%s, Input String:%s, Output byte array:%s\n",
encoder, inputString, Arrays.toString(byteArrray));
assertArrayEquals(
new byte[] { 72, 101, 108, 108, 111, 32, 0, 0, 0, 0, 0, 33 },
byteArrray);
}
}