From ec5fb7dc9afee8221bffc53ebea549c4db507354 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 20 Jul 2024 13:10:15 +0530 Subject: [PATCH 1/2] Use TreeMap in Response --- .../extractor/downloader/Response.java | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java index ac792dc756..7bc43bb2cb 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.TreeMap; /** * A Data class used to hold the results from requests made by the Downloader implementation. @@ -14,17 +15,18 @@ public class Response { private final String responseMessage; private final Map> responseHeaders; private final String responseBody; - private final String latestUrl; public Response(final int responseCode, final String responseMessage, - @Nullable final Map> responseHeaders, + @Nonnull final Map> responseHeaders, @Nullable final String responseBody, @Nullable final String latestUrl) { this.responseCode = responseCode; this.responseMessage = responseMessage; - this.responseHeaders = responseHeaders == null ? Collections.emptyMap() : responseHeaders; + + this.responseHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + this.responseHeaders.putAll(responseHeaders); this.responseBody = responseBody == null ? "" : responseBody; this.latestUrl = latestUrl; @@ -71,13 +73,8 @@ public String latestUrl() { */ @Nullable public String getHeader(final String name) { - for (final Map.Entry> headerEntry : responseHeaders.entrySet()) { - final String key = headerEntry.getKey(); - if (key != null && key.equalsIgnoreCase(name) && !headerEntry.getValue().isEmpty()) { - return headerEntry.getValue().get(0); - } - } - - return null; + // Header lookup is case-insensitive + final var values = responseHeaders.getOrDefault(name, Collections.emptyList()); + return values.isEmpty() ? null : values.get(0); } } From 7fb4dbefecb26870771a7a467d1d22cbac24f366 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 20 Jul 2024 16:55:56 +0530 Subject: [PATCH 2/2] Reuse map if it is a SortedMap instead of creating a new one --- .../schabi/newpipe/extractor/downloader/Response.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java index 7bc43bb2cb..c95b86cad4 100644 --- a/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java +++ b/extractor/src/main/java/org/schabi/newpipe/extractor/downloader/Response.java @@ -5,6 +5,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.SortedMap; import java.util.TreeMap; /** @@ -13,7 +14,7 @@ public class Response { private final int responseCode; private final String responseMessage; - private final Map> responseHeaders; + private final SortedMap> responseHeaders; private final String responseBody; private final String latestUrl; @@ -25,8 +26,12 @@ public Response(final int responseCode, this.responseCode = responseCode; this.responseMessage = responseMessage; - this.responseHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); - this.responseHeaders.putAll(responseHeaders); + if (responseHeaders instanceof SortedMap) { + this.responseHeaders = (SortedMap>) responseHeaders; + } else { + this.responseHeaders = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); + this.responseHeaders.putAll(responseHeaders); + } this.responseBody = responseBody == null ? "" : responseBody; this.latestUrl = latestUrl;