- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringEncodingTest.java
More file actions
Latest commit
93 lines (82 loc) · 3.42 KB
/
Copy pathStringEncodingTest.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
packagebasics;
importorg.junit.jupiter.api.Assertions;
importorg.junit.jupiter.api.Test;
importjava.io.*;
importjava.nio.charset.*;
importjava.util.stream.Collectors;
importjava.util.stream.IntStream;
publicclassStringEncodingTest {
@Test
publicvoiddefault_encoding_test() {
try {
System.out.println(decodeText("The façade pattern is a software design pattern.", StandardCharsets.US_ASCII, CodingErrorAction.IGNORE));
} catch (IOExceptione) {
System.out.println("error");
}
}
@Test
publicvoidset_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 (IOExceptione) {
System.out.println("error");
}
}
@Test
publicvoidset_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 (IOExceptione) {
System.out.println("error");
}
}
@Test
publicvoidset_report_test() {
Assertions.assertThrows(
MalformedInputException.class,
() -> decodeText(
"The façade pattern is a software design pattern.",
StandardCharsets.US_ASCII,
CodingErrorAction.REPORT));
}
privateStringdecodeText(Stringinput, Charsetcharset,
CodingErrorActioncodingErrorAction) throwsIOException {
CharsetDecodercharsetDecoder = charset.newDecoder();
charsetDecoder.onMalformedInput(codingErrorAction);
return
newBufferedReader(
newInputStreamReader(
newByteArrayInputStream(input.getBytes()),
charsetDecoder)).readLine();
}
@Test
publicvoidascii_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 (UnsupportedEncodingExceptione) {
System.out.println("error");
}
}
privateStringconvertToBinary(Stringinput, Stringencoding)
throwsUnsupportedEncodingException {
byte[] encodedInput = input.getBytes(encoding);
returnIntStream.range(0, encodedInput.length)
.mapToObj(i -> String.format("%8s", Integer.toBinaryString(encodedInput[i] & 0xFF))
.replace(" ", "0")) // 음수를 피하기 위해 & 0xFF 사용
.collect(Collectors.joining(" "));
}
}