Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove uses of Charset name parsing #85795

Merged
merged 2 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions build-tools/src/main/java/org/elasticsearch/gradle/LoggedExec.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.function.Consumer;
Expand Down Expand Up @@ -95,13 +94,7 @@ public void setSpoolOutput(boolean spoolOutput) {
};
} else {
out = new ByteArrayOutputStream();
outputLogger = logger -> {
try {
logger.error(((ByteArrayOutputStream) out).toString("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
};
outputLogger = logger -> { logger.error(((ByteArrayOutputStream) out).toString(StandardCharsets.UTF_8)); };
}
setStandardOutput(out);
setErrorOutput(out);
Expand Down Expand Up @@ -134,13 +127,9 @@ private static <T extends BaseExecSpec> ExecResult genericExec(Function<Action<T
}
});
} catch (Exception e) {
try {
if (output.size() != 0) {
LOGGER.error("Exec output and error:");
NEWLINE.splitAsStream(output.toString("UTF-8")).forEach(s -> LOGGER.error("| " + s));
}
} catch (UnsupportedEncodingException ue) {
throw new GradleException("Failed to read exec output", ue);
if (output.size() != 0) {
LOGGER.error("Exec output and error:");
NEWLINE.splitAsStream(output.toString(StandardCharsets.UTF_8)).forEach(s -> LOGGER.error("| " + s));
}
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

package org.elasticsearch.ingest.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
Expand All @@ -24,11 +24,7 @@ public final class URLDecodeProcessor extends AbstractStringProcessor<String> {
}

public static String apply(String value) {
try {
return URLDecoder.decode(value, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("Could not URL-decode value.", e);
}
return URLDecoder.decode(value, StandardCharsets.UTF_8);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

package org.elasticsearch.ingest.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class URLDecodeProcessorTests extends AbstractStringProcessorTestCase<String> {
Expand All @@ -25,11 +25,7 @@ protected AbstractStringProcessor<String> newProcessor(String field, boolean ign

@Override
protected String expectedResult(String input) {
try {
return "Hello Günter" + URLDecoder.decode(input, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException("invalid");
}
return "Hello Günter" + URLDecoder.decode(input, StandardCharsets.UTF_8);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import org.elasticsearch.xcontent.json.JsonXContent;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -165,11 +165,7 @@ private static void addIndices(StringBuilder path, String[] indices) {
}

private static String encodeIndex(String s) {
try {
return URLEncoder.encode(s, "utf-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return URLEncoder.encode(s, StandardCharsets.UTF_8);
}

private static String sortToUri(SortBuilder<?> sort) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.elasticsearch.lucene.search.uhighlight.Snippet;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
Expand Down Expand Up @@ -119,38 +118,28 @@ public String toString() {

// Merge original annotations and search hits into a single set of markups for each passage
static MarkupPassage mergeAnnotations(AnnotationToken[] annotations, Passage passage) {
try {
MarkupPassage markupPassage = new MarkupPassage();

// Add search hits first - they take precedence over any other markup
for (int i = 0; i < passage.getNumMatches(); i++) {
int start = passage.getMatchStarts()[i];
int end = passage.getMatchEnds()[i];
String searchTerm = passage.getMatchTerms()[i].utf8ToString();
Markup markup = new Markup(
start,
end,
SEARCH_HIT_TYPE + "=" + URLEncoder.encode(searchTerm, StandardCharsets.UTF_8.name())
);
markupPassage.addUnlessOverlapping(markup);
}
MarkupPassage markupPassage = new MarkupPassage();

// Add search hits first - they take precedence over any other markup
for (int i = 0; i < passage.getNumMatches(); i++) {
int start = passage.getMatchStarts()[i];
int end = passage.getMatchEnds()[i];
String searchTerm = passage.getMatchTerms()[i].utf8ToString();
Markup markup = new Markup(start, end, SEARCH_HIT_TYPE + "=" + URLEncoder.encode(searchTerm, StandardCharsets.UTF_8));
markupPassage.addUnlessOverlapping(markup);
}

// Now add original text's annotations - ignoring any that might conflict with the search hits markup.
for (AnnotationToken token : annotations) {
int start = token.offset();
int end = token.endOffset();
if (start >= passage.getStartOffset() && end <= passage.getEndOffset()) {
String escapedValue = URLEncoder.encode(token.value(), StandardCharsets.UTF_8.name());
Markup markup = new Markup(start, end, escapedValue);
markupPassage.addUnlessOverlapping(markup);
}
// Now add original text's annotations - ignoring any that might conflict with the search hits markup.
for (AnnotationToken token : annotations) {
int start = token.offset();
int end = token.endOffset();
if (start >= passage.getStartOffset() && end <= passage.getEndOffset()) {
String escapedValue = URLEncoder.encode(token.value(), StandardCharsets.UTF_8);
Markup markup = new Markup(start, end, escapedValue);
markupPassage.addUnlessOverlapping(markup);
}
return markupPassage;

} catch (UnsupportedEncodingException e) {
// We should always have UTF-8 support
throw new IllegalStateException(e);
}
return markupPassage;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.Objects;

Expand Down Expand Up @@ -229,19 +229,11 @@ public RestStatus status() {
* @return the relative URI for the location of the document
*/
public String getLocation(@Nullable String routing) {
final String encodedIndex;
final String encodedType;
final String encodedId;
final String encodedRouting;
try {
// encode the path components separately otherwise the path separators will be encoded
encodedIndex = URLEncoder.encode(getIndex(), "UTF-8");
encodedType = URLEncoder.encode(MapperService.SINGLE_MAPPING_NAME, "UTF-8");
encodedId = URLEncoder.encode(getId(), "UTF-8");
encodedRouting = routing == null ? null : URLEncoder.encode(routing, "UTF-8");
} catch (final UnsupportedEncodingException e) {
throw new AssertionError(e);
}
// encode the path components separately otherwise the path separators will be encoded
final String encodedIndex = URLEncoder.encode(getIndex(), StandardCharsets.UTF_8);
final String encodedType = URLEncoder.encode(MapperService.SINGLE_MAPPING_NAME, StandardCharsets.UTF_8);
final String encodedId = URLEncoder.encode(getId(), StandardCharsets.UTF_8);
final String encodedRouting = routing == null ? null : URLEncoder.encode(routing, StandardCharsets.UTF_8);
final String routingStart = "?routing=";
final int bufferSizeExcludingRouting = 3 + encodedIndex.length() + encodedType.length() + encodedId.length();
final int bufferSize;
Expand Down
17 changes: 3 additions & 14 deletions server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;
Expand Down Expand Up @@ -387,21 +387,10 @@ static void init(final boolean foreground, final Path pidFile, final boolean qui
if (e instanceof CreationException) {
// guice: log the shortened exc to the log file
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream ps = null;
try {
ps = new PrintStream(os, false, "UTF-8");
} catch (UnsupportedEncodingException uee) {
assert false;
e.addSuppressed(uee);
}
PrintStream ps = new PrintStream(os, false, StandardCharsets.UTF_8);
new StartupException(e).printStackTrace(ps);
ps.flush();
try {
logger.error("Guice Exception: {}", os.toString("UTF-8"));
} catch (UnsupportedEncodingException uee) {
assert false;
e.addSuppressed(uee);
}
logger.error("Guice Exception: {}", os.toString(StandardCharsets.UTF_8));
} else if (e instanceof NodeValidationException) {
logger.error("node validation exception\n{}", e.getMessage());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.elasticsearch.xcontent.XContentType;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -249,7 +249,7 @@ public void testSerializeDynamicTemplates() throws Exception {
}
}

public void testToStringSizeLimit() throws UnsupportedEncodingException {
public void testToStringSizeLimit() {
IndexRequest request = new IndexRequest("index");

String source = "{\"name\":\"value\"}";
Expand All @@ -260,7 +260,7 @@ public void testToStringSizeLimit() throws UnsupportedEncodingException {
{"name":"%s"}
""".formatted(randomUnicodeOfLength(IndexRequest.MAX_SOURCE_LENGTH_IN_TOSTRING));
request.source(source, XContentType.JSON);
int actualBytes = source.getBytes("UTF-8").length;
int actualBytes = source.getBytes(StandardCharsets.UTF_8).length;
assertEquals(
"index {[index][null], source[n/a, actual length: ["
+ new ByteSizeValue(actualBytes).toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import org.elasticsearch.common.hash.MurmurHash3;
import org.elasticsearch.test.ESTestCase;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class MurmurHash3Tests extends ESTestCase {
public void testKnownValues() throws UnsupportedEncodingException {
public void testKnownValues() {
assertHash(0x629942693e10f867L, 0x92db0b82baeb5347L, "hell", 0);
assertHash(0xa78ddff5adae8d10L, 0x128900ef20900135L, "hello", 1);
assertHash(0x8a486b23f422e826L, 0xf962a2c58947765fL, "hello ", 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

public class WrapperQueryBuilderTests extends AbstractQueryTestCase<WrapperQueryBuilder> {

Expand Down Expand Up @@ -97,12 +97,7 @@ public void testFromJson() throws IOException {

WrapperQueryBuilder parsed = (WrapperQueryBuilder) parseQuery(json);
checkGeneratedJson(json, parsed);

try {
assertEquals(json, "{}", new String(parsed.source(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
assertEquals(json, "{}", new String(parsed.source(), StandardCharsets.UTF_8));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -92,8 +91,8 @@ public void addSecretInput(String input) {
}

/** Returns all output written to this terminal. */
public String getOutput() throws UnsupportedEncodingException {
return stdoutBuffer.toString("UTF-8");
public String getOutput() {
return stdoutBuffer.toString(StandardCharsets.UTF_8);
}

/** Returns all bytes written to this terminal. */
Expand All @@ -102,8 +101,8 @@ public byte[] getOutputBytes() {
}

/** Returns all output written to this terminal. */
public String getErrorOutput() throws UnsupportedEncodingException {
return stderrBuffer.toString("UTF-8");
public String getErrorOutput() {
return stderrBuffer.toString(StandardCharsets.UTF_8);
}

/** Wipes the input and output. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -402,10 +402,10 @@ private boolean shouldFail(String blobName, double probability) {
private int hashCode(String path) {
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] bytes = digest.digest(path.getBytes("UTF-8"));
byte[] bytes = digest.digest(path.getBytes(StandardCharsets.UTF_8));
int i = 0;
return ((bytes[i++] & 0xFF) << 24) | ((bytes[i++] & 0xFF) << 16) | ((bytes[i++] & 0xFF) << 8) | (bytes[i++] & 0xFF);
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
} catch (NoSuchAlgorithmException ex) {
throw new ElasticsearchException("cannot calculate hashcode", ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.xcontent.XContentType;

import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -47,8 +46,7 @@ public HttpResponseBuilder withHttpStatus(final int httpStatus) {
return this;
}

public HttpResponseBuilder withResponseBody(final String responseJson) throws ElasticsearchParseException,
UnsupportedEncodingException {
public HttpResponseBuilder withResponseBody(final String responseJson) throws ElasticsearchParseException {
if (responseJson == null || responseJson.trim().isEmpty()) {
throw new ElasticsearchParseException(
"Invalid string provided as http response body, Failed to parse content to form response body."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -463,8 +462,8 @@ private String base64Encode(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}

private static String urlEncode(String param) throws UnsupportedEncodingException {
return URLEncoder.encode(param, StandardCharsets.UTF_8.name());
private static String urlEncode(String param) {
return URLEncoder.encode(param, StandardCharsets.UTF_8);
}

private String deflateAndBase64Encode(SAMLObject message) throws Exception {
Expand Down
Loading