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 org.apache.http.annotation.ThreadingBehavior;
31 import org.apache.http.annotation.Contract;
32 import org.apache.http.util.Args;
33
34
35
36
37
38
39 @Contract(threading = ThreadingBehavior.IMMUTABLE)
40 public class SocketConfig implements Cloneable {
41
42 public static final SocketConfig DEFAULT = new Builder().build();
43
44 private final int soTimeout;
45 private final boolean soReuseAddress;
46 private final int soLinger;
47 private final boolean soKeepAlive;
48 private final boolean tcpNoDelay;
49 private final int sndBufSize;
50 private final int rcvBufSize;
51 private final int backlogSize;
52
53 SocketConfig(
54 final int soTimeout,
55 final boolean soReuseAddress,
56 final int soLinger,
57 final boolean soKeepAlive,
58 final boolean tcpNoDelay,
59 final int sndBufSize,
60 final int rcvBufSize,
61 final int backlogSize) {
62 super();
63 this.soTimeout = soTimeout;
64 this.soReuseAddress = soReuseAddress;
65 this.soLinger = soLinger;
66 this.soKeepAlive = soKeepAlive;
67 this.tcpNoDelay = tcpNoDelay;
68 this.sndBufSize = sndBufSize;
69 this.rcvBufSize = rcvBufSize;
70 this.backlogSize = backlogSize;
71 }
72
73
74
75
76
77
78
79
80
81
82 public int getSoTimeout() {
83 return soTimeout;
84 }
85
86
87
88
89
90
91
92
93
94
95
96 public boolean isSoReuseAddress() {
97 return soReuseAddress;
98 }
99
100
101
102
103
104
105
106
107
108
109
110 public int getSoLinger() {
111 return soLinger;
112 }
113
114
115
116
117
118
119
120
121
122
123
124 public boolean isSoKeepAlive() {
125 return soKeepAlive;
126 }
127
128
129
130
131
132
133
134
135
136
137
138 public boolean isTcpNoDelay() {
139 return tcpNoDelay;
140 }
141
142
143
144
145
146
147
148
149
150
151
152
153 public int getSndBufSize() {
154 return sndBufSize;
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168 public int getRcvBufSize() {
169 return rcvBufSize;
170 }
171
172
173
174
175
176
177
178
179
180
181 public int getBacklogSize() {
182 return backlogSize;
183 }
184
185 @Override
186 protected SocketConfig clone() throws CloneNotSupportedException {
187 return (SocketConfig) super.clone();
188 }
189
190 @Override
191 public String toString() {
192 final StringBuilder builder = new StringBuilder();
193 builder.append("[soTimeout=").append(this.soTimeout)
194 .append(", soReuseAddress=").append(this.soReuseAddress)
195 .append(", soLinger=").append(this.soLinger)
196 .append(", soKeepAlive=").append(this.soKeepAlive)
197 .append(", tcpNoDelay=").append(this.tcpNoDelay)
198 .append(", sndBufSize=").append(this.sndBufSize)
199 .append(", rcvBufSize=").append(this.rcvBufSize)
200 .append(", backlogSize=").append(this.backlogSize)
201 .append("]");
202 return builder.toString();
203 }
204
205 public static SocketConfig.Builder custom() {
206 return new Builder();
207 }
208
209 public static SocketConfig.Builder copy(final SocketConfig config) {
210 Args.notNull(config, "Socket config");
211 return new Builder()
212 .setSoTimeout(config.getSoTimeout())
213 .setSoReuseAddress(config.isSoReuseAddress())
214 .setSoLinger(config.getSoLinger())
215 .setSoKeepAlive(config.isSoKeepAlive())
216 .setTcpNoDelay(config.isTcpNoDelay())
217 .setSndBufSize(config.getSndBufSize())
218 .setRcvBufSize(config.getRcvBufSize())
219 .setBacklogSize(config.getBacklogSize());
220 }
221
222 public static class Builder {
223
224 private int soTimeout;
225 private boolean soReuseAddress;
226 private int soLinger;
227 private boolean soKeepAlive;
228 private boolean tcpNoDelay;
229 private int sndBufSize;
230 private int rcvBufSize;
231 private int backlogSize;
232
233 Builder() {
234 this.soLinger = -1;
235 this.tcpNoDelay = true;
236 }
237
238 public Builder setSoTimeout(final int soTimeout) {
239 this.soTimeout = soTimeout;
240 return this;
241 }
242
243 public Builder setSoReuseAddress(final boolean soReuseAddress) {
244 this.soReuseAddress = soReuseAddress;
245 return this;
246 }
247
248 public Builder setSoLinger(final int soLinger) {
249 this.soLinger = soLinger;
250 return this;
251 }
252
253 public Builder setSoKeepAlive(final boolean soKeepAlive) {
254 this.soKeepAlive = soKeepAlive;
255 return this;
256 }
257
258 public Builder setTcpNoDelay(final boolean tcpNoDelay) {
259 this.tcpNoDelay = tcpNoDelay;
260 return this;
261 }
262
263
264
265
266 public Builder setSndBufSize(final int sndBufSize) {
267 this.sndBufSize = sndBufSize;
268 return this;
269 }
270
271
272
273
274 public Builder setRcvBufSize(final int rcvBufSize) {
275 this.rcvBufSize = rcvBufSize;
276 return this;
277 }
278
279
280
281
282 public Builder setBacklogSize(final int backlogSize) {
283 this.backlogSize = backlogSize;
284 return this;
285 }
286
287 public SocketConfig build() {
288 return new SocketConfig(soTimeout, soReuseAddress, soLinger, soKeepAlive, tcpNoDelay,
289 sndBufSize, rcvBufSize, backlogSize);
290 }
291
292 }
293
294 }