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 package org.apache.hc.core5.http.impl;
29
30 import java.nio.charset.Charset;
31 import java.nio.charset.CharsetDecoder;
32 import java.nio.charset.CharsetEncoder;
33 import java.nio.charset.CodingErrorAction;
34
35 import org.apache.hc.core5.http.config.CharCodingConfig;
36
37 public final class CharCodingSupport {
38
39 private CharCodingSupport() {
40 }
41
42 public static CharsetDecoder createDecoder(final CharCodingConfig cconfig) {
43 if (cconfig == null) {
44 return null;
45 }
46 final Charset charset = cconfig.getCharset();
47 final CodingErrorAction malformed = cconfig.getMalformedInputAction();
48 final CodingErrorAction unmappable = cconfig.getUnmappableInputAction();
49 if (charset != null) {
50 return charset.newDecoder()
51 .onMalformedInput(malformed != null ? malformed : CodingErrorAction.REPORT)
52 .onUnmappableCharacter(unmappable != null ? unmappable: CodingErrorAction.REPORT);
53 }
54 return null;
55 }
56
57 public static CharsetEncoder createEncoder(final CharCodingConfig cconfig) {
58 if (cconfig == null) {
59 return null;
60 }
61 final Charset charset = cconfig.getCharset();
62 if (charset != null) {
63 final CodingErrorAction malformed = cconfig.getMalformedInputAction();
64 final CodingErrorAction unmappable = cconfig.getUnmappableInputAction();
65 return charset.newEncoder()
66 .onMalformedInput(malformed != null ? malformed : CodingErrorAction.REPORT)
67 .onUnmappableCharacter(unmappable != null ? unmappable: CodingErrorAction.REPORT);
68 }
69 return null;
70 }
71
72 }