Skip to content

Commit

Permalink
HADOOP-19134. Use StringBuilder instead of StringBuffer. (apache#6692)…
Browse files Browse the repository at this point in the history
…. Contributed by PJ Fanning
  • Loading branch information
pjfanning authored Aug 18, 2024
1 parent b5f8899 commit 59dba6e
Show file tree
Hide file tree
Showing 122 changed files with 195 additions and 196 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ protected int init(String[] args) throws IOException {

@Override
public String getCommandUsage() {
StringBuffer sbuf = new StringBuffer(USAGE_PREFIX + COMMANDS);
StringBuilder sbuf = new StringBuilder(USAGE_PREFIX + COMMANDS);
String banner = StringUtils.repeat("=", 66);
sbuf.append(banner + "\n");
sbuf.append(CreateCommand.USAGE + ":\n\n" + CreateCommand.DESC + "\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ protected void parseExecResult(BufferedReader lines) throws IOException {
@VisibleForTesting
protected void parseOutput() throws IOException {
if (output.size() < 2) {
StringBuffer sb = new StringBuffer("Fewer lines of output than expected");
StringBuilder sb = new StringBuilder("Fewer lines of output than expected");
if (output.size() > 0) {
sb.append(": " + output.get(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ private static void unTarUsingTar(InputStream inputStream, File untarDir,

private static void unTarUsingTar(File inFile, File untarDir,
boolean gzipped) throws IOException {
StringBuffer untarCommand = new StringBuffer();
StringBuilder untarCommand = new StringBuilder();
// not using canonical path here; this postpones relative path
// resolution until bash is executed.
final String source = "'" + FileUtil.makeSecureShellPath(inFile) + "'";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public RejectState getRejectState() {

@Override
public String toString() {
return new StringBuffer().append("xid:").append(xid)
return new StringBuilder().append("xid:").append(xid)
.append(",messageType:").append(messageType).append("verifier_flavor:")
.append(verifier.getFlavor()).append("rejectState:")
.append(rejectState).toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public static Configuration excludeIncompatibleCredentialProviders(
if (providerPath == null) {
return config;
}
StringBuffer newProviderPath = new StringBuffer();
StringBuilder newProviderPath = new StringBuilder();
String[] providers = providerPath.split(",");
Path path = null;
for (String provider: providers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ protected int init(String[] args) throws IOException {

@Override
public String getCommandUsage() {
StringBuffer sbuf = new StringBuffer(USAGE_PREFIX + COMMANDS);
StringBuilder sbuf = new StringBuilder(USAGE_PREFIX + COMMANDS);
String banner = StringUtils.repeat("=", 66);
sbuf.append(banner + "\n")
.append(CreateCommand.USAGE + ":\n\n" + CreateCommand.DESC + "\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public void check(final String[] hosts, final String[] cns,
strictWithSubDomains);
}
// Build up lists of allowed hosts For logging/debugging purposes.
StringBuffer buf = new StringBuffer(32);
StringBuilder buf = new StringBuilder(32);
buf.append('<');
for (int i = 0; i < hosts.length; i++) {
String h = hosts[i];
Expand Down Expand Up @@ -408,15 +408,15 @@ public void check(final String[] hosts, final String[] cns,
throw new SSLException(msg);
}

// StringBuffer for building the error message.
buf = new StringBuffer();
// StringBuilder for building the error message.
buf = new StringBuilder();

boolean match = false;
out:
for (Iterator<String> it = names.iterator(); it.hasNext();) {
// Don't trim the CN, though!
final String cn = StringUtils.toLowerCase(it.next());
// Store CN in StringBuffer in case we need to report an error.
// Store CN in StringBuilder in case we need to report an error.
buf.append(" <")
.append(cn)
.append('>');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1014,7 +1014,7 @@ private void runCommand() throws IOException {
BufferedReader inReader =
new BufferedReader(new InputStreamReader(process.getInputStream(),
StandardCharsets.UTF_8));
final StringBuffer errMsg = new StringBuffer();
final StringBuilder errMsg = new StringBuilder();

// read error and input streams as this would free up the buffers
// free the error stream buffer
Expand Down Expand Up @@ -1208,7 +1208,7 @@ public static class ShellCommandExecutor extends Shell
implements CommandExecutor {

private String[] command;
private StringBuffer output;
private StringBuilder output;


public ShellCommandExecutor(String[] execString) {
Expand Down Expand Up @@ -1289,7 +1289,7 @@ public String[] getExecString() {

@Override
protected void parseExecResult(BufferedReader lines) throws IOException {
output = new StringBuffer();
output = new StringBuilder();
char[] buf = new char[512];
int nRead;
while ( (nRead = lines.read(buf, 0, buf.length)) > 0 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ public static String wrap(String str, int wrapLength, String newLineStr,

int inputLineLength = str.length();
int offset = 0;
StringBuffer wrappedLine = new StringBuffer(inputLineLength + 32);
StringBuilder wrappedLine = new StringBuilder(inputLineLength + 32);

while(inputLineLength - offset > wrapLength) {
if(str.charAt(offset) == 32) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public MockQuotaUsage() {
public String toString(boolean hOption,
boolean tOption, List<StorageType> types) {
if (tOption) {
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
result.append(hOption ? HUMAN : BYTES);

for (StorageType type : types) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void testUriErrors() throws Exception {
}

private static char[] generatePassword(int length) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int i = 0; i < length; i++) {
sb.append(chars[r.nextInt(chars.length)]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public void testBashQuote() {
@Test(timeout=120000)
public void testDestroyAllShellProcesses() throws Throwable {
Assume.assumeFalse(WINDOWS);
StringBuffer sleepCommand = new StringBuffer();
StringBuilder sleepCommand = new StringBuilder();
sleepCommand.append("sleep 200");
String[] shellCmd = {"bash", "-c", sleepCommand.toString()};
final ShellCommandExecutor shexc1 = new ShellCommandExecutor(shellCmd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public void log(Object o) {
*/
@Test
public void testSingleton() throws Throwable {
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
String name = "singleton";
RemoteIterator<String> it = remoteIteratorFromSingleton(name);
assertStringValueContains(it, "SingletonIterator");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected String generateLoadBalancingKeyProviderUriString() {
if (kmsUrl == null || kmsUrl.size() == 0) {
return null;
}
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();

for (int i = 0; i < kmsUrl.size(); i++) {
sb.append(KMSClientProvider.SCHEME_NAME + "://" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private Integer listCorruptFileBlocks(String dir, String baseUrl)
final String cookiePrefix = "Cookie:";
boolean allDone = false;
while (!allDone) {
final StringBuffer url = new StringBuffer(baseUrl);
final StringBuilder url = new StringBuilder(baseUrl);
if (cookie > 0) {
url.append("&startblockafter=").append(String.valueOf(cookie));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.apache.hadoop.hdfs.server.namenode.FSEditLogOp;
import org.apache.hadoop.hdfs.server.namenode.FSEditLogOpCodes;
import org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.OpInstanceCache;
import org.apache.hadoop.hdfs.tools.offlineEditsViewer.OfflineEditsViewer;
import org.apache.hadoop.hdfs.util.XMLUtils.Stanza;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
Expand All @@ -57,7 +56,7 @@ class OfflineEditsXmlLoader
private Stanza stanza;
private Stack<Stanza> stanzaStack;
private FSEditLogOpCodes opCode;
private StringBuffer cbuf;
private StringBuilder cbuf;
private long nextTxId;
private final OpInstanceCache opCache = new OpInstanceCache();

Expand Down Expand Up @@ -119,7 +118,7 @@ public void startDocument() {
stanza = null;
stanzaStack = new Stack<Stanza>();
opCode = null;
cbuf = new StringBuffer();
cbuf = new StringBuilder();
nextTxId = -1;
}

Expand Down Expand Up @@ -182,7 +181,7 @@ public void startElement (String uri, String name,
@Override
public void endElement (String uri, String name, String qName) {
String str = XMLUtils.unmangleXmlString(cbuf.toString(), false).trim();
cbuf = new StringBuffer();
cbuf = new StringBuilder();
switch (state) {
case EXPECT_EDITS_TAG:
throw new InvalidXmlException("expected <EDITS/>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ long getId() {
}

String getType() {
StringBuffer s = new StringBuffer();
StringBuilder s = new StringBuilder();
if (type.contains(PBImageCorruptionType.CORRUPT_NODE)) {
s.append(PBImageCorruptionType.CORRUPT_NODE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ private class MyFile {
for (int idx = 0; idx < nLevels; idx++) {
levels[idx] = gen.nextInt(10);
}
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (int idx = 0; idx < nLevels; idx++) {
sb.append(dirNames[levels[idx]]);
sb.append("/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ private Trash getPerUserTrash(UserGroupInformation ugi,
FileSystem fileSystem, Configuration config) throws IOException {
// generate an unique path per instance
UUID trashId = UUID.randomUUID();
StringBuffer sb = new StringBuffer()
StringBuilder sb = new StringBuilder()
.append(ugi.getUserName())
.append("-")
.append(trashId.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1833,7 +1833,7 @@ public void testMetaSavePostponedMisreplicatedBlocks() throws IOException {
DataInputStream in = new DataInputStream(fstream);

BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
Expand Down Expand Up @@ -1861,7 +1861,7 @@ public void testMetaSaveMissingReplicas() throws Exception {
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
Expand Down Expand Up @@ -1933,7 +1933,7 @@ public void testMetaSaveInMaintenanceReplicas() throws Exception {
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
Expand Down Expand Up @@ -1989,7 +1989,7 @@ public void testMetaSaveDecommissioningReplicas() throws Exception {
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void testExcludeDataNodes() throws Exception {
//For GETFILECHECKSUM, OPEN and APPEND,
//the chosen datanode must be different with exclude nodes.

StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 2; i++) {
sb.append(locations[i].getXferAddr());
{ // test GETFILECHECKSUM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class ConfBlock extends HtmlBlock {
__().
tbody();
for (ConfEntryInfo entry : info.getProperties()) {
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
String[] sources = entry.getSource();
//Skip the last entry, because it is always the same HDFS file, and
// output them in reverse order so most recent is output first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2080,7 +2080,7 @@ private void writeOutput(TaskAttempt attempt, Configuration conf)

private void validateOutput() throws IOException {
File expectedFile = new File(new Path(outputDir, partFile).toString());
StringBuffer expectedOutput = new StringBuffer();
StringBuilder expectedOutput = new StringBuilder();
expectedOutput.append(key1).append('\t').append(val1).append("\n");
expectedOutput.append(val1).append("\n");
expectedOutput.append(val2).append("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public void verifyTaskAttemptGeneric(TaskAttempt ta, TaskType ttype,
String expectDiag = "";
List<String> diagnosticsList = ta.getDiagnostics();
if (diagnosticsList != null && !diagnostics.isEmpty()) {
StringBuffer b = new StringBuffer();
StringBuilder b = new StringBuilder();
for (String diag : diagnosticsList) {
b.append(diag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public void verifyAMJobGenericSecure(Job job, int mapsPending,
String diagString = "";
List<String> diagList = job.getDiagnostics();
if (diagList != null && !diagList.isEmpty()) {
StringBuffer b = new StringBuffer();
StringBuilder b = new StringBuilder();
for (String diag : diagList) {
b.append(diag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1027,8 +1027,8 @@ static void setupChildMapredLocalDirs(Task t, JobConf conf) {
String taskId = t.getTaskID().toString();
boolean isCleanup = t.isTaskCleanupTask();
String user = t.getUser();
StringBuffer childMapredLocalDir =
new StringBuffer(localDirs[0] + Path.SEPARATOR
StringBuilder childMapredLocalDir =
new StringBuilder(localDirs[0] + Path.SEPARATOR
+ getLocalTaskDir(user, jobId, taskId, isCleanup));
for (int i = 1; i < localDirs.length; i++) {
childMapredLocalDir.append("," + localDirs[i] + Path.SEPARATOR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static String getApplicationWebURLOnJHSWithoutScheme(Configuration conf,
InetSocketAddress address = NetUtils.createSocketAddr(
hsAddress, getDefaultJHSWebappPort(),
getDefaultJHSWebappURLWithoutScheme());
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
if (address.getAddress() != null &&
(address.getAddress().isAnyLocalAddress() ||
address.getAddress().isLoopbackAddress())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void testNewApis() throws Exception {
static String readOutput(Path outDir, Configuration conf)
throws IOException {
FileSystem fs = outDir.getFileSystem(conf);
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();

Path[] fileList = FileUtil.stat2Paths(fs.listStatus(outDir,
new Utils.OutputFileUtils.OutputFilesFilter()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public static void addInputPaths(JobConf conf, String commaSeparatedPaths) {
*/
public static void setInputPaths(JobConf conf, Path... inputPaths) {
Path path = new Path(conf.getWorkingDirectory(), inputPaths[0]);
StringBuffer str = new StringBuffer(StringUtils.escapeString(path.toString()));
StringBuilder str = new StringBuilder(StringUtils.escapeString(path.toString()));
for(int i = 1; i < inputPaths.length;i++) {
str.append(StringUtils.COMMA_STR);
path = new Path(conf.getWorkingDirectory(), inputPaths[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public List<IOException> getProblems() {
* @return the concatenated messages from all of the problems.
*/
public String getMessage() {
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
Iterator<IOException> itr = problems.iterator();
while(itr.hasNext()) {
result.append(itr.next().getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void addToSet(Set<String> set, String[] array) {

@Override
public String toString() {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for(int i=0; i < getPaths().length; i++) {
sb.append(getPath(i).toUri().getPath() + ":0+" + getLength(i));
if (i < getPaths().length -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public synchronized void write(DataOutput out) throws IOException {
}

public String toString() {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
Iterator<Range> it = ranges.iterator();
while(it.hasNext()) {
Range range = it.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,8 +518,8 @@ static String buildCommandLine(List<String> setup, List<String> cmd,
throws IOException {

String stdout = FileUtil.makeShellPath(stdoutFilename);
String stderr = FileUtil.makeShellPath(stderrFilename);
StringBuffer mergedCmd = new StringBuffer();
String stderr = FileUtil.makeShellPath(stderrFilename);
StringBuilder mergedCmd = new StringBuilder();

// Export the pid of taskJvm to env variable JVM_PID.
// Currently pid is not used on Windows
Expand Down Expand Up @@ -606,7 +606,7 @@ static String buildDebugScriptCommandLine(List<String> cmd, String debugout)
*/
public static String addCommand(List<String> cmd, boolean isExecutable)
throws IOException {
StringBuffer command = new StringBuffer();
StringBuilder command = new StringBuilder();
for(String s: cmd) {
command.append('\'');
if (isExecutable) {
Expand Down
Loading

0 comments on commit 59dba6e

Please sign in to comment.