Skip to content

KAFKA-19137: Use StandardCharsets.UTF_8 instead of StandardCharsets.UTF_8.name() #19464

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

Merged
merged 3 commits into from
Apr 15, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void start(Map<String, String> props) {
outputStream = new PrintStream(
Files.newOutputStream(Paths.get(filename), StandardOpenOption.CREATE, StandardOpenOption.APPEND),
false,
StandardCharsets.UTF_8.name());
StandardCharsets.UTF_8);
} catch (IOException e) {
throw new ConnectException("Couldn't find or create file '" + filename + "' for FileStreamSinkTask", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.URI;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -236,14 +234,9 @@ private void checkHerder(SourceAndTarget sourceAndTarget) {
private void addHerder(SourceAndTarget sourceAndTarget) {
log.info("creating herder for " + sourceAndTarget.toString());
Map<String, String> workerProps = config.workerConfig(sourceAndTarget);
List<String> restNamespace;
try {
String encodedSource = encodePath(sourceAndTarget.source());
String encodedTarget = encodePath(sourceAndTarget.target());
restNamespace = Arrays.asList(encodedSource, encodedTarget);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Unable to create encoded URL paths for source and target using UTF-8", e);
}
String encodedSource = encodePath(sourceAndTarget.source());
String encodedTarget = encodePath(sourceAndTarget.target());
List<String> restNamespace = List.of(encodedSource, encodedTarget);
String workerId = generateWorkerId(sourceAndTarget);
Plugins plugins = new Plugins(workerProps);
plugins.compareAndSwapWithDelegatingLoader();
Expand Down Expand Up @@ -282,8 +275,8 @@ private void addHerder(SourceAndTarget sourceAndTarget) {
herders.put(sourceAndTarget, herder);
}

private static String encodePath(String rawPath) throws UnsupportedEncodingException {
return URLEncoder.encode(rawPath, StandardCharsets.UTF_8.name())
private static String encodePath(String rawPath) {
return URLEncoder.encode(rawPath, StandardCharsets.UTF_8)
// Java's out-of-the-box URL encoder encodes spaces (' ') as pluses ('+'),
// and pluses as '%2B'
// But Jetty doesn't decode pluses at all and leaves them as-are in decoded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void testRestForwardToLeader(boolean dualListener, boolean followerSsl, b
"\"name\": \"blah\"," +
"\"config\": {}" +
"}";
StringEntity entity = new StringEntity(jsonBody, StandardCharsets.UTF_8.name());
StringEntity entity = new StringEntity(jsonBody, StandardCharsets.UTF_8);
entity.setContentType("application/json");
request.setEntity(entity);
HttpResponse httpResponse = executeRequest(followerUrl, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ private String executeGet(URI serverUrl, String endpoint) throws IOException {

private String executePut(URI serverUrl, String endpoint, String jsonBody) throws IOException {
HttpPut request = new HttpPut(endpoint);
StringEntity entity = new StringEntity(jsonBody, StandardCharsets.UTF_8.name());
StringEntity entity = new StringEntity(jsonBody, StandardCharsets.UTF_8);
entity.setContentType("application/json");
request.setEntity(entity);
HttpResponse response = executeRequest(serverUrl, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class PrintedTest {
Expand Down Expand Up @@ -75,34 +75,34 @@ public void shouldCreateProcessorThatPrintsToFile() throws IOException {
}

@Test
public void shouldCreateProcessorThatPrintsToStdOut() throws UnsupportedEncodingException {
public void shouldCreateProcessorThatPrintsToStdOut() {
final ProcessorSupplier<String, Integer, Void, Void> supplier = new PrintedInternal<>(sysOutPrinter).build("processor");
final Processor<String, Integer, Void, Void> processor = supplier.get();

processor.process(new Record<>("good", 2, 0L));
processor.close();
assertThat(sysOut.toString(StandardCharsets.UTF_8.name()), equalTo("[processor]: good, 2\n"));
assertEquals(sysOut.toString(StandardCharsets.UTF_8), "[processor]: good, 2\n");
}

@Test
public void shouldPrintWithLabel() throws UnsupportedEncodingException {
public void shouldPrintWithLabel() {
final Processor<String, Integer, Void, Void> processor = new PrintedInternal<>(sysOutPrinter.withLabel("label"))
.build("processor")
.get();

processor.process(new Record<>("hello", 3, 0L));
processor.close();
assertThat(sysOut.toString(StandardCharsets.UTF_8.name()), equalTo("[label]: hello, 3\n"));
assertEquals(sysOut.toString(StandardCharsets.UTF_8), "[label]: hello, 3\n");
}

@Test
public void shouldPrintWithKeyValueMapper() throws UnsupportedEncodingException {
public void shouldPrintWithKeyValueMapper() {
final Processor<String, Integer, Void, Void> processor = new PrintedInternal<>(
sysOutPrinter.withKeyValueMapper((key, value) -> String.format("%s -> %d", key, value))
).build("processor").get();
processor.process(new Record<>("hello", 1, 0L));
processor.close();
assertThat(sysOut.toString(StandardCharsets.UTF_8.name()), equalTo("[processor]: hello -> 1\n"));
assertEquals(sysOut.toString(StandardCharsets.UTF_8), "[processor]: hello -> 1\n");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ static HttpURLConnection newHttpConnection(URL url) throws IOException {

// Static package-private so unit tests can mock reading response
static String readResponse(InputStream is) {
try (Scanner s = new Scanner(is, StandardCharsets.UTF_8.name()).useDelimiter("\\A")) {
try (Scanner s = new Scanner(is, StandardCharsets.UTF_8).useDelimiter("\\A")) {
return s.hasNext() ? s.next() : "";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -591,34 +590,30 @@ public CommandResult(int returnCode, String out, String err, PluginScanResult re
private static CommandResult runCommand(Object... args) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
try {
int returnCode = ConnectPluginPath.mainNoExit(
Arrays.stream(args)
.map(Object::toString)
.collect(Collectors.toList())
.toArray(new String[]{}),
new PrintStream(out, true, "utf-8"),
new PrintStream(err, true, "utf-8"));
Set<Path> pluginLocations = getPluginLocations(args);
ClassLoader parent = ConnectPluginPath.class.getClassLoader();
ClassLoaderFactory factory = new ClassLoaderFactory();
try (DelegatingClassLoader delegatingClassLoader = factory.newDelegatingClassLoader(parent)) {
Set<PluginSource> sources = PluginUtils.pluginSources(pluginLocations, delegatingClassLoader, factory);
String stdout = new String(out.toByteArray(), StandardCharsets.UTF_8);
String stderr = new String(err.toByteArray(), StandardCharsets.UTF_8);
log.info("STDOUT:\n{}", stdout);
log.info("STDERR:\n{}", stderr);
return new CommandResult(
returnCode,
stdout,
stderr,
new ReflectionScanner().discoverPlugins(sources),
new ServiceLoaderScanner().discoverPlugins(sources)
);
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (UnsupportedEncodingException e) {
int returnCode = ConnectPluginPath.mainNoExit(
Arrays.stream(args)
.map(Object::toString)
.toList()
.toArray(new String[]{}),
new PrintStream(out, true, StandardCharsets.UTF_8),
new PrintStream(err, true, StandardCharsets.UTF_8));
Set<Path> pluginLocations = getPluginLocations(args);
ClassLoader parent = ConnectPluginPath.class.getClassLoader();
ClassLoaderFactory factory = new ClassLoaderFactory();
try (DelegatingClassLoader delegatingClassLoader = factory.newDelegatingClassLoader(parent)) {
Set<PluginSource> sources = PluginUtils.pluginSources(pluginLocations, delegatingClassLoader, factory);
String stdout = out.toString(StandardCharsets.UTF_8);
String stderr = err.toString(StandardCharsets.UTF_8);
log.info("STDOUT:\n{}", stdout);
log.info("STDERR:\n{}", stderr);
return new CommandResult(
returnCode,
stdout,
stderr,
new ReflectionScanner().discoverPlugins(sources),
new ServiceLoaderScanner().discoverPlugins(sources)
);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Expand Down