You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/10/03 08:51:41 UTC
[6/7] httpcomponents-client git commit: Removed IOUtils as redundant;
try-with-resources for resource management
Removed IOUtils as redundant; try-with-resources for resource management
Project: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/repo
Commit: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/commit/e71d8fac
Tree: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/tree/e71d8fac
Diff: http://git-wip-us.apache.org/repos/asf/httpcomponents-client/diff/e71d8fac
Branch: refs/heads/master
Commit: e71d8fac889e9dc18b1e9a57f8e890ce1ea2b9d8
Parents: 1d66105
Author: Oleg Kalnichevski <ol...@apache.org>
Authored: Tue Oct 3 09:43:03 2017 +0200
Committer: Oleg Kalnichevski <ol...@apache.org>
Committed: Tue Oct 3 09:43:03 2017 +0200
----------------------------------------------------------------------
.../hc/client5/http/impl/cache/CachingExec.java | 7 +-
.../http/impl/cache/FileResourceFactory.java | 22 +++-
.../hc/client5/http/impl/cache/IOUtils.java | 110 -------------------
.../impl/cache/TestHttpCacheJiraNumber1147.java | 5 +-
...taleWhileRevalidationReleasesConnection.java | 9 +-
5 files changed, 27 insertions(+), 126 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/e71d8fac/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
index 660b4cd..a4d6aee 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/CachingExec.java
@@ -68,6 +68,7 @@ import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.http.HttpVersion;
import org.apache.hc.core5.http.ProtocolVersion;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
import org.apache.hc.core5.http.message.MessageSupport;
@@ -677,7 +678,7 @@ public class CachingExec implements ExecChainHandler {
final Header resultEtagHeader = backendResponse.getFirstHeader(HeaderConstants.ETAG);
if (resultEtagHeader == null) {
log.warn("304 response did not contain ETag");
- IOUtils.consume(backendResponse.getEntity());
+ EntityUtils.consume(backendResponse.getEntity());
backendResponse.close();
return callBackend(target, request, scope, chain);
}
@@ -686,7 +687,7 @@ public class CachingExec implements ExecChainHandler {
final Variant matchingVariant = variants.get(resultEtag);
if (matchingVariant == null) {
log.debug("304 response did not contain ETag matching one sent in If-None-Match");
- IOUtils.consume(backendResponse.getEntity());
+ EntityUtils.consume(backendResponse.getEntity());
backendResponse.close();
return callBackend(target, request, scope, chain);
}
@@ -694,7 +695,7 @@ public class CachingExec implements ExecChainHandler {
final HttpCacheEntry matchedEntry = matchingVariant.getEntry();
if (revalidationResponseIsTooOld(backendResponse, matchedEntry)) {
- IOUtils.consume(backendResponse.getEntity());
+ EntityUtils.consume(backendResponse.getEntity());
backendResponse.close();
return retryRequestUnconditionally(target, request, scope, chain);
}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/e71d8fac/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/FileResourceFactory.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/FileResourceFactory.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/FileResourceFactory.java
index b3c59ff..d1c8232 100644
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/FileResourceFactory.java
+++ b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/FileResourceFactory.java
@@ -29,6 +29,9 @@ package org.apache.hc.client5.http.impl.cache;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.RandomAccessFile;
+import java.nio.channels.FileChannel;
import org.apache.hc.client5.http.cache.Resource;
import org.apache.hc.client5.http.cache.ResourceFactory;
@@ -97,14 +100,23 @@ public class FileResourceFactory implements ResourceFactory {
final String requestId,
final Resource resource) throws ResourceIOException {
final File file = generateUniqueCacheFile(requestId);
-
try {
if (resource instanceof FileResource) {
- final File src = ((FileResource) resource).getFile();
- IOUtils.copyFile(src, file);
+ try (final RandomAccessFile srcFile = new RandomAccessFile(((FileResource) resource).getFile(), "r");
+ final RandomAccessFile dstFile = new RandomAccessFile(file, "rw");
+ final FileChannel src = srcFile.getChannel();
+ final FileChannel dst = dstFile.getChannel()) {
+ src.transferTo(0, srcFile.length(), dst);
+ }
} else {
- final FileOutputStream out = new FileOutputStream(file);
- IOUtils.copyAndClose(resource.getInputStream(), out);
+ try (final FileOutputStream out = new FileOutputStream(file);
+ final InputStream in = resource.getInputStream()) {
+ final byte[] buf = new byte[2048];
+ int len;
+ while ((len = in.read(buf)) != -1) {
+ out.write(buf, 0, len);
+ }
+ }
}
} catch (final IOException ex) {
throw new ResourceIOException(ex.getMessage(), ex);
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/e71d8fac/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/IOUtils.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/IOUtils.java b/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/IOUtils.java
deleted file mode 100644
index 551b998..0000000
--- a/httpclient5-cache/src/main/java/org/apache/hc/client5/http/impl/cache/IOUtils.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * ====================================================================
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- *
- */
-package org.apache.hc.client5.http.impl.cache;
-
-import java.io.Closeable;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.RandomAccessFile;
-import java.nio.channels.FileChannel;
-
-import org.apache.hc.core5.annotation.Contract;
-import org.apache.hc.core5.annotation.ThreadingBehavior;
-import org.apache.hc.core5.http.HttpEntity;
-
-@Contract(threading = ThreadingBehavior.IMMUTABLE)
-class IOUtils {
-
- static void consume(final HttpEntity entity) throws IOException {
- if (entity == null) {
- return;
- }
- if (entity.isStreaming()) {
- final InputStream instream = entity.getContent();
- if (instream != null) {
- instream.close();
- }
- }
- }
-
- static void copy(final InputStream in, final OutputStream out) throws IOException {
- final byte[] buf = new byte[2048];
- int len;
- while ((len = in.read(buf)) != -1) {
- out.write(buf, 0, len);
- }
- }
-
- static void closeSilently(final Closeable closable) {
- try {
- closable.close();
- } catch (final IOException ignore) {
- }
- }
-
- static void copyAndClose(final InputStream in, final OutputStream out) throws IOException {
- try {
- copy(in, out);
- in.close();
- out.close();
- } catch (final IOException ex) {
- closeSilently(in);
- closeSilently(out);
- // Propagate the original exception
- throw ex;
- }
- }
-
- static void copyFile(final File in, final File out) throws IOException {
- final RandomAccessFile f1 = new RandomAccessFile(in, "r");
- final RandomAccessFile f2 = new RandomAccessFile(out, "rw");
- try {
- final FileChannel c1 = f1.getChannel();
- final FileChannel c2 = f2.getChannel();
- try {
- c1.transferTo(0, f1.length(), c2);
- c1.close();
- c2.close();
- } catch (final IOException ex) {
- closeSilently(c1);
- closeSilently(c2);
- // Propagate the original exception
- throw ex;
- }
- f1.close();
- f2.close();
- } catch (final IOException ex) {
- closeSilently(f1);
- closeSilently(f2);
- // Propagate the original exception
- throw ex;
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/e71d8fac/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestHttpCacheJiraNumber1147.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestHttpCacheJiraNumber1147.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestHttpCacheJiraNumber1147.java
index 132c2b5..4ede4ba 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestHttpCacheJiraNumber1147.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestHttpCacheJiraNumber1147.java
@@ -48,6 +48,7 @@ import org.apache.hc.client5.http.utils.DateUtils;
import org.apache.hc.core5.http.ClassicHttpRequest;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.HttpHost;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
import org.junit.After;
import org.junit.Assert;
@@ -121,7 +122,7 @@ public class TestHttpCacheJiraNumber1147 {
final ExecChain.Scope scope = new ExecChain.Scope("teset", route, get, mockEndpoint, context);
final ClassicHttpResponse response1 = t.execute(get, scope, mockExecChain);
Assert.assertEquals(200, response1.getCode());
- IOUtils.consume(response1.getEntity());
+ EntityUtils.consume(response1.getEntity());
verify(mockExecChain).proceed(isA(ClassicHttpRequest.class), isA(ExecChain.Scope.class));
@@ -132,7 +133,7 @@ public class TestHttpCacheJiraNumber1147 {
final ClassicHttpResponse response2 = t.execute(get, scope, mockExecChain);
Assert.assertEquals(200, response2.getCode());
- IOUtils.consume(response2.getEntity());
+ EntityUtils.consume(response2.getEntity());
verify(mockExecChain, Mockito.times(2)).proceed(
isA(ClassicHttpRequest.class),
http://git-wip-us.apache.org/repos/asf/httpcomponents-client/blob/e71d8fac/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestStaleWhileRevalidationReleasesConnection.java
----------------------------------------------------------------------
diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestStaleWhileRevalidationReleasesConnection.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestStaleWhileRevalidationReleasesConnection.java
index 3f43745..55bca7f 100644
--- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestStaleWhileRevalidationReleasesConnection.java
+++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestStaleWhileRevalidationReleasesConnection.java
@@ -34,7 +34,6 @@ import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
-import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.cache.CacheResponseStatus;
import org.apache.hc.client5.http.cache.HttpCacheContext;
import org.apache.hc.client5.http.classic.methods.HttpGet;
@@ -53,6 +52,7 @@ import org.apache.hc.core5.http.impl.bootstrap.HttpServer;
import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap;
import org.apache.hc.core5.http.io.HttpRequestHandler;
import org.apache.hc.core5.http.io.entity.ByteArrayEntity;
+import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.protocol.BasicHttpContext;
import org.apache.hc.core5.http.protocol.HttpContext;
import org.junit.After;
@@ -198,17 +198,14 @@ public class TestStaleWhileRevalidationReleasesConnection {
try {
response = cachingClient.execute(httpget, localContext);
return null;
- } catch (final ClientProtocolException e1) {
- return e1;
} catch (final IOException e1) {
return e1;
} finally {
if(response!=null) {
final HttpEntity entity = response.getEntity();
try {
- IOUtils.consume(entity);
- } catch (final IOException e) {
- e.printStackTrace();
+ EntityUtils.consume(entity);
+ } catch (final IOException ingnore) {
}
}
}