private static String toUppercaseAscii(String s) { char[] source = s.toCharArray(); char[] result = new char[source.length]; for (int i = 0; i < source.length; i++) { result[i] = toUppercaseAscii(source[i]); } return new String(result); }
private static String toUppercaseAscii(String s) { char[] source = s.toCharArray(); char[] result = new char[source.length]; for (int i = 0; i < source.length; i++) { result[i] = toUppercaseAscii(source[i]); } return new String(result); }
/** * Check if the given header name is present in this set of headers. * * @param headerName case-insensitive header name * @return true if the header is present, false otherwise. */ public boolean contains(String headerName) { return getUniqueHeaderNames().contains(toUppercaseAscii(headerName)); }
/** * Check if the given header name is present in this set of headers. * * @param headerName case-insensitive header name * @return true if the header is present, false otherwise. */ public boolean contains(String headerName) { return getUniqueHeaderNames().contains(toUppercaseAscii(headerName)); }
/** * Iterate over all entries in this set of headers. * <p> * The consumer is called for each value of a header. If a header has multiple values, each value is consumed * once, so the same header name (with the original case it was inserted with) may be consumed more than once. * * @param consumer accepts the header name and value */ public void forEach(BiConsumer<String, String> consumer) { class Index { private int value = -1; int increment() { return ++value; } } Map<String, Index> valueIndexByKey = new HashMap<>(); for (String headerName : headerNames) { String key = toUppercaseAscii(headerName); int index = valueIndexByKey.computeIfAbsent(key, k -> new Index()).increment(); consumer.accept(headerName, headersByCapitalizedName.get(key).values.get(index)); } }
/** * Iterate over all entries in this set of headers. * <p> * The consumer is called for each value of a header. If a header has multiple values, each value is consumed * once, so the same header name (with the original case it was inserted with) may be consumed more than once. * * @param consumer accepts the header name and value */ public void forEach(BiConsumer<String, String> consumer) { class Index { private int value = -1; int increment() { return ++value; } } Map<String, Index> valueIndexByKey = new HashMap<>(); for (String headerName : headerNames) { String key = toUppercaseAscii(headerName); int index = valueIndexByKey.computeIfAbsent(key, k -> new Index()).increment(); consumer.accept(headerName, headersByCapitalizedName.get(key).values.get(index)); } }
/** * Create a new set of headers, adding/replacing the provided headers into this instance. * <p> * Multi-valued headers present in both this and the provided headers are not merged. The provided headers * are guaranteed to be present and have the same values in the returned instance. * * @param headers to add or replace on this. * @return new set of headers containing both this instance's values as well as the provided values */ public RawHttpHeaders and(RawHttpHeaders headers) { Builder builder = RawHttpHeaders.newBuilderSkippingValidation(this); Set<String> visitedNames = new HashSet<>(headers.headerNames.size()); headers.forEach((name, value) -> { String key = toUppercaseAscii(name); boolean isNewKey = visitedNames.add(key); if (isNewKey) { builder.overwrite(name, value); } else { builder.with(name, value); } }); return builder.build(); }
/** * Create a new set of headers, adding/replacing the provided headers into this instance. * <p> * Multi-valued headers present in both this and the provided headers are not merged. The provided headers * are guaranteed to be present and have the same values in the returned instance. * * @param headers to add or replace on this. * @return new set of headers containing both this instance's values as well as the provided values */ public RawHttpHeaders and(RawHttpHeaders headers) { Builder builder = RawHttpHeaders.newBuilderSkippingValidation(this); Set<String> visitedNames = new HashSet<>(headers.headerNames.size()); headers.forEach((name, value) -> { String key = toUppercaseAscii(name); boolean isNewKey = visitedNames.add(key); if (isNewKey) { builder.overwrite(name, value); } else { builder.with(name, value); } }); return builder.build(); }