@ -1,5 +1,5 @@
@@ -1,5 +1,5 @@
/ *
* Copyright 2002 - 2020 the original author or authors .
* Copyright 2002 - 2021 the original author or authors .
*
* Licensed under the Apache License , Version 2 . 0 ( the "License" ) ;
* you may not use this file except in compliance with the License .
@ -20,7 +20,6 @@ import java.util.AbstractSet;
@@ -20,7 +20,6 @@ import java.util.AbstractSet;
import java.util.Collection ;
import java.util.Collections ;
import java.util.Enumeration ;
import java.util.HashSet ;
import java.util.Iterator ;
import java.util.List ;
import java.util.Map ;
@ -167,12 +166,7 @@ class TomcatHeadersAdapter implements MultiValueMap<String, String> {
@@ -167,12 +166,7 @@ class TomcatHeadersAdapter implements MultiValueMap<String, String> {
@Override
public Set < String > keySet ( ) {
Set < String > result = new HashSet < > ( 8 ) ;
Enumeration < String > names = this . headers . names ( ) ;
while ( names . hasMoreElements ( ) ) {
result . add ( names . nextElement ( ) ) ;
}
return result ;
return new HeaderNames ( ) ;
}
@Override
@ -247,4 +241,59 @@ class TomcatHeadersAdapter implements MultiValueMap<String, String> {
@@ -247,4 +241,59 @@ class TomcatHeadersAdapter implements MultiValueMap<String, String> {
}
}
private class HeaderNames extends AbstractSet < String > {
@Override
public Iterator < String > iterator ( ) {
return new HeaderNamesIterator ( headers . names ( ) ) ;
}
@Override
public int size ( ) {
Enumeration < String > names = headers . names ( ) ;
int size = 0 ;
while ( names . hasMoreElements ( ) ) {
names . nextElement ( ) ;
size + + ;
}
return size ;
}
}
private final class HeaderNamesIterator implements Iterator < String > {
private final Enumeration < String > enumeration ;
@Nullable
private String currentName ;
private HeaderNamesIterator ( Enumeration < String > enumeration ) {
this . enumeration = enumeration ;
}
@Override
public boolean hasNext ( ) {
return this . enumeration . hasMoreElements ( ) ;
}
@Override
public String next ( ) {
this . currentName = this . enumeration . nextElement ( ) ;
return this . currentName ;
}
@Override
public void remove ( ) {
if ( this . currentName = = null ) {
throw new IllegalStateException ( "No current Header in iterator" ) ;
}
int index = headers . findHeader ( this . currentName , 0 ) ;
if ( index = = - 1 ) {
throw new IllegalStateException ( "Header not present: " + this . currentName ) ;
}
headers . removeHeader ( index ) ;
}
}
}