8245309: Re-examine use of ThreadLocalCoders in sun.net.www.ParseUtil

Reviewed-by: shade, dfuchs, alanb, chegar
This commit is contained in:
Julia Boes 2020-09-16 10:49:13 +00:00
parent 60c4902f61
commit e0cf023263
2 changed files with 87 additions and 9 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -34,10 +34,10 @@ import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CodingErrorAction;
import sun.nio.cs.ThreadLocalCoders;
import sun.nio.cs.UTF_8;
/**
@ -177,9 +177,9 @@ public final class ParseUtil {
StringBuilder sb = new StringBuilder(n);
ByteBuffer bb = ByteBuffer.allocate(n);
CharBuffer cb = CharBuffer.allocate(n);
CharsetDecoder dec = ThreadLocalCoders.decoderFor(UTF_8.INSTANCE)
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
CharsetDecoder dec = UTF_8.INSTANCE.newDecoder()
.onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT);
char c = s.charAt(0);
for (int i = 0; i < n;) {
@ -451,6 +451,7 @@ public final class ParseUtil {
private static String quote(String s, long lowMask, long highMask) {
int n = s.length();
StringBuilder sb = null;
CharsetEncoder encoder = null;
boolean allowNonASCII = ((lowMask & L_ESCAPED) != 0);
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
@ -468,11 +469,14 @@ public final class ParseUtil {
} else if (allowNonASCII
&& (Character.isSpaceChar(c)
|| Character.isISOControl(c))) {
if (encoder == null) {
encoder = UTF_8.INSTANCE.newEncoder();
}
if (sb == null) {
sb = new StringBuilder();
sb.append(s, 0, i);
}
appendEncoded(sb, c);
appendEncoded(encoder, sb, c);
} else {
if (sb != null)
sb.append(c);
@ -494,11 +498,11 @@ public final class ParseUtil {
&& match(s.charAt(pos + 2), L_HEX, H_HEX);
}
private static void appendEncoded(StringBuilder sb, char c) {
private static void appendEncoded(CharsetEncoder encoder,
StringBuilder sb, char c) {
ByteBuffer bb = null;
try {
bb = ThreadLocalCoders.encoderFor(UTF_8.INSTANCE)
.encode(CharBuffer.wrap("" + c));
bb = encoder.encode(CharBuffer.wrap("" + c));
} catch (CharacterCodingException x) {
assert false;
}