HADOOP-6471. StringBuffer -> StringBuilder - conversion of references as necessary. Contributed by Kay Kay.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@926256 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas White 2010-03-22 19:08:03 +00:00
parent 124137ff01
commit 0485fe23ba
31 changed files with 119 additions and 74 deletions

View File

@ -199,6 +199,9 @@ Trunk (unreleased changes)
HADOOP-3659. Patch to allow hadoop native to compile on Mac OS X.
(Colin Evans and Allen Wittenauer via tomwhite)
HADOOP-6471. StringBuffer -> StringBuilder - conversion of references
as necessary. (Kay Kay via tomwhite)
OPTIMIZATIONS
HADOOP-6467. Improve the performance on HarFileSystem.listStatus(..).

View File

@ -136,7 +136,7 @@ public static String getMD5Hash(String text) {
}
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;

View File

@ -46,7 +46,7 @@ public CPUParser() {
* @return the EventRecord created
*/
public EventRecord query(String s) throws Exception {
StringBuffer sb = Environment.runCommand("cat /proc/cpuinfo");
CharSequence sb = Environment.runCommandGeneric("cat /proc/cpuinfo");
EventRecord retval = new EventRecord(InetAddress.getLocalHost()
.getCanonicalHostName(), InetAddress.getAllByName(InetAddress.getLocalHost()
.getHostName()), Calendar.getInstance(), "CPU", "Unknown", "CPU", "-");

View File

@ -247,7 +247,7 @@ public static ArrayList<MonitorJob> getJobs() {
if (!file_present)
if (superuser) {
StringBuffer sb = runCommand("sudo smartctl -i " + devices[i]);
CharSequence sb = runCommandGeneric("sudo smartctl -i " + devices[i]);
String patternStr = "[(failed)(device not supported)]";
Pattern pattern = Pattern.compile(patternStr);
Matcher matcher = pattern.matcher(sb.toString());
@ -263,7 +263,7 @@ public static ArrayList<MonitorJob> getJobs() {
}
// now remove disks that dont exist
StringBuffer resetSB = new StringBuffer();
StringBuilder resetSB = new StringBuilder();
for (int j = 0; j < devices.length; j++) {
resetSB.append(devices[j] == null ? "" : devices[j] + ", ");
if (devices[j] != null)
@ -323,7 +323,7 @@ public static int getInterval(ArrayList<MonitorJob> monitors) {
* @return true, if the command is availble, false otherwise
*/
public static boolean checkExistence(String cmd) {
StringBuffer sb = runCommand("which " + cmd);
CharSequence sb = runCommandGeneric("which " + cmd);
if (sb.length() > 1)
return true;
@ -331,15 +331,30 @@ public static boolean checkExistence(String cmd) {
}
/**
* Runs a shell command in the system and provides a StringBuffer
* Runs a shell command in the system and provides a StringBuilder
* with the output of the command.
* <p>This method is deprecated. See related method that returns a CharSequence as oppposed to a StringBuffer.
*
* @param cmd an array of string that form the command to run
*
* @return a text that contains the output of the command
* @see #runCommandGeneric(String[])
* @deprecated
*/
public static StringBuffer runCommand(String[] cmd) {
return new StringBuffer(runCommandGeneric(cmd));
}
/**
* Runs a shell command in the system and provides a StringBuilder
* with the output of the command.
*
* @param cmd an array of string that form the command to run
*
* @return a StringBuffer that contains the output of the command
* @return a text that contains the output of the command
*/
public static StringBuffer runCommand(String[] cmd) {
StringBuffer retval = new StringBuffer(MAX_OUTPUT_LENGTH);
public static CharSequence runCommandGeneric(String[] cmd) {
StringBuilder retval = new StringBuilder(MAX_OUTPUT_LENGTH);
Process p;
try {
p = Runtime.getRuntime().exec(cmd);
@ -358,17 +373,30 @@ public static StringBuffer runCommand(String[] cmd) {
}
/**
* Runs a shell command in the system and provides a StringBuffer
* Runs a shell command in the system and provides a StringBuilder
* with the output of the command.
* <p>This method is deprecated in favor of the one that returns CharSequence as opposed to StringBuffer
* @param cmd the command to run
*
* @return a text that contains the output of the command
* @see #runCommandGeneric(String)
* @deprecated
*/
public static StringBuffer runCommand(String cmd) {
return new StringBuffer(runCommandGeneric(cmd));
}
/**
* Runs a shell command in the system and provides a StringBuilder
* with the output of the command.
*
* @param cmd the command to run
*
* @return a StringBuffer that contains the output of the command
* @return a text that contains the output of the command
*/
public static StringBuffer runCommand(String cmd) {
return runCommand(cmd.split("\\s+"));
public static CharSequence runCommandGeneric(String cmd) {
return runCommandGeneric(cmd.split("\\s+"));
}
/**
* Determines the greatest common divisor (GCD) of two integers.
*

View File

@ -113,7 +113,7 @@ protected Calendar parseDate(String strDate, String strTime) {
*
*/
private void findHostname() {
String startupInfo = Environment.runCommand(
String startupInfo = Environment.runCommandGeneric(
"grep --max-count=1 STARTUP_MSG:\\s*host " + file.getName()).toString();
Pattern pattern = Pattern.compile("\\s+(\\w+/.+)\\s+");
Matcher matcher = pattern.matcher(startupInfo);

View File

@ -144,11 +144,25 @@ private void append(SerializedRecord sr) {
/**
* Pack a SerializedRecord into an array of bytes
*
* <p>
* This method is deprecated.
* @param sr the SerializedRecord to be packed
* @return Packed representation fo the Serialized Record
* @see #packConcurrent(SerializedRecord)
* @deprecated
*/
public static StringBuffer pack(SerializedRecord sr) {
StringBuffer sb = new StringBuffer();
return new StringBuffer(packConcurrent(sr));
}
/**
* Pack a SerializedRecord into an array of bytes
*
* @param sr the SerializedRecord to be packed
* @return Packed representation fo the Serialized Record
*/
public static CharSequence packConcurrent(SerializedRecord sr) {
StringBuilder sb = new StringBuilder();
ArrayList<String> keys = new ArrayList<String>(sr.fields.keySet());

View File

@ -54,7 +54,7 @@ public NICParser() {
* @return the EventRecord created
*/
public EventRecord query(String device) throws UnknownHostException {
StringBuffer sb = Environment.runCommand("/sbin/ifconfig " + device);
CharSequence sb = Environment.runCommandGeneric("/sbin/ifconfig " + device);
EventRecord retval = new EventRecord(InetAddress.getLocalHost()
.getCanonicalHostName(), InetAddress.getAllByName(InetAddress.getLocalHost()
.getHostName()), Calendar.getInstance(), "NIC", "Unknown", device, "-");

View File

@ -66,12 +66,12 @@ public SMARTParser() {
*/
public EventRecord query(String device) throws Exception {
String conf = Environment.getProperty("disks." + device + ".source");
StringBuffer sb;
CharSequence sb;
if (conf == null)
sb = Environment.runCommand("sudo smartctl --all " + device);
sb = Environment.runCommandGeneric("sudo smartctl --all " + device);
else
sb = Environment.runCommand("cat " + conf);
sb = Environment.runCommandGeneric("cat " + conf);
EventRecord retval = new EventRecord(InetAddress.getLocalHost()
.getCanonicalHostName(), InetAddress.getAllByName(InetAddress.getLocalHost()
@ -146,11 +146,11 @@ public EventRecord query(String device) throws Exception {
* This format is mostly found in IDE and SATA disks.
*
* @param er the EventRecord in which to store attributes found
* @param sb the StringBuffer with the text to parse
* @param sb the text to parse
*
* @return the EventRecord in which new attributes are stored.
*/
private EventRecord readColumns(EventRecord er, StringBuffer sb) {
private EventRecord readColumns(EventRecord er, CharSequence sb) {
Pattern pattern = Pattern.compile("^\\s{0,2}(\\d{1,3}\\s+.*)$",
Pattern.MULTILINE);

View File

@ -42,10 +42,10 @@ public class SensorsParser extends ShellParser {
* @return the EventRecord created
*/
public EventRecord query(String s) throws Exception {
StringBuffer sb;
CharSequence sb;
//sb = Environment.runCommand("sensors -A");
sb = Environment.runCommand("cat sensors.out");
//sb = Environment.runCommandGeneric("sensors -A");
sb = Environment.runCommandGeneric("cat sensors.out");
EventRecord retval = new EventRecord(InetAddress.getLocalHost()
.getCanonicalHostName(), InetAddress.getAllByName(InetAddress.getLocalHost()
@ -70,7 +70,7 @@ public EventRecord query(String s) throws Exception {
*
* @return the EventRecord created
*/
private EventRecord readGroup(EventRecord er, StringBuffer sb, String prefix) {
private EventRecord readGroup(EventRecord er, CharSequence sb, String prefix) {
Pattern pattern = Pattern.compile(".*(" + prefix
+ "\\s*\\d*)\\s*:\\s*(\\+?\\d+)", Pattern.MULTILINE);

View File

@ -934,7 +934,7 @@ public boolean isIncluded(int value) {
@Override
public String toString() {
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
boolean first = true;
for(Range r: ranges) {
if (first) {
@ -1846,7 +1846,7 @@ public void setClassLoader(ClassLoader classLoader) {
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append("Configuration: ");
if(loadDefaults) {
toString(defaultResources, sb);
@ -1858,8 +1858,8 @@ public String toString() {
return sb.toString();
}
private void toString(List resources, StringBuffer sb) {
ListIterator i = resources.listIterator();
private <T> void toString(List<T> resources, StringBuilder sb) {
ListIterator<T> i = resources.listIterator();
while (i.hasNext()) {
if (i.nextIndex() != 0) {
sb.append(", ");

View File

@ -166,7 +166,7 @@ public static boolean copy(FileSystem srcFS, Path[] srcs,
throws IOException {
boolean gotException = false;
boolean returnVal = true;
StringBuffer exceptions = new StringBuffer();
StringBuilder exceptions = new StringBuilder();
if (srcs.length == 1)
return copy(srcFS, srcs[0], dstFS, dst, deleteSource, overwrite, conf);
@ -529,7 +529,7 @@ public static void unTar(File inFile, File untarDir) throws IOException {
}
}
StringBuffer untarCommand = new StringBuffer();
StringBuilder untarCommand = new StringBuilder();
boolean gzipped = inFile.toString().endsWith("gz");
if (gzipped) {
untarCommand.append(" gzip -dc '");
@ -745,7 +745,7 @@ public static int chmod(String filename, String perm
*/
public static int chmod(String filename, String perm, boolean recursive)
throws IOException, InterruptedException {
StringBuffer cmdBuf = new StringBuffer();
StringBuilder cmdBuf = new StringBuilder();
cmdBuf.append("chmod ");
if (recursive) {
cmdBuf.append("-R ");

View File

@ -238,7 +238,7 @@ public Path suffix(String suffix) {
public String toString() {
// we can't use uri.toString(), which escapes everything, because we want
// illegal characters unescaped in the string, for glob processing, etc.
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
if (uri.getScheme() != null) {
buffer.append(uri.getScheme());
buffer.append(":");

View File

@ -177,7 +177,7 @@ public boolean equals(Object right_obj) {
* Generate the stream of bytes as hex pairs separated by ' '.
*/
public String toString() {
StringBuffer sb = new StringBuffer(3*size);
StringBuilder sb = new StringBuilder(3*size);
for (int idx = 0; idx < size; idx++) {
// if not the first, put a blank separator in
if (idx != 0) {

View File

@ -183,7 +183,7 @@ public int compare(byte[] b1, int s1, int l1,
/** Returns a string representation of this object. */
public String toString() {
StringBuffer buf = new StringBuffer(MD5_LEN*2);
StringBuilder buf = new StringBuilder(MD5_LEN*2);
for (int i = 0; i < MD5_LEN; i++) {
int b = digest[i];
buf.append(HEX_DIGITS[(b >> 4) & 0xf]);

View File

@ -780,7 +780,7 @@ public int hashCode() {
}
public String toString() {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append("size: ").append(this.theMetadata.size()).append("\n");
Iterator<Map.Entry<Text, Text>> iter =
this.theMetadata.entrySet().iterator();

View File

@ -132,7 +132,7 @@ public int compareTo(Object o) {
/** Convert to a String. */
public String toString() {
StringBuffer buffer = new StringBuffer(length);
StringBuilder buffer = new StringBuilder(length);
try {
synchronized (IBUF) {
IBUF.reset(bytes, length);
@ -204,12 +204,12 @@ public static byte[] getBytes(String string) {
*/
public static String readString(DataInput in) throws IOException {
int bytes = in.readUnsignedShort();
StringBuffer buffer = new StringBuffer(bytes);
StringBuilder buffer = new StringBuilder(bytes);
readChars(in, buffer, bytes);
return buffer.toString();
}
private static void readChars(DataInput in, StringBuffer buffer, int nBytes)
private static void readChars(DataInput in, StringBuilder buffer, int nBytes)
throws IOException {
DataOutputBuffer obuf = OBUF_FACTORY.get();
obuf.reset();

View File

@ -42,14 +42,14 @@ public class CompressionCodecFactory {
private void addCodec(CompressionCodec codec) {
String suffix = codec.getDefaultExtension();
codecs.put(new StringBuffer(suffix).reverse().toString(), codec);
codecs.put(new StringBuilder(suffix).reverse().toString(), codec);
}
/**
* Print the extension map out as a string.
*/
public String toString() {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
Iterator<Map.Entry<String, CompressionCodec>> itr =
codecs.entrySet().iterator();
buf.append("{ ");
@ -112,7 +112,7 @@ public static List<Class<? extends CompressionCodec>> getCodecClasses(Configurat
*/
public static void setCodecClasses(Configuration conf,
List<Class> classes) {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
Iterator<Class> itr = classes.iterator();
if (itr.hasNext()) {
Class cls = itr.next();
@ -154,7 +154,7 @@ public CompressionCodec getCodec(Path file) {
CompressionCodec result = null;
if (codecs != null) {
String filename = file.getName();
String reversedFilename = new StringBuffer(filename).reverse().toString();
String reversedFilename = new StringBuilder(filename).reverse().toString();
SortedMap<String, CompressionCodec> subMap =
codecs.headMap(reversedFilename);
if (!subMap.isEmpty()) {

View File

@ -90,7 +90,7 @@ public void write(DataOutput out) throws IOException {
}
public String toString() {
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
buffer.append(methodName);
buffer.append("(");
for (int i = 0; i < parameters.length; i++) {

View File

@ -580,7 +580,7 @@ public int countNumOfAvailableNodes(String scope,
/** convert a network tree to a string */
public String toString() {
// print the number of racks
StringBuffer tree = new StringBuffer();
StringBuilder tree = new StringBuilder();
tree.append("Number of racks: ");
tree.append(numOfRacks);
tree.append("\n");

View File

@ -124,7 +124,7 @@ private String runResolveCommand(List<String> args) {
if (args.size() == 0) {
return null;
}
StringBuffer allOutput = new StringBuffer();
StringBuilder allOutput = new StringBuilder();
int numProcessed = 0;
if (maxArgs < MIN_ALLOWABLE_ARGS) {
LOG.warn("Invalid value " + Integer.toString(maxArgs)

View File

@ -221,7 +221,7 @@ public boolean equals(Object other) {
// inheric javadoc
public String toString() {
StringBuffer sb = new StringBuffer(2*count);
StringBuilder sb = new StringBuilder(2*count);
for(int idx = 0; idx < count; idx++) {
sb.append(Character.forDigit((bytes[idx] & 0xF0) >> 4, 16));
sb.append(Character.forDigit(bytes[idx] & 0x0F, 16));

View File

@ -51,7 +51,7 @@ private void throwExceptionOnError(String tag) throws IOException {
private String readField(String tag) throws IOException {
try {
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
while (true) {
char c = (char) stream.read();
switch (c) {

View File

@ -45,7 +45,7 @@ private Utils() {
* @return
*/
static String toXMLString(String s) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (int idx = 0; idx < s.length(); idx++) {
char ch = s.charAt(idx);
if (ch == '<') {
@ -86,7 +86,7 @@ static private int h2c(char ch) {
* @return
*/
static String fromXMLString(String s) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
for (int idx = 0; idx < s.length();) {
char ch = s.charAt(idx++);
if (ch == '%') {
@ -109,7 +109,7 @@ static String fromXMLString(String s) {
* @return
*/
static String toCSVString(String s) {
StringBuffer sb = new StringBuffer(s.length()+1);
StringBuilder sb = new StringBuilder(s.length()+1);
sb.append('\'');
int len = s.length();
for (int i = 0; i < len; i++) {
@ -151,7 +151,7 @@ static String fromCSVString(String s) throws IOException {
throw new IOException("Error deserializing string.");
}
int len = s.length();
StringBuffer sb = new StringBuffer(len-1);
StringBuilder sb = new StringBuilder(len-1);
for (int i = 1; i < len; i++) {
char c = s.charAt(i);
if (c == '%') {
@ -214,7 +214,7 @@ static Buffer fromXMLBuffer(String s)
* @return
*/
static String toCSVBuffer(Buffer buf) {
StringBuffer sb = new StringBuffer("#");
StringBuilder sb = new StringBuilder("#");
sb.append(buf.toString());
return sb.toString();
}

View File

@ -41,7 +41,7 @@ public class XmlRecordOutput implements RecordOutput {
private Stack<String> compoundStack;
private void putIndent() {
StringBuffer sb = new StringBuffer("");
StringBuilder sb = new StringBuilder("");
for (int idx = 0; idx < indent; idx++) {
sb.append(" ");
}

View File

@ -782,7 +782,7 @@ public JRecord(String name, ArrayList<JField<JType>> flist) {
// precompute signature
int idx = name.lastIndexOf('.');
String recName = name.substring(idx+1);
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append("L").append(recName).append("(");
for (Iterator<JField<JType>> i = flist.iterator(); i.hasNext();) {
String s = i.next().getType().getSignature();

View File

@ -211,12 +211,12 @@ public synchronized void setStatus(String status) {
}
public String toString() {
StringBuffer result = new StringBuffer();
StringBuilder result = new StringBuilder();
toString(result);
return result.toString();
}
private synchronized void toString(StringBuffer buffer) {
private synchronized void toString(StringBuilder buffer) {
buffer.append(status);
if (phases.size() != 0 && currentPhase < phases.size()) {
buffer.append(" > ");

View File

@ -128,7 +128,7 @@ public static String formatPercent(double done, int digits) {
public static String arrayToString(String[] strs) {
if (strs.length == 0) { return ""; }
StringBuffer sbuf = new StringBuffer();
StringBuilder sbuf = new StringBuilder();
sbuf.append(strs[0]);
for (int idx = 1; idx < strs.length; idx++) {
sbuf.append(",");
@ -183,7 +183,7 @@ public static String uriToString(URI[] uris){
if (uris == null) {
return null;
}
StringBuffer ret = new StringBuffer(uris[0].toString());
StringBuilder ret = new StringBuilder(uris[0].toString());
for(int i = 1; i < uris.length;i++){
ret.append(",");
ret.append(uris[i].toString());
@ -247,7 +247,7 @@ public static String formatTimeDiff(long finishTime, long startTime){
* @param timeDiff The time difference to format
*/
public static String formatTime(long timeDiff){
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
long hours = timeDiff / (60*60*1000);
long rem = (timeDiff % (60*60*1000));
long minutes = rem / (60*1000);
@ -279,7 +279,7 @@ public static String formatTime(long timeDiff){
*/
public static String getFormattedTimeWithDiff(DateFormat dateFormat,
long finishTime, long startTime){
StringBuffer buf = new StringBuffer();
StringBuilder buf = new StringBuilder();
if (0 != finishTime) {
buf.append(dateFormat.format(new Date(finishTime)));
if (0 != startTime){
@ -533,7 +533,7 @@ public static String unEscapeString(String str, char escapeChar,
* @return a message for logging
*/
private static String toStartupShutdownString(String prefix, String [] msg) {
StringBuffer b = new StringBuffer(prefix);
StringBuilder b = new StringBuilder(prefix);
b.append("\n/************************************************************");
for(String s : msg)
b.append("\n" + prefix + s);
@ -645,7 +645,7 @@ public static String escapeHTML(String string) {
if(string == null) {
return null;
}
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
boolean lastCharacterWasSpace = false;
char[] chars = string.toCharArray();
for(char c : chars) {
@ -708,7 +708,7 @@ public static synchronized String limitDecimalTo2(double d) {
* @param strings Strings to join.
*/
public static String join(CharSequence separator, Iterable<String> strings) {
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
boolean first = true;
for (String s : strings) {
if (first) {

View File

@ -64,7 +64,7 @@ public boolean equals(Object o) {
/** Returns a string representation of this object. */
public String toString() {
StringBuffer buf = new StringBuffer(length*2);
StringBuilder buf = new StringBuilder(length*2);
for (int i = 0; i < length; i++) {
int b = data[i];
buf.append(HEX_DIGITS[(b >> 4) & 0xf]);

View File

@ -35,7 +35,7 @@ public class TestText extends TestCase {
// generate a valid java String
private static String getTestString(int len) throws Exception {
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
int length = (len==RAND_LEN) ? RANDOM.nextInt(1000) : len;
while (buffer.length()<length) {
int codePoint = RANDOM.nextInt(Character.MAX_CODE_POINT);
@ -60,7 +60,7 @@ public static String getTestString() throws Exception {
public static String getLongString() throws Exception {
String str = getTestString();
int length = Short.MAX_VALUE+str.length();
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
while(buffer.length()<length)
buffer.append(str);

View File

@ -29,7 +29,7 @@ public class TestUTF8 extends TestCase {
private static final Random RANDOM = new Random();
public static String getTestString() throws Exception {
StringBuffer buffer = new StringBuffer();
StringBuilder buffer = new StringBuilder();
int length = RANDOM.nextInt(100);
for (int i = 0; i < length; i++) {
buffer.append((char)(RANDOM.nextInt(Character.MAX_VALUE)));

View File

@ -294,7 +294,7 @@ public WritableSortable(int j) throws IOException {
seed = r.nextLong();
r.setSeed(seed);
Text t = new Text();
StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
indices = new int[j];
offsets = new int[j];
check = new String[j];
@ -315,7 +315,7 @@ public long getSeed() {
return seed;
}
private static void genRandom(Text t, int len, StringBuffer sb) {
private static void genRandom(Text t, int len, StringBuilder sb) {
sb.setLength(0);
for (int i = 0; i < len; ++i) {
sb.append(Integer.toString(r.nextInt(26) + 10, 36));