8315454: Add a way to create an immutable snapshot of a BitSet

Co-authored-by: Claes Redestad <redestad@openjdk.org>
Reviewed-by: redestad
This commit is contained in:
Per Minborg 2023-09-05 06:43:00 +00:00
parent 9def4538ab
commit f292268268
3 changed files with 193 additions and 13 deletions

View file

@ -32,7 +32,9 @@ import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException ;
import java.util.BitSet;
import java.util.Objects;
import java.util.function.IntPredicate;
import jdk.internal.util.ImmutableBitSetPredicate;
import jdk.internal.util.StaticProperty;
/**
@ -78,7 +80,7 @@ import jdk.internal.util.StaticProperty;
* @since 1.0
*/
public class URLEncoder {
private static final BitSet DONT_NEED_ENCODING;
private static final IntPredicate DONT_NEED_ENCODING;
private static final int CASE_DIFF = ('a' - 'A');
private static final String DEFAULT_ENCODING_NAME;
@ -120,17 +122,18 @@ public class URLEncoder {
*
*/
DONT_NEED_ENCODING = new BitSet(128);
DONT_NEED_ENCODING.set('a', 'z' + 1);
DONT_NEED_ENCODING.set('A', 'Z' + 1);
DONT_NEED_ENCODING.set('0', '9' + 1);
DONT_NEED_ENCODING.set(' '); /* encoding a space to a + is done
var bitSet = new BitSet(128);
bitSet.set('a', 'z' + 1);
bitSet.set('A', 'Z' + 1);
bitSet.set('0', '9' + 1);
bitSet.set(' '); /* encoding a space to a + is done
* in the encode() method */
DONT_NEED_ENCODING.set('-');
DONT_NEED_ENCODING.set('_');
DONT_NEED_ENCODING.set('.');
DONT_NEED_ENCODING.set('*');
bitSet.set('-');
bitSet.set('_');
bitSet.set('.');
bitSet.set('*');
DONT_NEED_ENCODING = ImmutableBitSetPredicate.of(bitSet);
DEFAULT_ENCODING_NAME = StaticProperty.fileEncoding();
}
@ -226,7 +229,7 @@ public class URLEncoder {
for (int i = 0; i < s.length();) {
int c = s.charAt(i);
//System.out.println("Examining character: " + c);
if (DONT_NEED_ENCODING.get(c)) {
if (DONT_NEED_ENCODING.test(c)) {
if (c == ' ') {
c = '+';
needToChange = true;
@ -269,7 +272,7 @@ public class URLEncoder {
}
}
i++;
} while (i < s.length() && !DONT_NEED_ENCODING.get((c = s.charAt(i))));
} while (i < s.length() && !DONT_NEED_ENCODING.test((c = s.charAt(i))));
charArrayWriter.flush();
String str = charArrayWriter.toString();