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.http.config;
29
30 import java.nio.charset.Charset;
31 import java.nio.charset.CodingErrorAction;
32
33 import org.apache.http.Consts;
34 import org.apache.http.annotation.Immutable;
35 import org.apache.http.util.Args;
36
37
38
39
40
41
42 @Immutable
43 public class ConnectionConfig implements Cloneable {
44
45 public static final ConnectionConfig DEFAULT = new Builder().build();
46
47 private final int bufferSize;
48 private final int fragmentSizeHint;
49 private final Charset charset;
50 private final CodingErrorAction malformedInputAction;
51 private final CodingErrorAction unmappableInputAction;
52 private final MessageConstraints messageConstraints;
53
54 ConnectionConfig(
55 final int bufferSize,
56 final int fragmentSizeHint,
57 final Charset charset,
58 final CodingErrorAction malformedInputAction,
59 final CodingErrorAction unmappableInputAction,
60 final MessageConstraints messageConstraints) {
61 super();
62 this.bufferSize = bufferSize;
63 this.fragmentSizeHint = fragmentSizeHint;
64 this.charset = charset;
65 this.malformedInputAction = malformedInputAction;
66 this.unmappableInputAction = unmappableInputAction;
67 this.messageConstraints = messageConstraints;
68 }
69
70 public int getBufferSize() {
71 return bufferSize;
72 }
73
74 public int getFragmentSizeHint() {
75 return fragmentSizeHint;
76 }
77
78 public Charset getCharset() {
79 return charset;
80 }
81
82 public CodingErrorAction getMalformedInputAction() {
83 return malformedInputAction;
84 }
85
86 public CodingErrorAction getUnmappableInputAction() {
87 return unmappableInputAction;
88 }
89
90 public MessageConstraints getMessageConstraints() {
91 return messageConstraints;
92 }
93
94 @Override
95 protected ConnectionConfig clone() throws CloneNotSupportedException {
96 return (ConnectionConfig) super.clone();
97 }
98
99 @Override
100 public String toString() {
101 final StringBuilder builder = new StringBuilder();
102 builder.append("[bufferSize=").append(this.bufferSize)
103 .append(", fragmentSizeHint=").append(this.fragmentSizeHint)
104 .append(", charset=").append(this.charset)
105 .append(", malformedInputAction=").append(this.malformedInputAction)
106 .append(", unmappableInputAction=").append(this.unmappableInputAction)
107 .append(", messageConstraints=").append(this.messageConstraints)
108 .append("]");
109 return builder.toString();
110 }
111
112 public static ConnectionConfig.Builder custom() {
113 return new Builder();
114 }
115
116 public static ConnectionConfig.Builder copy(final ConnectionConfig config) {
117 Args.notNull(config, "Connection config");
118 return new Builder()
119 .setCharset(config.getCharset())
120 .setMalformedInputAction(config.getMalformedInputAction())
121 .setUnmappableInputAction(config.getUnmappableInputAction())
122 .setMessageConstraints(config.getMessageConstraints());
123 }
124
125 public static class Builder {
126
127 private int bufferSize;
128 private int fragmentSizeHint;
129 private Charset charset;
130 private CodingErrorAction malformedInputAction;
131 private CodingErrorAction unmappableInputAction;
132 private MessageConstraints messageConstraints;
133
134 Builder() {
135 this.fragmentSizeHint = -1;
136 }
137
138 public Builder setBufferSize(final int bufferSize) {
139 this.bufferSize = bufferSize;
140 return this;
141 }
142
143 public Builder setFragmentSizeHint(final int fragmentSizeHint) {
144 this.fragmentSizeHint = fragmentSizeHint;
145 return this;
146 }
147
148 public Builder setCharset(final Charset charset) {
149 this.charset = charset;
150 return this;
151 }
152
153 public Builder setMalformedInputAction(final CodingErrorAction malformedInputAction) {
154 this.malformedInputAction = malformedInputAction;
155 if (malformedInputAction != null && this.charset == null) {
156 this.charset = Consts.ASCII;
157 }
158 return this;
159 }
160
161 public Builder setUnmappableInputAction(final CodingErrorAction unmappableInputAction) {
162 this.unmappableInputAction = unmappableInputAction;
163 if (unmappableInputAction != null && this.charset == null) {
164 this.charset = Consts.ASCII;
165 }
166 return this;
167 }
168
169 public Builder setMessageConstraints(final MessageConstraints messageConstraints) {
170 this.messageConstraints = messageConstraints;
171 return this;
172 }
173
174 public ConnectionConfig build() {
175 Charset cs = charset;
176 if (cs == null && (malformedInputAction != null || unmappableInputAction != null)) {
177 cs = Consts.ASCII;
178 }
179 final int bufSize = this.bufferSize > 0 ? this.bufferSize : 8 * 1024;
180 final int fragmentHintSize = this.fragmentSizeHint >= 0 ? this.fragmentSizeHint : bufSize;
181 return new ConnectionConfig(
182 bufSize,
183 fragmentHintSize,
184 charset,
185 malformedInputAction,
186 unmappableInputAction,
187 messageConstraints);
188 }
189
190 }
191
192 }