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.net.SocketOptions;
31
32 import org.apache.http.annotation.Immutable;
33 import org.apache.http.util.Args;
34
35
36
37
38
39
40 @Immutable
41 public class SocketConfig implements Cloneable {
42
43 public static final SocketConfig DEFAULT = new Builder().build();
44
45 private final int soTimeout;
46 private final boolean soReuseAddress;
47 private final int soLinger;
48 private final boolean soKeepAlive;
49 private final boolean tcpNoDelay;
50
51 SocketConfig(
52 final int soTimeout,
53 final boolean soReuseAddress,
54 final int soLinger,
55 final boolean soKeepAlive,
56 final boolean tcpNoDelay) {
57 super();
58 this.soTimeout = soTimeout;
59 this.soReuseAddress = soReuseAddress;
60 this.soLinger = soLinger;
61 this.soKeepAlive = soKeepAlive;
62 this.tcpNoDelay = tcpNoDelay;
63 }
64
65
66
67
68
69
70
71
72 public int getSoTimeout() {
73 return soTimeout;
74 }
75
76
77
78
79
80
81
82
83
84 public boolean isSoReuseAddress() {
85 return soReuseAddress;
86 }
87
88
89
90
91
92
93
94
95
96 public int getSoLinger() {
97 return soLinger;
98 }
99
100
101
102
103
104
105
106
107
108 public boolean isSoKeepAlive() {
109 return this.soKeepAlive;
110 }
111
112
113
114
115
116
117
118
119
120 public boolean isTcpNoDelay() {
121 return tcpNoDelay;
122 }
123
124 @Override
125 protected SocketConfig clone() throws CloneNotSupportedException {
126 return (SocketConfig) super.clone();
127 }
128
129 @Override
130 public String toString() {
131 final StringBuilder builder = new StringBuilder();
132 builder.append("[soTimeout=").append(this.soTimeout)
133 .append(", soReuseAddress=").append(this.soReuseAddress)
134 .append(", soLinger=").append(this.soLinger)
135 .append(", soKeepAlive=").append(this.soKeepAlive)
136 .append(", tcpNoDelay=").append(this.tcpNoDelay)
137 .append("]");
138 return builder.toString();
139 }
140
141 public static SocketConfig.Builder custom() {
142 return new Builder();
143 }
144
145 public static SocketConfig.Builder copy(final SocketConfig config) {
146 Args.notNull(config, "Socket config");
147 return new Builder()
148 .setSoTimeout(config.getSoTimeout())
149 .setSoReuseAddress(config.isSoReuseAddress())
150 .setSoLinger(config.getSoLinger())
151 .setSoKeepAlive(config.isSoKeepAlive())
152 .setTcpNoDelay(config.isTcpNoDelay());
153 }
154
155 public static class Builder {
156
157 private int soTimeout;
158 private boolean soReuseAddress;
159 private int soLinger;
160 private boolean soKeepAlive;
161 private boolean tcpNoDelay;
162
163 Builder() {
164 this.soLinger = -1;
165 this.tcpNoDelay = true;
166 }
167
168 public Builder setSoTimeout(final int soTimeout) {
169 this.soTimeout = soTimeout;
170 return this;
171 }
172
173 public Builder setSoReuseAddress(final boolean soReuseAddress) {
174 this.soReuseAddress = soReuseAddress;
175 return this;
176 }
177
178 public Builder setSoLinger(final int soLinger) {
179 this.soLinger = soLinger;
180 return this;
181 }
182
183 public Builder setSoKeepAlive(final boolean soKeepAlive) {
184 this.soKeepAlive = soKeepAlive;
185 return this;
186 }
187
188 public Builder setTcpNoDelay(final boolean tcpNoDelay) {
189 this.tcpNoDelay = tcpNoDelay;
190 return this;
191 }
192
193 public SocketConfig build() {
194 return new SocketConfig(soTimeout, soReuseAddress, soLinger, soKeepAlive, tcpNoDelay);
195 }
196
197 }
198
199 }