9999import okhttp3 .ResponseBody ;
100100
101101/** Core S3 API client. */
102- public abstract class S3Base {
102+ public abstract class S3Base implements AutoCloseable {
103103 static {
104104 try {
105105 RequestBody .create (new byte [] {}, null );
@@ -136,7 +136,10 @@ public abstract class S3Base {
136136 protected String region ;
137137 protected Provider provider ;
138138 protected OkHttpClient httpClient ;
139+ protected boolean closeHttpClient ;
139140
141+ /** @deprecated This method is no longer supported. */
142+ @ Deprecated
140143 protected S3Base (
141144 HttpUrl baseUrl ,
142145 String awsS3Prefix ,
@@ -146,6 +149,28 @@ protected S3Base(
146149 String region ,
147150 Provider provider ,
148151 OkHttpClient httpClient ) {
152+ this (
153+ baseUrl ,
154+ awsS3Prefix ,
155+ awsDomainSuffix ,
156+ awsDualstack ,
157+ useVirtualStyle ,
158+ region ,
159+ provider ,
160+ httpClient ,
161+ false );
162+ }
163+
164+ protected S3Base (
165+ HttpUrl baseUrl ,
166+ String awsS3Prefix ,
167+ String awsDomainSuffix ,
168+ boolean awsDualstack ,
169+ boolean useVirtualStyle ,
170+ String region ,
171+ Provider provider ,
172+ OkHttpClient httpClient ,
173+ boolean closeHttpClient ) {
149174 this .baseUrl = baseUrl ;
150175 this .awsS3Prefix = awsS3Prefix ;
151176 this .awsDomainSuffix = awsDomainSuffix ;
@@ -154,6 +179,7 @@ protected S3Base(
154179 this .region = region ;
155180 this .provider = provider ;
156181 this .httpClient = httpClient ;
182+ this .closeHttpClient = closeHttpClient ;
157183 }
158184
159185 /** @deprecated This method is no longer supported. */
@@ -182,6 +208,7 @@ protected S3Base(
182208 this .region = region ;
183209 this .provider = provider ;
184210 this .httpClient = httpClient ;
211+ this .closeHttpClient = false ;
185212 }
186213
187214 protected S3Base (S3Base client ) {
@@ -193,6 +220,7 @@ protected S3Base(S3Base client) {
193220 this .region = client .region ;
194221 this .provider = client .provider ;
195222 this .httpClient = client .httpClient ;
223+ this .closeHttpClient = client .closeHttpClient ;
196224 }
197225
198226 /** Check whether argument is valid or not. */
@@ -3757,4 +3785,12 @@ protected CompletableFuture<UploadPartCopyResponse> uploadPartCopyAsync(
37573785 }
37583786 });
37593787 }
3788+
3789+ @ Override
3790+ public void close () throws Exception {
3791+ if (closeHttpClient ) {
3792+ httpClient .dispatcher ().executorService ().shutdown ();
3793+ httpClient .connectionPool ().evictAll ();
3794+ }
3795+ }
37603796}
0 commit comments