@@ -30,8 +30,8 @@ public final class IOUrl extends IO {
30
30
private static final HashMap <Integer , String > REASONS = new HashMap <>();
31
31
/** Optional SSL context for ignoring certificates. */
32
32
private static SSLContext ssl ;
33
- /** Global HTTP client instance . */
34
- private static HttpClient client ;
33
+ /** Cached HTTP client instances . */
34
+ private static final HttpClient [] CLIENTS = new HttpClient [ 2 ] ;
35
35
36
36
static {
37
37
REASONS .put (100 , "Continue" );
@@ -134,7 +134,7 @@ public InputStream inputStream() throws IOException {
134
134
* @throws IOException I/O exception
135
135
*/
136
136
public HttpResponse <InputStream > response () throws IOException {
137
- if ( client == null ) client = clientBuilder (true ). build ( );
137
+ final HttpClient client = client (true );
138
138
139
139
final HttpResponse <InputStream > response ;
140
140
try {
@@ -166,14 +166,18 @@ public HttpResponse<InputStream> response() throws IOException {
166
166
}
167
167
168
168
/**
169
- * Returns a unified HTTP client builder .
169
+ * Returns a singleton HTTP client instance .
170
170
* @param redirect follow redirects
171
171
* @return client builder
172
172
*/
173
- public static HttpClient .Builder clientBuilder (final boolean redirect ) {
174
- final HttpClient .Builder cb = HttpClient .newBuilder ();
175
- if (ssl != null ) cb .sslContext (ssl ).connectTimeout (Duration .ofMinutes (1 ));
176
- return cb .followRedirects (redirect ? Redirect .ALWAYS : Redirect .NEVER );
173
+ public static HttpClient client (final boolean redirect ) {
174
+ final int i = redirect ? 1 : 0 ;
175
+ if (CLIENTS [i ] == null ) {
176
+ final HttpClient .Builder cb = HttpClient .newBuilder ();
177
+ if (ssl != null ) cb .sslContext (ssl ).connectTimeout (Duration .ofMinutes (1 ));
178
+ CLIENTS [i ] = cb .followRedirects (redirect ? Redirect .ALWAYS : Redirect .NEVER ).build ();
179
+ }
180
+ return CLIENTS [i ];
177
181
}
178
182
179
183
/**
0 commit comments