Skip to content

Commit abe483a

Browse files
authored
Merge pull request eugenp#5637 from kcacademic/character-encoding
BAEL-2300: Adding files for the tutorial on character encoding.
2 parents 8e6fff4 + 6f9e1fd commit abe483a

3 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.encoding;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.io.UnsupportedEncodingException;
8+
9+
public class CharacterEncodingExamples {
10+
11+
static String readFile(String filePath, String encoding) throws IOException {
12+
File file = new File(filePath);
13+
StringBuffer buffer = new StringBuffer();
14+
try (InputStreamReader isr = new InputStreamReader(new FileInputStream(file), encoding)) {
15+
int data;
16+
while ((data = isr.read()) != -1) {
17+
buffer.append((char) data);
18+
}
19+
}
20+
return buffer.toString();
21+
}
22+
23+
static String convertToBinary(String input, String encoding) throws UnsupportedEncodingException {
24+
byte[] bytes = input.getBytes(encoding);
25+
StringBuffer buffer = new StringBuffer();
26+
for (int b : bytes) {
27+
buffer.append(Integer.toBinaryString((b + 256) % 256));
28+
buffer.append(" ");
29+
}
30+
return buffer.toString();
31+
}
32+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.encoding;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
public class CharacterEncodingExamplesUnitTest {
9+
10+
@Test
11+
public void givenTextFile_whenCalledWithEncodingASCII_thenProduceIncorrectResult() throws IOException {
12+
Assert.assertEquals(
13+
CharacterEncodingExamples.readFile(
14+
"src/test/resources/encoding.txt", "US-ASCII"),
15+
"The fa��ade pattern is a software-design pattern commonly used with object-oriented programming.");
16+
}
17+
18+
@Test
19+
public void givenTextFile_whenCalledWithEncodingUTF8_thenProduceCorrectResult() throws IOException {
20+
Assert.assertEquals(
21+
CharacterEncodingExamples.readFile(
22+
"src/test/resources/encoding.txt", "UTF-8"),
23+
"The façade pattern is a software-design pattern commonly used with object-oriented programming.");
24+
}
25+
26+
@Test
27+
public void givenCharacterA_whenConvertedtoBinaryWithEncodingASCII_thenProduceResult() throws IOException {
28+
Assert.assertEquals(
29+
CharacterEncodingExamples.convertToBinary("A", "US-ASCII"),
30+
"1000001 ");
31+
}
32+
33+
@Test
34+
public void givenCharacterA_whenConvertedtoBinaryWithEncodingUTF8_thenProduceResult() throws IOException {
35+
Assert.assertEquals(
36+
CharacterEncodingExamples.convertToBinary("A", "UTF-8"),
37+
"1000001 ");
38+
}
39+
40+
@Test
41+
public void givenCharacterCh_whenConvertedtoBinaryWithEncodingBig5_thenProduceResult() throws IOException {
42+
Assert.assertEquals(
43+
CharacterEncodingExamples.convertToBinary("語", "Big5"),
44+
"10111011 1111001 ");
45+
}
46+
47+
@Test
48+
public void givenCharacterCh_whenConvertedtoBinaryWithEncodingUTF8_thenProduceResult() throws IOException {
49+
Assert.assertEquals(
50+
CharacterEncodingExamples.convertToBinary("語", "UTF-8"),
51+
"11101000 10101010 10011110 ");
52+
}
53+
54+
@Test
55+
public void givenCharacterCh_whenConvertedtoBinaryWithEncodingUTF32_thenProduceResult() throws IOException {
56+
Assert.assertEquals(
57+
CharacterEncodingExamples.convertToBinary("語", "UTF-32"),
58+
"0 0 10001010 10011110 ");
59+
}
60+
61+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The façade pattern is a software-design pattern commonly used with object-oriented programming.

0 commit comments

Comments
 (0)