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