-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringEncodingTest.java
More file actions
93 lines (82 loc) · 3.42 KB
/
StringEncodingTest.java
File metadata and controls
93 lines (82 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package basics;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.nio.charset.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class StringEncodingTest {
@Test
public void default_encoding_test() {
try {
System.out.println(decodeText("The façade pattern is a software design pattern.", StandardCharsets.US_ASCII, CodingErrorAction.IGNORE));
} catch (IOException e) {
System.out.println("error");
}
}
@Test
public void set_ignore_test() {
try {
Assertions.assertEquals(
"The faade pattern is a software design pattern.",
decodeText(
"The façade pattern is a software design pattern.",
StandardCharsets.US_ASCII,
CodingErrorAction.IGNORE));
} catch (IOException e) {
System.out.println("error");
}
}
@Test
public void set_replace_test() {
try {
Assertions.assertEquals(
"The fa��ade pattern is a software design pattern.",
decodeText(
"The façade pattern is a software design pattern.",
StandardCharsets.US_ASCII,
CodingErrorAction.REPLACE));
} catch (IOException e) {
System.out.println("error");
}
}
@Test
public void set_report_test() {
Assertions.assertThrows(
MalformedInputException.class,
() -> decodeText(
"The façade pattern is a software design pattern.",
StandardCharsets.US_ASCII,
CodingErrorAction.REPORT));
}
private String decodeText(String input, Charset charset,
CodingErrorAction codingErrorAction) throws IOException {
CharsetDecoder charsetDecoder = charset.newDecoder();
charsetDecoder.onMalformedInput(codingErrorAction);
return
new BufferedReader(
new InputStreamReader(
new ByteArrayInputStream(input.getBytes()),
charsetDecoder)).readLine();
}
@Test
public void ascii_encoding_test() {
try {
Assertions.assertEquals(convertToBinary("T", "US-ASCII"), "01010100");
Assertions.assertEquals(convertToBinary("語", "Big5"), "10111011 01111001");
Assertions.assertEquals(convertToBinary("T", "UTF-32"), "00000000 00000000 00000000 01010100");
Assertions.assertEquals(convertToBinary("T", "UTF-8"), "01010100");
Assertions.assertEquals(convertToBinary("語", "UTF-8"), "11101000 10101010 10011110");
} catch (UnsupportedEncodingException e) {
System.out.println("error");
}
}
private String convertToBinary(String input, String encoding)
throws UnsupportedEncodingException {
byte[] encodedInput = input.getBytes(encoding);
return IntStream.range(0, encodedInput.length)
.mapToObj(i -> String.format("%8s", Integer.toBinaryString(encodedInput[i] & 0xFF))
.replace(" ", "0")) // 음수를 피하기 위해 & 0xFF 사용
.collect(Collectors.joining(" "));
}
}