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.config;
29
30 import java.nio.charset.Charset;
31 import java.nio.charset.CodingErrorAction;
32 import java.nio.charset.StandardCharsets;
33
34 import org.apache.hc.core5.annotation.Contract;
35 import org.apache.hc.core5.annotation.ThreadingBehavior;
36 import org.apache.hc.core5.util.Args;
37
38
39
40
41
42
43 @Contract(threading = ThreadingBehavior.IMMUTABLE)
44 public class CharCodingConfig {
45
46 public static final CharCodingConfig DEFAULT = new Builder().build();
47
48 private static final Charset DEFAULT_CHARSET = StandardCharsets.US_ASCII;
49
50 private final Charset charset;
51 private final CodingErrorAction malformedInputAction;
52 private final CodingErrorAction unmappableInputAction;
53
54 CharCodingConfig(
55 final Charset charset,
56 final CodingErrorAction malformedInputAction,
57 final CodingErrorAction unmappableInputAction) {
58 super();
59 this.charset = charset;
60 this.malformedInputAction = malformedInputAction;
61 this.unmappableInputAction = unmappableInputAction;
62 }
63
64 public Charset getCharset() {
65 return charset;
66 }
67
68 public CodingErrorAction getMalformedInputAction() {
69 return malformedInputAction;
70 }
71
72 public CodingErrorAction getUnmappableInputAction() {
73 return unmappableInputAction;
74 }
75
76 @Override
77 public String toString() {
78 final StringBuilder builder = new StringBuilder();
79 builder.append("[charset=").append(this.charset)
80 .append(", malformedInputAction=").append(this.malformedInputAction)
81 .append(", unmappableInputAction=").append(this.unmappableInputAction)
82 .append("]");
83 return builder.toString();
84 }
85
86 public static CharCodingConfig.Builder custom() {
87 return new Builder();
88 }
89
90 public static CharCodingConfig.Builder copy(final CharCodingConfig config) {
91 Args.notNull(config, "Config");
92 return new Builder()
93 .setCharset(config.getCharset())
94 .setMalformedInputAction(config.getMalformedInputAction())
95 .setUnmappableInputAction(config.getUnmappableInputAction());
96 }
97
98 public static class Builder {
99
100 private Charset charset;
101 private CodingErrorAction malformedInputAction;
102 private CodingErrorAction unmappableInputAction;
103
104 Builder() {
105 }
106
107 public Builder setCharset(final Charset charset) {
108 this.charset = charset;
109 return this;
110 }
111
112 public Builder setMalformedInputAction(final CodingErrorAction malformedInputAction) {
113 this.malformedInputAction = malformedInputAction;
114 if (malformedInputAction != null && this.charset == null) {
115 this.charset = DEFAULT_CHARSET;
116 }
117 return this;
118 }
119
120 public Builder setUnmappableInputAction(final CodingErrorAction unmappableInputAction) {
121 this.unmappableInputAction = unmappableInputAction;
122 if (unmappableInputAction != null && this.charset == null) {
123 this.charset = DEFAULT_CHARSET;
124 }
125 return this;
126 }
127
128 public CharCodingConfig build() {
129 Charset charsetCopy = charset;
130 if (charsetCopy == null && (malformedInputAction != null || unmappableInputAction != null)) {
131 charsetCopy = DEFAULT_CHARSET;
132 }
133 return new CharCodingConfig(charsetCopy, malformedInputAction, unmappableInputAction);
134 }
135
136 }
137
138 }