mirror of https://github.com/apache/kafka.git
MINOR: Use `Files.readString/writeString` and `String.repeat` to simplify code (#18372)
The 3 methods were introduced in Java 11. Reviewers: Divij Vaidya <diviv@amazon.com>
This commit is contained in:
parent
d6f24d3665
commit
73ab7ee4ea
|
@ -24,7 +24,6 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -117,7 +116,7 @@ public class DirectoryConfigProvider implements ConfigProvider {
|
||||||
|
|
||||||
private static String read(Path path) {
|
private static String read(Path path) {
|
||||||
try {
|
try {
|
||||||
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
|
return Files.readString(path);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error("Could not read file {} for property {}", path, path.getFileName(), e);
|
log.error("Could not read file {} for property {}", path, path.getFileName(), e);
|
||||||
throw new ConfigException("Could not read file " + path + " for property " + path.getFileName());
|
throw new ConfigException("Could not read file " + path + " for property " + path.getFileName());
|
||||||
|
|
|
@ -1426,14 +1426,8 @@ public abstract class AbstractStickyAssignorTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String pad(int num, int digits) {
|
private String pad(int num, int digits) {
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
int iDigits = Integer.toString(num).length();
|
int iDigits = Integer.toString(num).length();
|
||||||
|
return "0".repeat(Math.max(0, digits - iDigits)) + num;
|
||||||
for (int i = 1; i <= digits - iDigits; ++i)
|
|
||||||
sb.append("0");
|
|
||||||
|
|
||||||
sb.append(num);
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static List<String> topics(String... topics) {
|
protected static List<String> topics(String... topics) {
|
||||||
|
|
|
@ -443,7 +443,7 @@ public class MiniKdc {
|
||||||
reader.lines().forEach(line -> stringBuilder.append(line).append("{3}"));
|
reader.lines().forEach(line -> stringBuilder.append(line).append("{3}"));
|
||||||
}
|
}
|
||||||
String output = MessageFormat.format(stringBuilder.toString(), realm, host, String.valueOf(port), System.lineSeparator());
|
String output = MessageFormat.format(stringBuilder.toString(), realm, host, String.valueOf(port), System.lineSeparator());
|
||||||
Files.write(krb5conf.toPath(), output.getBytes(StandardCharsets.UTF_8));
|
Files.writeString(krb5conf.toPath(), output);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refreshJvmKerberosConfig() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
private void refreshJvmKerberosConfig() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
||||||
|
|
|
@ -58,11 +58,7 @@ public class CodeBuffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
private String indentSpaces() {
|
private String indentSpaces() {
|
||||||
StringBuilder bld = new StringBuilder();
|
return " ".repeat(Math.max(0, indent));
|
||||||
for (int i = 0; i < indent; i++) {
|
|
||||||
bld.append(" ");
|
|
||||||
}
|
|
||||||
return bld.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -25,7 +25,6 @@ import org.junit.jupiter.api.Timeout;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
@ -87,7 +86,7 @@ public class LinuxIoMetricsCollectorTest {
|
||||||
"read_bytes: " + readBytes + "\n" +
|
"read_bytes: " + readBytes + "\n" +
|
||||||
"write_bytes: " + writeBytes + "\n" +
|
"write_bytes: " + writeBytes + "\n" +
|
||||||
"cancelled_write_bytes: 0\n";
|
"cancelled_write_bytes: 0\n";
|
||||||
Files.write(selfDir.resolve("io"), bld.getBytes(StandardCharsets.UTF_8));
|
Files.writeString(selfDir.resolve("io"), bld);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,13 +35,7 @@ public class ShellNodePrinter implements MetadataNodePrinter {
|
||||||
}
|
}
|
||||||
|
|
||||||
String indentationString() {
|
String indentationString() {
|
||||||
StringBuilder bld = new StringBuilder();
|
return " ".repeat(2).repeat(Math.max(0, indentationLevel));
|
||||||
for (int i = 0; i < indentationLevel; i++) {
|
|
||||||
for (int j = 0; j < 2; j++) {
|
|
||||||
bld.append(" ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return bld.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -39,7 +39,7 @@ public final class LocalTieredStorageOutput<K, V> implements LocalTieredStorageT
|
||||||
this.keyDe = keyDe;
|
this.keyDe = keyDe;
|
||||||
this.valueDe = valueDe;
|
this.valueDe = valueDe;
|
||||||
// Columns length + 5 column separators.
|
// Columns length + 5 column separators.
|
||||||
output += repeatString("-", 51 + 8 + 13 + 10 + (3 * 2)) + System.lineSeparator();
|
output += "-".repeat(51 + 8 + 13 + 10 + (3 * 2)) + System.lineSeparator();
|
||||||
}
|
}
|
||||||
|
|
||||||
private String row(String file, Object offset, String record, String ident) {
|
private String row(String file, Object offset, String record, String ident) {
|
||||||
|
@ -54,14 +54,6 @@ public final class LocalTieredStorageOutput<K, V> implements LocalTieredStorageT
|
||||||
return row("", "", "");
|
return row("", "", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
private String repeatString(String str, int times) {
|
|
||||||
StringBuilder builder = new StringBuilder();
|
|
||||||
for (int i = 0; i < times; i++) {
|
|
||||||
builder.append(str);
|
|
||||||
}
|
|
||||||
return builder.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visitTopicIdPartition(TopicIdPartition topicIdPartition) {
|
public void visitTopicIdPartition(TopicIdPartition topicIdPartition) {
|
||||||
currentTopic = topicIdPartition.topicPartition().topic();
|
currentTopic = topicIdPartition.topicPartition().topic();
|
||||||
|
@ -110,4 +102,4 @@ public final class LocalTieredStorageOutput<K, V> implements LocalTieredStorageT
|
||||||
this.t2 = t2;
|
this.t2 = t2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -287,11 +287,7 @@ public class HighAvailabilityTaskAssignorIntegrationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getKiloByteValue() {
|
private static String getKiloByteValue() {
|
||||||
final StringBuilder kiloBuilder = new StringBuilder(1000);
|
return "0".repeat(1000);
|
||||||
for (int i = 0; i < 1000; i++) {
|
|
||||||
kiloBuilder.append('0');
|
|
||||||
}
|
|
||||||
return kiloBuilder.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertFalseNoRetry(final boolean assertion, final String message) {
|
private static void assertFalseNoRetry(final boolean assertion, final String message) {
|
||||||
|
|
|
@ -100,18 +100,12 @@ public class MaterializedTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldThrowTopologyExceptionIfStoreNameExceedsMaxAllowedLength() {
|
public void shouldThrowTopologyExceptionIfStoreNameExceedsMaxAllowedLength() {
|
||||||
final StringBuffer invalidStoreNameBuffer = new StringBuffer();
|
|
||||||
final int maxNameLength = 249;
|
final int maxNameLength = 249;
|
||||||
|
final String invalidStoreName = "a".repeat(maxNameLength + 1);
|
||||||
for (int i = 0; i < maxNameLength + 1; i++) {
|
|
||||||
invalidStoreNameBuffer.append('a');
|
|
||||||
}
|
|
||||||
|
|
||||||
final String invalidStoreName = invalidStoreNameBuffer.toString();
|
|
||||||
|
|
||||||
final TopologyException e = assertThrows(TopologyException.class,
|
final TopologyException e = assertThrows(TopologyException.class,
|
||||||
() -> Materialized.as(invalidStoreName));
|
() -> Materialized.as(invalidStoreName));
|
||||||
assertEquals(e.getMessage(), "Invalid topology: Name is illegal, it can't be longer than " + maxNameLength +
|
assertEquals(e.getMessage(), "Invalid topology: Name is illegal, it can't be longer than " + maxNameLength +
|
||||||
" characters, name: " + invalidStoreName);
|
" characters, name: " + invalidStoreName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -194,7 +194,7 @@ public class ProducerPerformance {
|
||||||
throw new IllegalArgumentException("File does not exist or empty file provided.");
|
throw new IllegalArgumentException("File does not exist or empty file provided.");
|
||||||
}
|
}
|
||||||
|
|
||||||
String[] payloadList = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).split(payloadDelimiter);
|
String[] payloadList = Files.readString(path).split(payloadDelimiter);
|
||||||
|
|
||||||
System.out.println("Number of messages read: " + payloadList.length);
|
System.out.println("Number of messages read: " + payloadList.length);
|
||||||
|
|
||||||
|
|
|
@ -36,7 +36,6 @@ import org.mockito.ArgumentCaptor;
|
||||||
import org.mockito.MockedStatic;
|
import org.mockito.MockedStatic;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
|
@ -325,7 +324,7 @@ public class LeaderElectionCommandTest {
|
||||||
|
|
||||||
String jsonString = stringifyTopicPartitions(new HashSet<>(partitions));
|
String jsonString = stringifyTopicPartitions(new HashSet<>(partitions));
|
||||||
|
|
||||||
Files.write(file.toPath(), jsonString.getBytes(StandardCharsets.UTF_8));
|
Files.writeString(file.toPath(), jsonString);
|
||||||
|
|
||||||
return file.toPath();
|
return file.toPath();
|
||||||
}
|
}
|
||||||
|
@ -333,7 +332,7 @@ public class LeaderElectionCommandTest {
|
||||||
private Path tempAdminConfig(String defaultApiTimeoutMs, String requestTimeoutMs) throws Exception {
|
private Path tempAdminConfig(String defaultApiTimeoutMs, String requestTimeoutMs) throws Exception {
|
||||||
String content = "default.api.timeout.ms=" + defaultApiTimeoutMs + "\nrequest.timeout.ms=" + requestTimeoutMs;
|
String content = "default.api.timeout.ms=" + defaultApiTimeoutMs + "\nrequest.timeout.ms=" + requestTimeoutMs;
|
||||||
java.io.File file = TestUtils.tempFile("admin-config", ".properties");
|
java.io.File file = TestUtils.tempFile("admin-config", ".properties");
|
||||||
Files.write(file.toPath(), content.getBytes(StandardCharsets.UTF_8));
|
Files.writeString(file.toPath(), content);
|
||||||
return file.toPath();
|
return file.toPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,6 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.Timeout;
|
import org.junit.jupiter.api.Timeout;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
@ -62,7 +61,7 @@ public class JsonUtilTest {
|
||||||
assertEquals(1, JsonUtil.objectFromCommandLineArgument(" {\"bar\": 1} ", Foo.class).bar);
|
assertEquals(1, JsonUtil.objectFromCommandLineArgument(" {\"bar\": 1} ", Foo.class).bar);
|
||||||
File tempFile = TestUtils.tempFile();
|
File tempFile = TestUtils.tempFile();
|
||||||
try {
|
try {
|
||||||
Files.write(tempFile.toPath(), "{\"bar\": 456}".getBytes(StandardCharsets.UTF_8));
|
Files.writeString(tempFile.toPath(), "{\"bar\": 456}");
|
||||||
assertEquals(456, JsonUtil.objectFromCommandLineArgument(tempFile.getAbsolutePath(), Foo.class).bar);
|
assertEquals(456, JsonUtil.objectFromCommandLineArgument(tempFile.getAbsolutePath(), Foo.class).bar);
|
||||||
} finally {
|
} finally {
|
||||||
Files.delete(tempFile.toPath());
|
Files.delete(tempFile.toPath());
|
||||||
|
|
Loading…
Reference in New Issue