From 6f9e1fd10374914bd45656a483113f8764dee6e0 Mon Sep 17 00:00:00 2001 From: Kumar Chandrakant Date: Thu, 8 Nov 2018 15:13:12 +0530 Subject: [PATCH] BAEL-2300: Adding files for the tutorial on character encoding. --- .../encoding/CharacterEncodingExamples.java | 32 ++++++++++ .../CharacterEncodingExamplesUnitTest.java | 61 +++++++++++++++++++ core-java/src/test/resources/encoding.txt | 1 + 3 files changed, 94 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/encoding/CharacterEncodingExamples.java create mode 100644 core-java/src/test/java/com/baeldung/encoding/CharacterEncodingExamplesUnitTest.java create mode 100644 core-java/src/test/resources/encoding.txt diff --git a/core-java/src/main/java/com/baeldung/encoding/CharacterEncodingExamples.java b/core-java/src/main/java/com/baeldung/encoding/CharacterEncodingExamples.java new file mode 100644 index 0000000000..bdd92e37f6 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/encoding/CharacterEncodingExamples.java @@ -0,0 +1,32 @@ +package com.baeldung.encoding; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; + +public class CharacterEncodingExamples { + + static String readFile(String filePath, String encoding) throws IOException { + File file = new File(filePath); + StringBuffer buffer = new StringBuffer(); + try (InputStreamReader isr = new InputStreamReader(new FileInputStream(file), encoding)) { + int data; + while ((data = isr.read()) != -1) { + buffer.append((char) data); + } + } + return buffer.toString(); + } + + static String convertToBinary(String input, String encoding) throws UnsupportedEncodingException { + byte[] bytes = input.getBytes(encoding); + StringBuffer buffer = new StringBuffer(); + for (int b : bytes) { + buffer.append(Integer.toBinaryString((b + 256) % 256)); + buffer.append(" "); + } + return buffer.toString(); + } +} diff --git a/core-java/src/test/java/com/baeldung/encoding/CharacterEncodingExamplesUnitTest.java b/core-java/src/test/java/com/baeldung/encoding/CharacterEncodingExamplesUnitTest.java new file mode 100644 index 0000000000..95b3605d95 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/encoding/CharacterEncodingExamplesUnitTest.java @@ -0,0 +1,61 @@ +package com.baeldung.encoding; + +import java.io.IOException; + +import org.junit.Assert; +import org.junit.Test; + +public class CharacterEncodingExamplesUnitTest { + + @Test + public void givenTextFile_whenCalledWithEncodingASCII_thenProduceIncorrectResult() throws IOException { + Assert.assertEquals( + CharacterEncodingExamples.readFile( + "src/test/resources/encoding.txt", "US-ASCII"), + "The fa��ade pattern is a software-design pattern commonly used with object-oriented programming."); + } + + @Test + public void givenTextFile_whenCalledWithEncodingUTF8_thenProduceCorrectResult() throws IOException { + Assert.assertEquals( + CharacterEncodingExamples.readFile( + "src/test/resources/encoding.txt", "UTF-8"), + "The façade pattern is a software-design pattern commonly used with object-oriented programming."); + } + + @Test + public void givenCharacterA_whenConvertedtoBinaryWithEncodingASCII_thenProduceResult() throws IOException { + Assert.assertEquals( + CharacterEncodingExamples.convertToBinary("A", "US-ASCII"), + "1000001 "); + } + + @Test + public void givenCharacterA_whenConvertedtoBinaryWithEncodingUTF8_thenProduceResult() throws IOException { + Assert.assertEquals( + CharacterEncodingExamples.convertToBinary("A", "UTF-8"), + "1000001 "); + } + + @Test + public void givenCharacterCh_whenConvertedtoBinaryWithEncodingBig5_thenProduceResult() throws IOException { + Assert.assertEquals( + CharacterEncodingExamples.convertToBinary("語", "Big5"), + "10111011 1111001 "); + } + + @Test + public void givenCharacterCh_whenConvertedtoBinaryWithEncodingUTF8_thenProduceResult() throws IOException { + Assert.assertEquals( + CharacterEncodingExamples.convertToBinary("語", "UTF-8"), + "11101000 10101010 10011110 "); + } + + @Test + public void givenCharacterCh_whenConvertedtoBinaryWithEncodingUTF32_thenProduceResult() throws IOException { + Assert.assertEquals( + CharacterEncodingExamples.convertToBinary("語", "UTF-32"), + "0 0 10001010 10011110 "); + } + +} diff --git a/core-java/src/test/resources/encoding.txt b/core-java/src/test/resources/encoding.txt new file mode 100644 index 0000000000..e1cf027df0 --- /dev/null +++ b/core-java/src/test/resources/encoding.txt @@ -0,0 +1 @@ +The façade pattern is a software-design pattern commonly used with object-oriented programming. \ No newline at end of file