Skip to content

Commit 613a83a

Browse files
author
Stephane Landelle
committed
closeSilently
1 parent b5af568 commit 613a83a

File tree

11 files changed

+56
-102
lines changed

11 files changed

+56
-102
lines changed

api/src/main/java/org/asynchttpclient/Body.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,4 @@ public interface Body extends Closeable {
3838
*/
3939
// FIXME introduce a visitor pattern so that Netty can pass a pooled buffer
4040
long read(ByteBuffer buffer) throws IOException;
41-
42-
/**
43-
* Releases any resources associated with this body.
44-
*
45-
* @throws IOException
46-
*/
47-
void close() throws IOException;
4841
}

api/src/main/java/org/asynchttpclient/BodyConsumer.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,8 @@ public interface BodyConsumer extends Closeable {
2525
/**
2626
* Consume the received bytes.
2727
*
28-
* @param byteBuffer a {@link ByteBuffer} represntation of the response's chunk.
28+
* @param byteBuffer a {@link ByteBuffer} representation of the response's chunk.
2929
* @throws IOException
3030
*/
3131
void consume(ByteBuffer byteBuffer) throws IOException;
32-
33-
/**
34-
* Invoked when all the response bytes has been processed.
35-
*
36-
* @throws IOException
37-
*/
38-
void close() throws IOException;
3932
}

api/src/main/java/org/asynchttpclient/SimpleAsyncHttpClient.java

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,7 @@
1212
*/
1313
package org.asynchttpclient;
1414

15-
import org.asynchttpclient.cookie.Cookie;
16-
import org.asynchttpclient.multipart.Part;
17-
import org.asynchttpclient.resumable.ResumableAsyncHandler;
18-
import org.asynchttpclient.resumable.ResumableIOExceptionFilter;
19-
import org.asynchttpclient.simple.HeaderMap;
20-
import org.asynchttpclient.simple.SimpleAHCTransferListener;
21-
import org.asynchttpclient.uri.UriComponents;
22-
import org.slf4j.Logger;
23-
import org.slf4j.LoggerFactory;
24-
25-
import javax.net.ssl.SSLContext;
15+
import static org.asynchttpclient.util.MiscUtils.closeSilently;
2616

2717
import java.io.Closeable;
2818
import java.io.IOException;
@@ -32,6 +22,16 @@
3222
import java.util.concurrent.ExecutorService;
3323
import java.util.concurrent.Future;
3424

25+
import javax.net.ssl.SSLContext;
26+
27+
import org.asynchttpclient.cookie.Cookie;
28+
import org.asynchttpclient.multipart.Part;
29+
import org.asynchttpclient.resumable.ResumableAsyncHandler;
30+
import org.asynchttpclient.resumable.ResumableIOExceptionFilter;
31+
import org.asynchttpclient.simple.HeaderMap;
32+
import org.asynchttpclient.simple.SimpleAHCTransferListener;
33+
import org.asynchttpclient.uri.UriComponents;
34+
3535
/**
3636
* Simple implementation of {@link AsyncHttpClient} and it's related builders ({@link AsyncHttpClientConfig},
3737
* {@link Realm}, {@link ProxyServer} and {@link AsyncHandler}. You can
@@ -64,7 +64,6 @@
6464
*/
6565
public class SimpleAsyncHttpClient implements Closeable {
6666

67-
private final static Logger logger = LoggerFactory.getLogger(SimpleAsyncHttpClient.class);
6867
private final AsyncHttpClientConfig config;
6968
private final RequestBuilder requestBuilder;
7069
private AsyncHttpClient asyncHttpClient;
@@ -778,13 +777,8 @@ public Response onCompleted(Response response) throws Exception {
778777
}
779778

780779
private void closeConsumer() {
781-
try {
782-
if (bodyConsumer != null) {
783-
bodyConsumer.close();
784-
}
785-
} catch (IOException ex) {
786-
logger.warn("Unable to close a BodyConsumer {}", bodyConsumer);
787-
}
780+
if (bodyConsumer != null)
781+
closeSilently(bodyConsumer);
788782
}
789783

790784
@Override

api/src/main/java/org/asynchttpclient/extra/ResumableRandomAccessFileListener.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.asynchttpclient.extra;
1414

15+
import static org.asynchttpclient.util.MiscUtils.closeSilently;
16+
1517
import org.asynchttpclient.resumable.ResumableListener;
1618

1719
import java.io.IOException;
@@ -52,13 +54,7 @@ public void onBytesReceived(ByteBuffer buffer) throws IOException {
5254
* {@inheritDoc}
5355
*/
5456
public void onAllBytesReceived() {
55-
if (file != null) {
56-
try {
57-
file.close();
58-
} catch (IOException e) {
59-
;
60-
}
61-
}
57+
closeSilently(file);
6258
}
6359

6460
/**
@@ -68,9 +64,7 @@ public long length() {
6864
try {
6965
return file.length();
7066
} catch (IOException e) {
71-
;
67+
return 0;
7268
}
73-
return 0;
7469
}
75-
7670
}

api/src/main/java/org/asynchttpclient/resumable/PropertiesBasedResumableProcessor.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,19 @@
1212
*/
1313
package org.asynchttpclient.resumable;
1414

15-
import org.asynchttpclient.util.StandardCharsets;
16-
import org.slf4j.Logger;
17-
import org.slf4j.LoggerFactory;
15+
import static org.asynchttpclient.util.MiscUtils.closeSilently;
1816

1917
import java.io.File;
2018
import java.io.FileNotFoundException;
2119
import java.io.FileOutputStream;
22-
import java.io.IOException;
2320
import java.util.Map;
2421
import java.util.Scanner;
2522
import java.util.concurrent.ConcurrentHashMap;
2623

24+
import org.asynchttpclient.util.StandardCharsets;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
2728
/**
2829
* A {@link org.asynchttpclient.resumable.ResumableAsyncHandler.ResumableProcessor} which use a properties file
2930
* to store the download index information.
@@ -81,12 +82,8 @@ public void save(Map<String, Long> map) {
8182
} catch (Throwable e) {
8283
log.warn(e.getMessage(), e);
8384
} finally {
84-
if (os != null) {
85-
try {
86-
os.close();
87-
} catch (IOException ignored) {
88-
}
89-
}
85+
if (os != null)
86+
closeSilently(os);
9087
}
9188
}
9289

api/src/main/java/org/asynchttpclient/util/MiscUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*/
1313
package org.asynchttpclient.util;
1414

15+
import java.io.Closeable;
16+
import java.io.IOException;
1517
import java.util.Collection;
1618
import java.util.Map;
1719

@@ -46,6 +48,13 @@ public static boolean getBoolean(String systemPropName, boolean defaultValue) {
4648
}
4749

4850
public static <T> T withDefault(T value, T defaults) {
49-
return value != null? value : value;
51+
return value != null ? value : value;
52+
}
53+
54+
public static void closeSilently(Closeable closeable) {
55+
try {
56+
closeable.close();
57+
} catch (IOException e) {
58+
}
5059
}
5160
}

api/src/test/java/org/asynchttpclient/multipart/MultipartBodyTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ private static File getTestfile() {
6262
private static void compareContentLength(final List<Part> parts) {
6363
Assert.assertNotNull(parts);
6464
// get expected values
65-
66-
// get real bytes
6765
final Body multipartBody = MultipartUtils.newMultipartBody(parts, new FluentCaseInsensitiveStringsMap());
6866
final long expectedContentLength = multipartBody.getContentLength();
6967
try {

providers/netty/src/main/java/org/asynchttpclient/providers/netty/request/body/BodyFileRegion.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,16 @@
1313
*/
1414
package org.asynchttpclient.providers.netty.request.body;
1515

16+
import static org.asynchttpclient.util.MiscUtils.closeSilently;
17+
18+
1619
import org.asynchttpclient.RandomAccessBody;
1720

1821
import io.netty.channel.FileRegion;
1922
import io.netty.util.AbstractReferenceCounted;
2023

2124
import java.io.IOException;
2225
import java.nio.channels.WritableByteChannel;
23-
2426
/**
2527
* Adapts a {@link RandomAccessBody} to Netty's {@link FileRegion}.
2628
*/
@@ -61,10 +63,6 @@ public long transferTo(WritableByteChannel target, long position) throws IOExcep
6163

6264
@Override
6365
protected void deallocate() {
64-
try {
65-
body.close();
66-
} catch (IOException e) {
67-
// we tried
68-
}
66+
closeSilently(body);
6967
}
7068
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/request/body/NettyBodyBody.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@
1313
*/
1414
package org.asynchttpclient.providers.netty.request.body;
1515

16+
import static org.asynchttpclient.util.MiscUtils.closeSilently;
17+
import io.netty.channel.Channel;
18+
import io.netty.channel.ChannelFuture;
19+
import io.netty.channel.ChannelProgressiveFuture;
20+
import io.netty.handler.codec.http.LastHttpContent;
21+
import io.netty.handler.stream.ChunkedWriteHandler;
22+
23+
import java.io.IOException;
24+
1625
import org.asynchttpclient.AsyncHttpClientConfig;
1726
import org.asynchttpclient.Body;
1827
import org.asynchttpclient.BodyGenerator;
@@ -22,21 +31,9 @@
2231
import org.asynchttpclient.providers.netty.future.NettyResponseFuture;
2332
import org.asynchttpclient.providers.netty.request.ProgressListener;
2433
import org.asynchttpclient.providers.netty.request.body.FeedableBodyGenerator.FeedListener;
25-
import org.slf4j.Logger;
26-
import org.slf4j.LoggerFactory;
27-
28-
import io.netty.channel.Channel;
29-
import io.netty.channel.ChannelFuture;
30-
import io.netty.channel.ChannelProgressiveFuture;
31-
import io.netty.handler.codec.http.LastHttpContent;
32-
import io.netty.handler.stream.ChunkedWriteHandler;
33-
34-
import java.io.IOException;
3534

3635
public class NettyBodyBody implements NettyBody {
3736

38-
private static final Logger LOGGER = LoggerFactory.getLogger(NettyBodyBody.class);
39-
4037
private final Body body;
4138
private final NettyAsyncHttpProviderConfig nettyConfig;
4239

@@ -63,7 +60,7 @@ public String getContentType() {
6360
public void write(final Channel channel, NettyResponseFuture<?> future, AsyncHttpClientConfig config) throws IOException {
6461

6562
Object msg;
66-
if (!ChannelManager.isSslHandlerConfigured(channel.pipeline()) && body instanceof RandomAccessBody && !nettyConfig.isDisableZeroCopy()) {
63+
if (body instanceof RandomAccessBody && !ChannelManager.isSslHandlerConfigured(channel.pipeline()) && !nettyConfig.isDisableZeroCopy()) {
6764
msg = new BodyFileRegion((RandomAccessBody) body);
6865

6966
} else {
@@ -83,11 +80,7 @@ public void onContentAdded() {
8380

8481
writeFuture.addListener(new ProgressListener(config, future.getAsyncHandler(), future, false, getContentLength()) {
8582
public void operationComplete(ChannelProgressiveFuture cf) {
86-
try {
87-
body.close();
88-
} catch (IOException e) {
89-
LOGGER.warn("Failed to close request body: {}", e.getMessage(), e);
90-
}
83+
closeSilently(body);
9184
super.operationComplete(cf);
9285
}
9386
});

providers/netty/src/main/java/org/asynchttpclient/providers/netty/request/body/NettyFileBody.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package org.asynchttpclient.providers.netty.request.body;
1515

16+
import static org.asynchttpclient.util.MiscUtils.closeSilently;
1617
import io.netty.channel.Channel;
1718
import io.netty.channel.ChannelFuture;
1819
import io.netty.channel.ChannelProgressiveFuture;
@@ -30,13 +31,9 @@
3031
import org.asynchttpclient.providers.netty.channel.ChannelManager;
3132
import org.asynchttpclient.providers.netty.future.NettyResponseFuture;
3233
import org.asynchttpclient.providers.netty.request.ProgressListener;
33-
import org.slf4j.Logger;
34-
import org.slf4j.LoggerFactory;
3534

3635
public class NettyFileBody implements NettyBody {
3736

38-
private static final Logger LOGGER = LoggerFactory.getLogger(NettyFileBody.class);
39-
4037
private final File file;
4138
private final long offset;
4239
private final long length;
@@ -88,23 +85,13 @@ public void write(Channel channel, NettyResponseFuture<?> future, AsyncHttpClien
8885
}
8986
writeFuture.addListener(new ProgressListener(config, future.getAsyncHandler(), future, false, getContentLength()) {
9087
public void operationComplete(ChannelProgressiveFuture cf) {
91-
try {
92-
// FIXME probably useless in Netty 4
93-
raf.close();
94-
} catch (IOException e) {
95-
LOGGER.warn("Failed to close request body: {}", e.getMessage(), e);
96-
}
88+
closeSilently(raf);
9789
super.operationComplete(cf);
9890
}
9991
});
10092
channel.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT);
10193
} catch (IOException ex) {
102-
if (raf != null) {
103-
try {
104-
raf.close();
105-
} catch (IOException e) {
106-
}
107-
}
94+
closeSilently(raf);
10895
throw ex;
10996
}
11097
}

providers/netty/src/main/java/org/asynchttpclient/providers/netty/request/body/NettyInputStreamBody.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package org.asynchttpclient.providers.netty.request.body;
1515

16+
import static org.asynchttpclient.util.MiscUtils.closeSilently;
17+
1618
import org.asynchttpclient.AsyncHttpClientConfig;
1719
import org.asynchttpclient.providers.netty.future.NettyResponseFuture;
1820
import org.asynchttpclient.providers.netty.request.ProgressListener;
@@ -69,11 +71,7 @@ public void write(Channel channel, NettyResponseFuture<?> future, AsyncHttpClien
6971
channel.write(new ChunkedStream(is), channel.newProgressivePromise()).addListener(
7072
new ProgressListener(config, future.getAsyncHandler(), future, false, getContentLength()) {
7173
public void operationComplete(ChannelProgressiveFuture cf) {
72-
try {
73-
is.close();
74-
} catch (IOException e) {
75-
LOGGER.warn("Failed to close request body: {}", e.getMessage(), e);
76-
}
74+
closeSilently(is);
7775
super.operationComplete(cf);
7876
}
7977
});

0 commit comments

Comments
 (0)