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 java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
@ -117,7 +116,7 @@ public class DirectoryConfigProvider implements ConfigProvider {
|
|||
|
||||
private static String read(Path path) {
|
||||
try {
|
||||
return new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
|
||||
return Files.readString(path);
|
||||
} catch (IOException 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());
|
||||
|
|
|
@ -1426,14 +1426,8 @@ public abstract class AbstractStickyAssignorTest {
|
|||
}
|
||||
|
||||
private String pad(int num, int digits) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int iDigits = Integer.toString(num).length();
|
||||
|
||||
for (int i = 1; i <= digits - iDigits; ++i)
|
||||
sb.append("0");
|
||||
|
||||
sb.append(num);
|
||||
return sb.toString();
|
||||
return "0".repeat(Math.max(0, digits - iDigits)) + num;
|
||||
}
|
||||
|
||||
protected static List<String> topics(String... topics) {
|
||||
|
|
|
@ -443,7 +443,7 @@ public class MiniKdc {
|
|||
reader.lines().forEach(line -> stringBuilder.append(line).append("{3}"));
|
||||
}
|
||||
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 {
|
||||
|
|
|
@ -58,11 +58,7 @@ public class CodeBuffer {
|
|||
}
|
||||
|
||||
private String indentSpaces() {
|
||||
StringBuilder bld = new StringBuilder();
|
||||
for (int i = 0; i < indent; i++) {
|
||||
bld.append(" ");
|
||||
}
|
||||
return bld.toString();
|
||||
return " ".repeat(Math.max(0, indent));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -25,7 +25,6 @@ import org.junit.jupiter.api.Timeout;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
|
@ -87,7 +86,7 @@ public class LinuxIoMetricsCollectorTest {
|
|||
"read_bytes: " + readBytes + "\n" +
|
||||
"write_bytes: " + writeBytes + "\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() {
|
||||
StringBuilder bld = new StringBuilder();
|
||||
for (int i = 0; i < indentationLevel; i++) {
|
||||
for (int j = 0; j < 2; j++) {
|
||||
bld.append(" ");
|
||||
}
|
||||
}
|
||||
return bld.toString();
|
||||
return " ".repeat(2).repeat(Math.max(0, indentationLevel));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -39,7 +39,7 @@ public final class LocalTieredStorageOutput<K, V> implements LocalTieredStorageT
|
|||
this.keyDe = keyDe;
|
||||
this.valueDe = valueDe;
|
||||
// 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) {
|
||||
|
@ -54,14 +54,6 @@ public final class LocalTieredStorageOutput<K, V> implements LocalTieredStorageT
|
|||
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
|
||||
public void visitTopicIdPartition(TopicIdPartition topicIdPartition) {
|
||||
currentTopic = topicIdPartition.topicPartition().topic();
|
||||
|
|
|
@ -287,11 +287,7 @@ public class HighAvailabilityTaskAssignorIntegrationTest {
|
|||
}
|
||||
|
||||
private static String getKiloByteValue() {
|
||||
final StringBuilder kiloBuilder = new StringBuilder(1000);
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
kiloBuilder.append('0');
|
||||
}
|
||||
return kiloBuilder.toString();
|
||||
return "0".repeat(1000);
|
||||
}
|
||||
|
||||
private static void assertFalseNoRetry(final boolean assertion, final String message) {
|
||||
|
|
|
@ -100,14 +100,8 @@ public class MaterializedTest {
|
|||
|
||||
@Test
|
||||
public void shouldThrowTopologyExceptionIfStoreNameExceedsMaxAllowedLength() {
|
||||
final StringBuffer invalidStoreNameBuffer = new StringBuffer();
|
||||
final int maxNameLength = 249;
|
||||
|
||||
for (int i = 0; i < maxNameLength + 1; i++) {
|
||||
invalidStoreNameBuffer.append('a');
|
||||
}
|
||||
|
||||
final String invalidStoreName = invalidStoreNameBuffer.toString();
|
||||
final String invalidStoreName = "a".repeat(maxNameLength + 1);
|
||||
|
||||
final TopologyException e = assertThrows(TopologyException.class,
|
||||
() -> Materialized.as(invalidStoreName));
|
||||
|
|
|
@ -194,7 +194,7 @@ public class ProducerPerformance {
|
|||
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);
|
||||
|
||||
|
|
|
@ -36,7 +36,6 @@ import org.mockito.ArgumentCaptor;
|
|||
import org.mockito.MockedStatic;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
|
@ -325,7 +324,7 @@ public class LeaderElectionCommandTest {
|
|||
|
||||
String jsonString = stringifyTopicPartitions(new HashSet<>(partitions));
|
||||
|
||||
Files.write(file.toPath(), jsonString.getBytes(StandardCharsets.UTF_8));
|
||||
Files.writeString(file.toPath(), jsonString);
|
||||
|
||||
return file.toPath();
|
||||
}
|
||||
|
@ -333,7 +332,7 @@ public class LeaderElectionCommandTest {
|
|||
private Path tempAdminConfig(String defaultApiTimeoutMs, String requestTimeoutMs) throws Exception {
|
||||
String content = "default.api.timeout.ms=" + defaultApiTimeoutMs + "\nrequest.timeout.ms=" + requestTimeoutMs;
|
||||
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();
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,6 @@ import org.junit.jupiter.api.Test;
|
|||
import org.junit.jupiter.api.Timeout;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
@ -62,7 +61,7 @@ public class JsonUtilTest {
|
|||
assertEquals(1, JsonUtil.objectFromCommandLineArgument(" {\"bar\": 1} ", Foo.class).bar);
|
||||
File tempFile = TestUtils.tempFile();
|
||||
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);
|
||||
} finally {
|
||||
Files.delete(tempFile.toPath());
|
||||
|
|
Loading…
Reference in New Issue