diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java index b64b27a51e..b2927be97b 100644 --- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java +++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/hpack/HPackEncoder.java @@ -243,8 +243,7 @@ private int findFullMatch(final List entries, final String value) { if (entries == null || entries.isEmpty()) { return 0; } - for (int i = 0; i < entries.size(); i++) { - final HPackEntry entry = entries.get(i); + for (final HPackEntry entry : entries) { if (Objects.equals(value, entry.getHeader().getValue())) { return entry.getIndex(); } @@ -303,8 +302,8 @@ void encodeHeader( void encodeHeaders( final ByteArrayBuffer dst, final List headers, final boolean noIndexing, final boolean useHuffman) throws CharacterCodingException { - for (int i = 0; i < headers.size(); i++) { - encodeHeader(dst, headers.get(i), noIndexing, useHuffman); + for (final Header header : headers) { + encodeHeader(dst, header, noIndexing, useHuffman); } } diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2RequestConverter.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2RequestConverter.java index c1ad90ca55..8cf20eb00c 100644 --- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2RequestConverter.java +++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2RequestConverter.java @@ -150,9 +150,7 @@ public HttpRequest convert(final List
headers) throws HttpException { throw new ProtocolException(ex.getMessage(), ex); } httpRequest.setPath(path); - for (int i = 0; i < messageHeaders.size(); i++) { - httpRequest.addHeader(messageHeaders.get(i)); - } + messageHeaders.forEach(httpRequest::addHeader); return httpRequest; } diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2ResponseConverter.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2ResponseConverter.java index 0b860be8b5..82dd1d4d87 100644 --- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2ResponseConverter.java +++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/DefaultH2ResponseConverter.java @@ -102,9 +102,7 @@ public HttpResponse convert(final List
headers) throws HttpException { } final HttpResponse response = new BasicHttpResponse(statusCode, null); response.setVersion(HttpVersion.HTTP_2); - for (int i = 0; i < messageHeaders.size(); i++) { - response.addHeader(messageHeaders.get(i)); - } + messageHeaders.forEach(response::addHeader); return response; } diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java index 431ee5b0b6..9e526f5ea6 100644 --- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java +++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/AbstractH2StreamMultiplexer.java @@ -36,6 +36,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Queue; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedDeque; @@ -478,8 +479,7 @@ public final void onOutput() throws HttpException, IOException { final int pendingOutputRequests = outputRequests.get(); boolean outputPending = false; if (!streamMap.isEmpty() && connOutputWindow.get() > 0) { - for (final Iterator> it = streamMap.entrySet().iterator(); it.hasNext(); ) { - final Map.Entry entry = it.next(); + for (final Entry entry : streamMap.entrySet()) { final H2Stream stream = entry.getValue(); if (!stream.isLocalClosed() && stream.getOutputWindow().get() > 0 @@ -553,8 +553,7 @@ public final void onTimeout(final Timeout timeout) throws HttpException, IOExcep "Timeout due to inactivity (" + timeout + ")"); } commitFrame(goAway); - for (final Iterator> it = streamMap.entrySet().iterator(); it.hasNext(); ) { - final Map.Entry entry = it.next(); + for (final Entry entry : streamMap.entrySet()) { final H2Stream stream = entry.getValue(); stream.reset(new H2StreamResetException(H2Error.NO_ERROR, "Timeout due to inactivity (" + timeout + ")")); } @@ -570,8 +569,7 @@ public final void onDisconnect() { break; } } - for (final Iterator> it = streamMap.entrySet().iterator(); it.hasNext(); ) { - final Map.Entry entry = it.next(); + for (final Entry entry : streamMap.entrySet()) { final H2Stream stream = entry.getValue(); stream.cancel(); } @@ -598,8 +596,7 @@ private void processPendingCommands() throws IOException, HttpException { if (command instanceof ShutdownCommand) { final ShutdownCommand shutdownCommand = (ShutdownCommand) command; if (shutdownCommand.getType() == CloseMode.IMMEDIATE) { - for (final Iterator> it = streamMap.entrySet().iterator(); it.hasNext(); ) { - final Map.Entry entry = it.next(); + for (final Entry entry : streamMap.entrySet()) { final H2Stream stream = entry.getValue(); stream.cancel(); } @@ -673,8 +670,7 @@ public final void onException(final Exception cause) { break; } } - for (final Iterator> it = streamMap.entrySet().iterator(); it.hasNext(); ) { - final Map.Entry entry = it.next(); + for (final Entry entry : streamMap.entrySet()) { final H2Stream stream = entry.getValue(); stream.reset(cause); } @@ -1009,8 +1005,7 @@ private void consumeFrame(final RawFrame frame) throws HttpException, IOExceptio } connState = streamMap.isEmpty() ? ConnectionHandshake.SHUTDOWN : ConnectionHandshake.GRACEFUL_SHUTDOWN; } else { - for (final Iterator> it = streamMap.entrySet().iterator(); it.hasNext(); ) { - final Map.Entry entry = it.next(); + for (final Entry entry : streamMap.entrySet()) { final H2Stream stream = entry.getValue(); stream.reset(new H2StreamResetException(errorCode, "Connection terminated by the peer (" + errorCode + ")")); } @@ -1233,8 +1228,7 @@ private void applyRemoteSettings(final H2Config config) throws H2ConnectionExcep if (delta != 0) { if (!streamMap.isEmpty()) { - for (final Iterator> it = streamMap.entrySet().iterator(); it.hasNext(); ) { - final Map.Entry entry = it.next(); + for (final Entry entry : streamMap.entrySet()) { final H2Stream stream = entry.getValue(); try { updateOutputWindow(stream.getId(), stream.getOutputWindow(), delta); @@ -1254,8 +1248,7 @@ private void applyLocalSettings() throws H2ConnectionException { initInputWinSize = localConfig.getInitialWindowSize(); if (delta != 0 && !streamMap.isEmpty()) { - for (final Iterator> it = streamMap.entrySet().iterator(); it.hasNext(); ) { - final Map.Entry entry = it.next(); + for (final Entry entry : streamMap.entrySet()) { final H2Stream stream = entry.getValue(); try { updateInputWindow(stream.getId(), stream.getInputWindow(), delta); diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2PrefaceHandler.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2PrefaceHandler.java index 35c80c20a5..1efbc9c3be 100644 --- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2PrefaceHandler.java +++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerH2PrefaceHandler.java @@ -85,8 +85,8 @@ public void inputReady(final IOSession session, final ByteBuffer src) throws IOE } final ByteBuffer data = inBuf.data(); if (data.remaining() >= PREFACE.length) { - for (int i = 0; i < PREFACE.length; i++) { - if (data.get() != PREFACE[i]) { + for (final byte element : PREFACE) { + if (data.get() != element) { throw new ProtocolNegotiationException("Unexpected HTTP/2 preface"); } } diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/nio/support/BasicPingHandler.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/nio/support/BasicPingHandler.java index 9a52223d43..fc4fb83dce 100644 --- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/nio/support/BasicPingHandler.java +++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/nio/support/BasicPingHandler.java @@ -57,8 +57,8 @@ public ByteBuffer getData() { @Override public void consumeResponse(final ByteBuffer feedback) throws HttpException, IOException { boolean result = true; - for (int i = 0; i < PING_MESSAGE.length; i++) { - if (!feedback.hasRemaining() || PING_MESSAGE[i] != feedback.get()) { + for (final byte element : PING_MESSAGE) { + if (!feedback.hasRemaining() || element != feedback.get()) { result = false; break; } diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2ConscriptRequestExecutionExample.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2ConscriptRequestExecutionExample.java index e33f4e248a..2296fa7344 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2ConscriptRequestExecutionExample.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2ConscriptRequestExecutionExample.java @@ -77,16 +77,12 @@ public static void main(final String[] args) throws Exception { @Override public void onHeaderInput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header)); } @Override public void onHeaderOutput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header)); } @Override diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FileServerExample.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FileServerExample.java index 91bfc10cef..719da07acb 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FileServerExample.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FileServerExample.java @@ -94,16 +94,12 @@ public static void main(final String[] args) throws Exception { @Override public void onHeaderInput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header)); } @Override public void onHeaderOutput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header)); } @Override diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FullDuplexClientExample.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FullDuplexClientExample.java index c752175093..8d716cf401 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FullDuplexClientExample.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FullDuplexClientExample.java @@ -82,16 +82,12 @@ public static void main(final String[] args) throws Exception { @Override public void onHeaderInput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header)); } @Override public void onHeaderOutput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header)); } @Override diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FullDuplexServerExample.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FullDuplexServerExample.java index d09f09b8c6..68448c89f8 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FullDuplexServerExample.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2FullDuplexServerExample.java @@ -88,16 +88,12 @@ public static void main(final String[] args) throws Exception { @Override public void onHeaderInput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header)); } @Override public void onHeaderOutput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header)); } @Override diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2MultiStreamExecutionExample.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2MultiStreamExecutionExample.java index 2628387f3e..b1a5299632 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2MultiStreamExecutionExample.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2MultiStreamExecutionExample.java @@ -76,16 +76,12 @@ public static void main(final String[] args) throws Exception { @Override public void onHeaderInput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header)); } @Override public void onHeaderOutput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header)); } @Override diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2RequestExecutionExample.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2RequestExecutionExample.java index 3653baeebe..59dd413557 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2RequestExecutionExample.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2RequestExecutionExample.java @@ -68,16 +68,12 @@ public static void main(final String[] args) throws Exception { @Override public void onHeaderInput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header)); } @Override public void onHeaderOutput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header)); } @Override diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2TlsAlpnRequestExecutionExample.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2TlsAlpnRequestExecutionExample.java index 5a67e09658..aa57a76c6d 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2TlsAlpnRequestExecutionExample.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2TlsAlpnRequestExecutionExample.java @@ -77,16 +77,12 @@ public static void main(final String[] args) throws Exception { @Override public void onHeaderInput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header)); } @Override public void onHeaderOutput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header)); } @Override diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2ViaHttp1ProxyExecutionExample.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2ViaHttp1ProxyExecutionExample.java index 6ebebc7326..fb5ac56440 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2ViaHttp1ProxyExecutionExample.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/examples/H2ViaHttp1ProxyExecutionExample.java @@ -103,16 +103,12 @@ public void onExchangeComplete(final HttpConnection connection, final boolean ke @Override public void onHeaderInput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") << " + header)); } @Override public void onHeaderOutput(final HttpConnection connection, final int streamId, final List headers) { - for (int i = 0; i < headers.size(); i++) { - System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + headers.get(i)); - } + headers.forEach(header -> System.out.println(connection.getRemoteAddress() + " (" + streamId + ") >> " + header)); } @Override diff --git a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/hpack/TestHPackCoding.java b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/hpack/TestHPackCoding.java index dba042bd29..c9c3ed2dc7 100644 --- a/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/hpack/TestHPackCoding.java +++ b/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/hpack/TestHPackCoding.java @@ -254,14 +254,16 @@ void testEnsureCapacity() throws Exception { }; private static String constructHelloString(final int[] raw, final int n) { + final StringBuilder str = new StringBuilder(raw.length); + for (final int element : raw) { + str.append((char) element); + } final StringBuilder buffer = new StringBuilder(); for (int j = 0; j < n; j++) { if (j > 0) { buffer.append("; "); } - for (int i = 0; i < raw.length; i++) { - buffer.append((char) raw[i]); - } + buffer.append(str); } return buffer.toString(); } diff --git a/httpcore5-reactive/src/test/java/org/apache/hc/core5/reactive/examples/ReactiveFullDuplexServerExample.java b/httpcore5-reactive/src/test/java/org/apache/hc/core5/reactive/examples/ReactiveFullDuplexServerExample.java index a2080d7f14..69480d60c6 100644 --- a/httpcore5-reactive/src/test/java/org/apache/hc/core5/reactive/examples/ReactiveFullDuplexServerExample.java +++ b/httpcore5-reactive/src/test/java/org/apache/hc/core5/reactive/examples/ReactiveFullDuplexServerExample.java @@ -68,7 +68,7 @@ public static void main(final String[] args) throws Exception { .build(); final HttpAsyncServer server = AsyncServerBootstrap.bootstrap() - .setExceptionCallback(e -> e.printStackTrace()) + .setExceptionCallback(Exception::printStackTrace) .setIOReactorConfig(config) .setStreamListener(new Http1StreamListener() { @Override diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java index 5cda8b208d..ea3d03162d 100644 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java +++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/benchmark/HttpBenchmark.java @@ -33,6 +33,7 @@ import java.nio.ByteBuffer; import java.nio.channels.ByteChannel; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -456,28 +457,25 @@ private Results doExecute(final HttpAsyncRequester requester, final Stats stats) final HttpVersion version = HttpVersion.HTTP_1_1; final CountDownLatch completionLatch = new CountDownLatch(config.getConcurrencyLevel()); - final BenchmarkWorker[] workers = new BenchmarkWorker[config.getConcurrencyLevel()]; - for (int i = 0; i < workers.length; i++) { + final List workers = new ArrayList<>(config.getConcurrencyLevel()); + for (int i = 0; i < config.getConcurrencyLevel(); i++) { final HttpCoreContext context = HttpCoreContext.create(); context.setProtocolVersion(version); - final BenchmarkWorker worker = new BenchmarkWorker( + workers.add(new BenchmarkWorker( requester, host, context, requestCount, completionLatch, stats, - config); - workers[i] = worker; + config)); } final long deadline = config.getTimeLimit() != null ? config.getTimeLimit().toMilliseconds() : Long.MAX_VALUE; final long startTime = System.currentTimeMillis(); - for (int i = 0; i < workers.length; i++) { - workers[i].execute(); - } + workers.forEach(BenchmarkWorker::execute); completionLatch.await(deadline, TimeUnit.MILLISECONDS); @@ -487,9 +485,7 @@ private Results doExecute(final HttpAsyncRequester requester, final Stats stats) final long endTime = System.currentTimeMillis(); - for (int i = 0; i < workers.length; i++) { - workers[i].releaseResources(); - } + workers.forEach(BenchmarkWorker::releaseResources); return new Results( stats.getServerName(), diff --git a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingH2StreamListener.java b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingH2StreamListener.java index 8ef7d720af..bfd0736aa3 100644 --- a/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingH2StreamListener.java +++ b/httpcore5-testing/src/main/java/org/apache/hc/core5/testing/nio/LoggingH2StreamListener.java @@ -85,9 +85,7 @@ private void logFlowControl(final String prefix, final int streamId, final int d public void onHeaderInput(final HttpConnection connection, final int streamId, final List headers) { if (headerLog.isDebugEnabled()) { final String prefix = LoggingSupport.getId(connection); - for (int i = 0; i < headers.size(); i++) { - headerLog.debug("{} << {}", prefix, headers.get(i)); - } + headers.forEach(header -> headerLog.debug("{} << {}", prefix, header)); } } @@ -95,9 +93,7 @@ public void onHeaderInput(final HttpConnection connection, final int streamId, f public void onHeaderOutput(final HttpConnection connection, final int streamId, final List headers) { if (headerLog.isDebugEnabled()) { final String prefix = LoggingSupport.getId(connection); - for (int i = 0; i < headers.size(); i++) { - headerLog.debug("{} >> {}", prefix, headers.get(i)); - } + headers.forEach(header -> headerLog.debug("{} >> {}", prefix, header)); } } diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java index 6daf502959..f59d83e5f5 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/classic/ClassicTLSIntegrationTest.java @@ -63,7 +63,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.AfterEachCallback; -import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.RegisterExtension; class ClassicTLSIntegrationTest { @@ -73,35 +72,25 @@ class ClassicTLSIntegrationTest { private HttpServer server; @RegisterExtension - public final AfterEachCallback serverCleanup = new AfterEachCallback() { - - @Override - public void afterEach(final ExtensionContext context) throws Exception { - if (server != null) { - try { - server.close(CloseMode.IMMEDIATE); - } catch (final Exception ignore) { - } + public final AfterEachCallback serverCleanup = context -> { + if (server != null) { + try { + server.close(CloseMode.IMMEDIATE); + } catch (final Exception ignore) { } } - }; private HttpRequester requester; @RegisterExtension - public final AfterEachCallback clientCleanup = new AfterEachCallback() { - - @Override - public void afterEach(final ExtensionContext context) throws Exception { - if (requester != null) { - try { - requester.close(CloseMode.GRACEFUL); - } catch (final Exception ignore) { - } + public final AfterEachCallback clientCleanup = context -> { + if (requester != null) { + try { + requester.close(CloseMode.GRACEFUL); + } catch (final Exception ignore) { } } - }; @Test diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java index f635786f57..5c67d2b1d6 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2IntegrationTest.java @@ -59,7 +59,6 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; -import org.apache.hc.core5.function.Supplier; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.EndpointDetails; import org.apache.hc.core5.http.EntityDetails; @@ -584,47 +583,40 @@ void testPushRefused() throws Exception { final H2TestClient client = resources.client(); final BlockingQueue pushResultQueue = new LinkedBlockingDeque<>(); - server.register("/hello", new Supplier() { + server.register("/hello", () -> new MessageExchangeHandler(new DiscardingEntityConsumer<>()) { @Override - public AsyncServerExchangeHandler get() { - return new MessageExchangeHandler(new DiscardingEntityConsumer<>()) { + protected void handle( + final Message request, + final AsyncServerRequestHandler.ResponseTrigger responseTrigger, + final HttpContext context) throws IOException, HttpException { - @Override - protected void handle( - final Message request, - final AsyncServerRequestHandler.ResponseTrigger responseTrigger, - final HttpContext context) throws IOException, HttpException { - - responseTrigger.pushPromise( - new BasicHttpRequest(Method.GET, new HttpHost(scheme.id, "localhost"), "/stuff"), - context, new BasicPushProducer(AsyncEntityProducers.create("Pushing all sorts of stuff")) { - - @Override - public void failed(final Exception cause) { - pushResultQueue.add(cause); - super.failed(cause); - } + responseTrigger.pushPromise( + new BasicHttpRequest(Method.GET, new HttpHost(scheme.id, "localhost"), "/stuff"), + context, new BasicPushProducer(AsyncEntityProducers.create("Pushing all sorts of stuff")) { - }); - responseTrigger.pushPromise( - new BasicHttpRequest(Method.GET, new HttpHost(scheme.id, "localhost"), "/more-stuff"), - context, new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 500)) { + @Override + public void failed(final Exception cause) { + pushResultQueue.add(cause); + super.failed(cause); + } - @Override - public void failed(final Exception cause) { - pushResultQueue.add(cause); - super.failed(cause); - } + }); + responseTrigger.pushPromise( + new BasicHttpRequest(Method.GET, new HttpHost(scheme.id, "localhost"), "/more-stuff"), + context, new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 500)) { - }); - responseTrigger.submitResponse( - new BasicResponseProducer(HttpStatus.SC_OK, AsyncEntityProducers.create("Hi there")), - context); + @Override + public void failed(final Exception cause) { + pushResultQueue.add(cause); + super.failed(cause); } - }; - } + }); + responseTrigger.submitResponse( + new BasicResponseProducer(HttpStatus.SC_OK, AsyncEntityProducers.create("Hi there")), + context); + } }); final InetSocketAddress serverEndpoint = server.start(); @@ -773,66 +765,59 @@ void testPrematureResponse() throws Exception { final H2TestServer server = resources.server(); final H2TestClient client = resources.client(); - server.register("*", new Supplier() { + server.register("*", () -> new AsyncServerExchangeHandler() { - @Override - public AsyncServerExchangeHandler get() { - return new AsyncServerExchangeHandler() { - - private final AtomicReference responseProducer = new AtomicReference<>(); - - @Override - public void updateCapacity(final CapacityChannel capacityChannel) throws IOException { - capacityChannel.update(Integer.MAX_VALUE); - } + private final AtomicReference responseProducer = new AtomicReference<>(); - @Override - public void consume(final ByteBuffer src) throws IOException { - } + @Override + public void updateCapacity(final CapacityChannel capacityChannel) throws IOException { + capacityChannel.update(Integer.MAX_VALUE); + } - @Override - public void streamEnd(final List trailers) throws HttpException, IOException { - } + @Override + public void consume(final ByteBuffer src) throws IOException { + } - @Override - public void handleRequest( - final HttpRequest request, - final EntityDetails entityDetails, - final ResponseChannel responseChannel, - final HttpContext context) throws HttpException, IOException { - final AsyncResponseProducer producer; - final Header h = request.getFirstHeader("password"); - if (h != null && "secret".equals(h.getValue())) { - producer = new BasicResponseProducer(HttpStatus.SC_OK, "All is well"); - } else { - producer = new BasicResponseProducer(HttpStatus.SC_UNAUTHORIZED, "You shall not pass"); - } - responseProducer.set(producer); - producer.sendResponse(responseChannel, context); - } + @Override + public void streamEnd(final List trailers) throws HttpException, IOException { + } - @Override - public int available() { - final AsyncResponseProducer producer = this.responseProducer.get(); - return producer.available(); - } + @Override + public void handleRequest( + final HttpRequest request, + final EntityDetails entityDetails, + final ResponseChannel responseChannel, + final HttpContext context) throws HttpException, IOException { + final AsyncResponseProducer producer; + final Header h = request.getFirstHeader("password"); + if (h != null && "secret".equals(h.getValue())) { + producer = new BasicResponseProducer(HttpStatus.SC_OK, "All is well"); + } else { + producer = new BasicResponseProducer(HttpStatus.SC_UNAUTHORIZED, "You shall not pass"); + } + responseProducer.set(producer); + producer.sendResponse(responseChannel, context); + } - @Override - public void produce(final DataStreamChannel channel) throws IOException { - final AsyncResponseProducer producer = this.responseProducer.get(); - producer.produce(channel); - } + @Override + public int available() { + final AsyncResponseProducer producer = this.responseProducer.get(); + return producer.available(); + } - @Override - public void failed(final Exception cause) { - } + @Override + public void produce(final DataStreamChannel channel) throws IOException { + final AsyncResponseProducer producer = this.responseProducer.get(); + producer.produce(channel); + } - @Override - public void releaseResources() { - } - }; + @Override + public void failed(final Exception cause) { } + @Override + public void releaseResources() { + } }); final InetSocketAddress serverEndpoint = server.start(); diff --git a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java index e0acd9685d..487c16cdc2 100644 --- a/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java +++ b/httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/TLSIntegrationTest.java @@ -103,35 +103,25 @@ class TLSIntegrationTest { private HttpAsyncServer server; @RegisterExtension - public final AfterEachCallback serverCleanup = new AfterEachCallback() { - - @Override - public void afterEach(final ExtensionContext context) throws Exception { - if (server != null) { - try { - server.close(CloseMode.IMMEDIATE); - } catch (final Exception ignore) { - } + public final AfterEachCallback serverCleanup = context -> { + if (server != null) { + try { + server.close(CloseMode.IMMEDIATE); + } catch (final Exception ignore) { } } - }; private HttpAsyncRequester client; @RegisterExtension - public final AfterEachCallback clientCleanup = new AfterEachCallback() { - - @Override - public void afterEach(final ExtensionContext context) throws Exception { - if (client != null) { - try { - client.close(CloseMode.GRACEFUL); - } catch (final Exception ignore) { - } + public final AfterEachCallback clientCleanup = context -> { + if (client != null) { + try { + client.close(CloseMode.GRACEFUL); + } catch (final Exception ignore) { } } - }; HttpAsyncServer createServer(final TlsStrategy tlsStrategy) { diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedOutputStream.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedOutputStream.java index b7d9faf4cc..347def627b 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedOutputStream.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/io/ChunkedOutputStream.java @@ -159,8 +159,7 @@ private void writeClosingChunk() throws IOException { private void writeTrailers() throws IOException { final List trailers = this.trailerSupplier != null ? this.trailerSupplier.get() : null; if (trailers != null) { - for (int i = 0; i < trailers.size(); i++) { - final Header header = trailers.get(i); + for (final Header header : trailers) { if (header instanceof FormattedHeader) { final CharArrayBuffer chbuffer = ((FormattedHeader) header).getBuffer(); this.buffer.writeLine(chbuffer, this.outputStream); diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ChunkEncoder.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ChunkEncoder.java index 223f6fbc79..94456b0e7e 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ChunkEncoder.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ChunkEncoder.java @@ -137,8 +137,7 @@ public void complete(final List trailers) throws IOException { private void writeTrailers(final List trailers) throws IOException { if (trailers != null) { - for (int i = 0; i < trailers.size(); i++) { - final Header header = trailers.get(i); + for (final Header header : trailers) { if (header instanceof FormattedHeader) { final CharArrayBuffer chbuffer = ((FormattedHeader) header).getBuffer(); buffer.writeLine(chbuffer); diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/routing/PathRoute.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/routing/PathRoute.java index b07086a627..d95e09e75b 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/routing/PathRoute.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/routing/PathRoute.java @@ -44,8 +44,12 @@ public PathRoute(final T pattern, final H handler) { @Override public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } final PathRoute other = (PathRoute) o; return Objects.equals(pattern, other.pattern); } diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java b/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java index 072ea7ca57..4793ab09be 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/message/HeaderGroup.java @@ -234,8 +234,7 @@ public Header[] getHeaders(final String name) { */ @Override public Header getFirstHeader(final String name) { - for (int i = 0; i < this.headers.size(); i++) { - final Header header = this.headers.get(i); + for (final Header header : this.headers) { if (header.getName().equalsIgnoreCase(name)) { return header; } @@ -256,8 +255,7 @@ public Header getFirstHeader(final String name) { public Header getHeader(final String name) throws ProtocolException { int count = 0; Header singleHeader = null; - for (int i = 0; i < this.headers.size(); i++) { - final Header header = this.headers.get(i); + for (final Header header : this.headers) { if (header.getName().equalsIgnoreCase(name)) { singleHeader = header; count++; @@ -311,8 +309,7 @@ public Header[] getHeaders() { */ @Override public boolean containsHeader(final String name) { - for (int i = 0; i < this.headers.size(); i++) { - final Header header = this.headers.get(i); + for (final Header header : this.headers) { if (header.getName().equalsIgnoreCase(name)) { return true; } @@ -331,8 +328,7 @@ public boolean containsHeader(final String name) { @Override public int countHeaders(final String name) { int count = 0; - for (int i = 0; i < this.headers.size(); i++) { - final Header header = this.headers.get(i); + for (final Header header : this.headers) { if (header.getName().equalsIgnoreCase(name)) { count++; } diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/HttpDateGenerator.java b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/HttpDateGenerator.java index 469dd17a4d..d168ad79f6 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/HttpDateGenerator.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/http/protocol/HttpDateGenerator.java @@ -69,7 +69,7 @@ public class HttpDateGenerator { private final DateTimeFormatter dateTimeFormatter; private long dateAsMillis; private String dateAsText; - private ZoneId zoneId; + private final ZoneId zoneId; private final ReentrantLock lock; diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java index b680ae63df..7aaad6c9d2 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/LaxConnPool.java @@ -122,10 +122,7 @@ public boolean isShutdown() { @Override public void close(final CloseMode closeMode) { if (isShutDown.compareAndSet(false, true)) { - for (final Iterator> it = routeToPool.values().iterator(); it.hasNext(); ) { - final PerRoutePool routePool = it.next(); - routePool.shutdown(closeMode); - } + routeToPool.values().forEach(routePool -> routePool.shutdown(closeMode)); routeToPool.clear(); } } diff --git a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java index 819238b38b..f3323b6cd4 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/pool/StrictConnPool.java @@ -609,11 +609,7 @@ public void enumAvailable(final Callback> callback) { public void enumLeased(final Callback> callback) { this.lock.lock(); try { - final Iterator> it = this.leased.iterator(); - while (it.hasNext()) { - final PoolEntry entry = it.next(); - callback.execute(entry); - } + leased.forEach(callback::execute); processPendingRequests(); } finally { this.lock.unlock(); diff --git a/httpcore5/src/main/java/org/apache/hc/core5/reactor/MultiCoreIOReactor.java b/httpcore5/src/main/java/org/apache/hc/core5/reactor/MultiCoreIOReactor.java index 957a772eb4..7b6cff470b 100644 --- a/httpcore5/src/main/java/org/apache/hc/core5/reactor/MultiCoreIOReactor.java +++ b/httpcore5/src/main/java/org/apache/hc/core5/reactor/MultiCoreIOReactor.java @@ -30,6 +30,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Stream; import org.apache.hc.core5.io.CloseMode; import org.apache.hc.core5.io.Closer; @@ -68,8 +69,8 @@ public IOReactorStatus getStatus() { */ public final void start() { if (this.status.compareAndSet(IOReactorStatus.INACTIVE, IOReactorStatus.ACTIVE)) { - for (int i = 0; i < this.threads.length; i++) { - this.threads[i].start(); + for (final Thread thread : this.threads) { + thread.start(); } } } @@ -78,10 +79,7 @@ public final void start() { public final void initiateShutdown() { if (this.status.compareAndSet(IOReactorStatus.INACTIVE, IOReactorStatus.SHUT_DOWN) || this.status.compareAndSet(IOReactorStatus.ACTIVE, IOReactorStatus.SHUTTING_DOWN)) { - for (int i = 0; i < this.ioReactors.length; i++) { - final IOReactor ioReactor = this.ioReactors[i]; - ioReactor.initiateShutdown(); - } + Stream.of(ioReactors).forEach(IOReactor::initiateShutdown); } } @@ -90,8 +88,7 @@ public final void awaitShutdown(final TimeValue waitTime) throws InterruptedExce Args.notNull(waitTime, "Wait time"); final long deadline = System.currentTimeMillis() + waitTime.toMilliseconds(); long remaining = waitTime.toMilliseconds(); - for (int i = 0; i < this.ioReactors.length; i++) { - final IOReactor ioReactor = this.ioReactors[i]; + for (final IOReactor ioReactor : this.ioReactors) { if (ioReactor.getStatus().compareTo(IOReactorStatus.SHUT_DOWN) < 0) { ioReactor.awaitShutdown(TimeValue.of(remaining, TimeUnit.MILLISECONDS)); remaining = deadline - System.currentTimeMillis(); @@ -100,8 +97,7 @@ public final void awaitShutdown(final TimeValue waitTime) throws InterruptedExce } } } - for (int i = 0; i < this.threads.length; i++) { - final Thread thread = this.threads[i]; + for (final Thread thread : this.threads) { thread.join(remaining); remaining = deadline - System.currentTimeMillis(); if (remaining <= 0) { @@ -137,12 +133,8 @@ public void close(final CloseMode closeMode, final Timeout timeout) { } this.status.set(IOReactorStatus.SHUT_DOWN); if (this.terminated.compareAndSet(false, true)) { - for (int i = 0; i < this.ioReactors.length; i++) { - Closer.close(this.ioReactors[i], CloseMode.IMMEDIATE); - } - for (int i = 0; i < this.threads.length; i++) { - this.threads[i].interrupt(); - } + Stream.of(ioReactors).forEach(e -> Closer.close(e, CloseMode.IMMEDIATE)); + Stream.of(threads).forEach(Thread::interrupt); } } diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncFileServerExample.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncFileServerExample.java index 82c85af000..cd8cec22ee 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncFileServerExample.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncFileServerExample.java @@ -86,7 +86,7 @@ public static void main(final String[] args) throws Exception { .build(); final HttpAsyncServer server = AsyncServerBootstrap.bootstrap() - .setExceptionCallback(e -> e.printStackTrace()) + .setExceptionCallback(Exception::printStackTrace) .setIOReactorConfig(config) .register("*", new AsyncServerRequestHandler>() { diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncFullDuplexServerExample.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncFullDuplexServerExample.java index b3c8452449..c4dafa7e8f 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncFullDuplexServerExample.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncFullDuplexServerExample.java @@ -75,7 +75,7 @@ public static void main(final String[] args) throws Exception { .build(); final HttpAsyncServer server = AsyncServerBootstrap.bootstrap() - .setExceptionCallback(e -> e.printStackTrace()) + .setExceptionCallback(Exception::printStackTrace) .setIOReactorConfig(config) .setStreamListener(new Http1StreamListener() { diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java index c1576b1713..15d5a0185e 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java @@ -163,7 +163,7 @@ public void onExchangeComplete(final HttpConnection connection, final boolean ke .create(); final HttpAsyncServer server = AsyncServerBootstrap.bootstrap() - .setExceptionCallback(e -> e.printStackTrace()) + .setExceptionCallback(Exception::printStackTrace) .setIOReactorConfig(config) .setStreamListener(new Http1StreamListener() { diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java index a89f382143..c812dacb14 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncServerFilterExample.java @@ -76,7 +76,7 @@ public static void main(final String[] args) throws Exception { .build(); final HttpAsyncServer server = AsyncServerBootstrap.bootstrap() - .setExceptionCallback(e -> e.printStackTrace()) + .setExceptionCallback(Exception::printStackTrace) .setIOReactorConfig(config) // Replace standard expect-continue handling with a custom auth filter diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java index ab7de44b14..dad78bd7d2 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicGetExecutionExample.java @@ -85,8 +85,7 @@ public void onExchangeComplete(final HttpConnection connection, final boolean ke final HttpHost target = new HttpHost("httpbin.org"); final String[] requestUris = new String[] {"/", "/ip", "/user-agent", "/headers"}; - for (int i = 0; i < requestUris.length; i++) { - final String requestUri = requestUris[i]; + for (final String requestUri : requestUris) { final ClassicHttpRequest request = ClassicRequestBuilder.get() .setHttpHost(target) .setPath(requestUri) diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java index 49644d1042..168c7885b5 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/ClassicPostExecutionExample.java @@ -105,12 +105,12 @@ public void onExchangeComplete(final HttpConnection connection, final boolean ke }; final String requestUri = "/post"; - for (int i = 0; i < requestBodies.length; i++) { + for (final HttpEntity element : requestBodies) { final ClassicHttpRequest request = ClassicRequestBuilder.get() .setHttpHost(target) .setPath(requestUri) .build(); - request.setEntity(requestBodies[i]); + request.setEntity(element); try (ClassicHttpResponse response = httpRequester.execute(target, request, Timeout.ofSeconds(5), context)) { System.out.println(requestUri + "->" + response.getCode()); System.out.println(EntityUtils.toString(response.getEntity())); diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/PrintVersionInfo.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/PrintVersionInfo.java index 85f1a6e1e8..0f9175c0fd 100644 --- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/PrintVersionInfo.java +++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/PrintVersionInfo.java @@ -27,14 +27,14 @@ package org.apache.hc.core5.http.examples; +import java.util.stream.Stream; + import org.apache.hc.core5.util.VersionInfo; /** * Prints version information for debugging purposes. * This can be used to verify that the correct versions of the * HttpComponent JARs are picked up from the classpath. - * - * */ public class PrintVersionInfo { @@ -53,11 +53,10 @@ public class PrintVersionInfo { * a list of packages for which to get version info. */ public static void main(final String args[]) { - final String[] pckgs = (args.length > 0) ? args : MODULE_LIST; + final String[] pckgs = (args.length > 0) ? args : MODULE_LIST; VersionInfo[] via = VersionInfo.loadVersionInfo(pckgs, null); System.out.println("version info for thread context classloader:"); - for (int i=0; i