Inefficient use StringBuffer or StringBuilder[Optimization|clean-up] {Package: sun.*}

Otávio Gonçalves de Santana otaviojava at java.net
Fri Jun 7 22:27:50 UTC 2013


I did one more refactoring in this packages.

diff --git a/src/share/classes/sun/management/Agent.java
b/src/share/classes/sun/management/Agent.java
--- a/src/share/classes/sun/management/Agent.java
+++ b/src/share/classes/sun/management/Agent.java
@@ -497,7 +497,7 @@
         } else {
             StringBuffer message = new StringBuffer(params[0]);
             for (int i = 1; i < params.length; i++) {
-                message.append(" " + params[i]);
+                message.append(' ').append(params[i]);
             }
             error(key, message.toString());
         }




diff --git a/src/share/classes/sun/reflect/annotation/AnnotationType.java
b/src/share/classes/sun/reflect/annotation/AnnotationType.java
--- a/src/share/classes/sun/reflect/annotation/AnnotationType.java
+++ b/src/share/classes/sun/reflect/annotation/AnnotationType.java
@@ -205,11 +205,11 @@
      * For debugging.
      */
     public String toString() {
-        StringBuffer s = new StringBuffer("Annotation Type:" + "\n");
-        s.append("   Member types: " + memberTypes + "\n");
-        s.append("   Member defaults: " + memberDefaults + "\n");
-        s.append("   Retention policy: " + retention + "\n");
-        s.append("   Inherited: " + inherited);
+        StringBuffer s = new StringBuffer("Annotation Type:").append('\n');
+        s.append("   Member types: " ).append( memberTypes ).append( '\n');
+        s.append("   Member defaults: " ).append( memberDefaults ).append(
'\n');
+        s.append("   Retention policy: " ).append( retention ).append(
'\n');
+        s.append("   Inherited: " ).append( inherited);
         return s.toString();
     }
 }




diff --git a/src/share/classes/sun/font/Decoration.java
b/src/share/classes/sun/font/Decoration.java
--- a/src/share/classes/sun/font/Decoration.java
+++ b/src/share/classes/sun/font/Decoration.java
@@ -430,12 +430,12 @@
             StringBuffer buf = new StringBuffer();
             buf.append(super.toString());
             buf.append("[");
-            if (fgPaint != null) buf.append("fgPaint: " + fgPaint);
-            if (bgPaint != null) buf.append(" bgPaint: " + bgPaint);
+            if (fgPaint != null) buf.append("fgPaint: ").append(fgPaint);
+            if (bgPaint != null) buf.append(" bgPaint: ").append(bgPaint);
             if (swapColors) buf.append(" swapColors: true");
             if (strikethrough) buf.append(" strikethrough: true");
-            if (stdUnderline != null) buf.append(" stdUnderline: " +
stdUnderline);
-            if (imUnderline != null) buf.append(" imUnderline: " +
imUnderline);
+            if (stdUnderline != null) buf.append(" stdUnderline:
").append(stdUnderline);
+            if (imUnderline != null) buf.append(" imUnderline:
").append(imUnderline);
             buf.append("]");
             return buf.toString();
         }




diff --git a/src/share/classes/sun/net/www/MimeEntry.java
b/src/share/classes/sun/net/www/MimeEntry.java
--- a/src/share/classes/sun/net/www/MimeEntry.java
+++ b/src/share/classes/sun/net/www/MimeEntry.java
@@ -297,7 +297,7 @@
             if (needSeparator) {
                 buf.append(separator);
             }
-            buf.append("application=" + command);
+            buf.append("application=").append(command);
             needSeparator = true;
         }

@@ -305,7 +305,7 @@
             if (needSeparator) {
                 buf.append(separator);
             }
-            buf.append("icon=" + getImageFileName());
+            buf.append("icon=").append(getImageFileName());
             needSeparator = true;
         }

@@ -314,7 +314,7 @@
             if (needSeparator) {
                 buf.append(separator);
             }
-            buf.append("file_extensions=" + extensions);
+            buf.append("file_extensions=").append(extensions);
             needSeparator = true;
         }

@@ -323,7 +323,7 @@
             if (needSeparator) {
                 buf.append(separator);
             }
-            buf.append("description=" + description);
+            buf.append("description=" ).append(description);
         }

         return buf.toString();




diff --git a/src/share/classes/sun/tools/jstatd/RemoteHostImpl.java
b/src/share/classes/sun/tools/jstatd/RemoteHostImpl.java
--- a/src/share/classes/sun/tools/jstatd/RemoteHostImpl.java
+++ b/src/share/classes/sun/tools/jstatd/RemoteHostImpl.java
@@ -68,7 +68,7 @@

         sb.append("local://").append(lvmid).append("@localhost");
         if (mode != null) {
-            sb.append("?mode=" + mode);
+            sb.append("?mode=").append(mode);
         }

         String vmidStr = sb.toString();




diff --git a/src/share/classes/sun/font/StandardGlyphVector.java
b/src/share/classes/sun/font/StandardGlyphVector.java
--- a/src/share/classes/sun/font/StandardGlyphVector.java
+++ b/src/share/classes/sun/font/StandardGlyphVector.java
@@ -1894,7 +1894,7 @@
             }
         }
         catch(Exception e) {
-            buf.append(" " + e.getMessage());
+            buf.append(' ').append(e.getMessage());
         }
         buf.append("}");





diff --git a/src/share/classes/sun/awt/datatransfer/SunClipboard.java
b/src/share/classes/sun/awt/datatransfer/SunClipboard.java
--- a/src/share/classes/sun/awt/datatransfer/SunClipboard.java
+++ b/src/share/classes/sun/awt/datatransfer/SunClipboard.java
@@ -87,7 +87,7 @@

     public SunClipboard(String name) {
         super(name);
-        CLIPBOARD_FLAVOR_LISTENER_KEY = new StringBuffer(name +
"_CLIPBOARD_FLAVOR_LISTENER_KEY");
+        CLIPBOARD_FLAVOR_LISTENER_KEY = new
StringBuffer(name).append("_CLIPBOARD_FLAVOR_LISTENER_KEY");
     }

     public synchronized void setContents(Transferable contents,







diff --git a/src/share/classes/sun/security/acl/AclEntryImpl.java
b/src/share/classes/sun/security/acl/AclEntryImpl.java
--- a/src/share/classes/sun/security/acl/AclEntryImpl.java
+++ b/src/share/classes/sun/security/acl/AclEntryImpl.java
@@ -147,7 +147,7 @@
             s.append("Group.");
         else
             s.append("User.");
-        s.append(user + "=");
+        s.append(user).append('=');
         Enumeration<Permission> e = permissions();
         while(e.hasMoreElements()) {
             Permission p = e.nextElement();




diff --git
a/src/share/classes/sun/security/krb5/internal/crypto/dk/DkCrypto.java
b/src/share/classes/sun/security/krb5/internal/crypto/dk/DkCrypto.java
--- a/src/share/classes/sun/security/krb5/internal/crypto/dk/DkCrypto.java
+++ b/src/share/classes/sun/security/krb5/internal/crypto/dk/DkCrypto.java
@@ -640,7 +640,7 @@

         for (int i = 0; i < digest.length; i++) {
             if ((digest[i] & 0x000000ff) < 0x10) {
-                digestString.append("0" +
+                digestString.append('0').append(
                     Integer.toHexString(digest[i] & 0x000000ff));
             } else {
                 digestString.append(




diff --git a/src/share/classes/sun/security/ssl/HandshakeMessage.java
b/src/share/classes/sun/security/ssl/HandshakeMessage.java
--- a/src/share/classes/sun/security/ssl/HandshakeMessage.java
+++ b/src/share/classes/sun/security/ssl/HandshakeMessage.java
@@ -1478,7 +1478,7 @@
                 boolean opened = false;
                 for (SignatureAndHashAlgorithm signAlg : algorithms) {
                     if (opened) {
-                        buffer.append(", " + signAlg.getAlgorithmName());
+                        buffer.append(",
").append(signAlg.getAlgorithmName());
                     } else {
                         buffer.append(signAlg.getAlgorithmName());
                         opened = true;




diff --git a/src/share/classes/sun/security/krb5/KrbException.java
b/src/share/classes/sun/security/krb5/KrbException.java
--- a/src/share/classes/sun/security/krb5/KrbException.java
+++ b/src/share/classes/sun/security/krb5/KrbException.java
@@ -96,7 +96,7 @@


     public String krbErrorMessage() {
-        StringBuffer strbuf = new StringBuffer("krb_error " + returnCode);
+        StringBuffer strbuf = new StringBuffer("krb_error
").append(returnCode);
         String msg =  getMessage();
         if (msg != null) {
             strbuf.append(" ");




diff --git a/src/share/classes/sun/security/x509/PolicyInformation.java
b/src/share/classes/sun/security/x509/PolicyInformation.java
--- a/src/share/classes/sun/security/x509/PolicyInformation.java
+++ b/src/share/classes/sun/security/x509/PolicyInformation.java
@@ -258,8 +258,8 @@
      * Return a printable representation of the PolicyInformation.
      */
     public String toString() {
-        StringBuilder s = new StringBuilder("  [" +
policyIdentifier.toString());
-        s.append(policyQualifiers + "  ]\n");
+        StringBuilder s = new StringBuilder("  [").append(
policyIdentifier.toString());
+        s.append(policyQualifiers).append("  ]\n");
         return s.toString();
     }





diff --git
a/src/share/classes/sun/security/pkcs/SigningCertificateInfo.java
b/src/share/classes/sun/security/pkcs/SigningCertificateInfo.java
--- a/src/share/classes/sun/security/pkcs/SigningCertificateInfo.java
+++ b/src/share/classes/sun/security/pkcs/SigningCertificateInfo.java
@@ -158,8 +158,8 @@
         }
         buffer.append(hexDumper.encode(certHash));
         if (issuer != null && serialNumber != null) {
-            buffer.append("\n\tIssuer: " + issuer + "\n");
-            buffer.append("\t" + serialNumber);
+            buffer.append("\n\tIssuer: " ).append( issuer ).append( '\n');
+            buffer.append('\t').append( serialNumber);
         }
         buffer.append("\n]");
         return buffer.toString();


On Fri, Jun 7, 2013 at 10:22 AM, Otávio Gonçalves de Santana <
otaviojava at java.net> wrote:

> Inefficient use StringBuffer or StringBuilder[Optimization|clean-up]
>
> The string addition in the parameter will create another string buffer,
> append all the components, then convert that to a string so the you can be
> appended to your string buffer.
>
> So I replace this:
>
> sb.append("    xxx:   [" + getXXX() + "]\n");
>
> for this:
>
> sb.append("    xxx:   [").append(getXXX()).append("]\n");
>
> The classes are:
>
>
>  -- sun
> sun.awt.datatransfer.SunClipboard
> sun.font.Decoration
> sun.font.StandardGlyphVector
> sun.management.Agent
> sun.net.www.MimeEntry
> sun.reflect.annotation.AnnotationType
> sun.tools.jstatd.RemoteHostImpl
>
>
> sun.security.acl.AclEntryImpl
> sun.security.krb5.internal.crypto.dk.DkCrypto
> sun.security.krb5.KrbException
> sun.security.pkcs.SigningCertificateInfo
> sun.security.ssl.HandshakeMessage
> sun.security.x509.PolicyInformation
> sun.security.x509.X509CRLImpl
>
>
> The diff bellow:
>
>
> diff --git a/src/share/classes/sun/management/Agent.java
> b/src/share/classes/sun/management/Agent.java
> --- a/src/share/classes/sun/management/Agent.java
> +++ b/src/share/classes/sun/management/Agent.java
> @@ -497,7 +497,7 @@
>          } else {
>              StringBuffer message = new StringBuffer(params[0]);
>              for (int i = 1; i < params.length; i++) {
> -                message.append(" " + params[i]);
> +                message.append(" ").append(params[i]);
>              }
>              error(key, message.toString());
>          }
>
>
>
>
> diff --git a/src/share/classes/sun/reflect/annotation/AnnotationType.java
> b/src/share/classes/sun/reflect/annotation/AnnotationType.java
> --- a/src/share/classes/sun/reflect/annotation/AnnotationType.java
> +++ b/src/share/classes/sun/reflect/annotation/AnnotationType.java
> @@ -205,11 +205,11 @@
>       * For debugging.
>       */
>      public String toString() {
> -        StringBuffer s = new StringBuffer("Annotation Type:" + "\n");
> -        s.append("   Member types: " + memberTypes + "\n");
> -        s.append("   Member defaults: " + memberDefaults + "\n");
> -        s.append("   Retention policy: " + retention + "\n");
> -        s.append("   Inherited: " + inherited);
> +        StringBuffer s = new StringBuffer("Annotation
> Type:").append("\n");
> +        s.append("   Member types: " ).append( memberTypes ).append(
> "\n");
> +        s.append("   Member defaults: " ).append( memberDefaults
> ).append( "\n");
> +        s.append("   Retention policy: " ).append( retention ).append(
> "\n");
> +        s.append("   Inherited: " ).append( inherited);
>          return s.toString();
>      }
>  }
>
>
>
>
> diff --git a/src/share/classes/sun/font/Decoration.java
> b/src/share/classes/sun/font/Decoration.java
> --- a/src/share/classes/sun/font/Decoration.java
> +++ b/src/share/classes/sun/font/Decoration.java
> @@ -430,12 +430,12 @@
>              StringBuffer buf = new StringBuffer();
>              buf.append(super.toString());
>              buf.append("[");
> -            if (fgPaint != null) buf.append("fgPaint: " + fgPaint);
> -            if (bgPaint != null) buf.append(" bgPaint: " + bgPaint);
> +            if (fgPaint != null) buf.append("fgPaint: ").append(fgPaint);
> +            if (bgPaint != null) buf.append(" bgPaint: ").append(bgPaint);
>              if (swapColors) buf.append(" swapColors: true");
>              if (strikethrough) buf.append(" strikethrough: true");
> -            if (stdUnderline != null) buf.append(" stdUnderline: " +
> stdUnderline);
> -            if (imUnderline != null) buf.append(" imUnderline: " +
> imUnderline);
> +            if (stdUnderline != null) buf.append(" stdUnderline:
> ").append(stdUnderline);
> +            if (imUnderline != null) buf.append(" imUnderline:
> ").append(imUnderline);
>              buf.append("]");
>              return buf.toString();
>          }
>
>
>
>
> diff --git a/src/share/classes/sun/net/www/MimeEntry.java
> b/src/share/classes/sun/net/www/MimeEntry.java
> --- a/src/share/classes/sun/net/www/MimeEntry.java
> +++ b/src/share/classes/sun/net/www/MimeEntry.java
> @@ -297,7 +297,7 @@
>              if (needSeparator) {
>                  buf.append(separator);
>              }
> -            buf.append("application=" + command);
> +            buf.append("application=").append(command);
>              needSeparator = true;
>          }
>
> @@ -305,7 +305,7 @@
>              if (needSeparator) {
>                  buf.append(separator);
>              }
> -            buf.append("icon=" + getImageFileName());
> +            buf.append("icon=").append(getImageFileName());
>              needSeparator = true;
>          }
>
> @@ -314,7 +314,7 @@
>              if (needSeparator) {
>                  buf.append(separator);
>              }
> -            buf.append("file_extensions=" + extensions);
> +            buf.append("file_extensions=").append(extensions);
>              needSeparator = true;
>          }
>
> @@ -323,7 +323,7 @@
>              if (needSeparator) {
>                  buf.append(separator);
>              }
> -            buf.append("description=" + description);
> +            buf.append("description=" ).append(description);
>          }
>
>          return buf.toString();
>
>
>
>
> diff --git a/src/share/classes/sun/tools/jstatd/RemoteHostImpl.java
> b/src/share/classes/sun/tools/jstatd/RemoteHostImpl.java
> --- a/src/share/classes/sun/tools/jstatd/RemoteHostImpl.java
> +++ b/src/share/classes/sun/tools/jstatd/RemoteHostImpl.java
> @@ -68,7 +68,7 @@
>
>          sb.append("local://").append(lvmid).append("@localhost");
>          if (mode != null) {
> -            sb.append("?mode=" + mode);
> +            sb.append("?mode=").append(mode);
>          }
>
>          String vmidStr = sb.toString();
>
>
>
>
> diff --git a/src/share/classes/sun/font/StandardGlyphVector.java
> b/src/share/classes/sun/font/StandardGlyphVector.java
> --- a/src/share/classes/sun/font/StandardGlyphVector.java
> +++ b/src/share/classes/sun/font/StandardGlyphVector.java
> @@ -1894,7 +1894,7 @@
>              }
>          }
>          catch(Exception e) {
> -            buf.append(" " + e.getMessage());
> +            buf.append(" ").append(e.getMessage());
>          }
>          buf.append("}");
>
>
>
>
>
> diff --git a/src/share/classes/sun/awt/datatransfer/SunClipboard.java
> b/src/share/classes/sun/awt/datatransfer/SunClipboard.java
> --- a/src/share/classes/sun/awt/datatransfer/SunClipboard.java
> +++ b/src/share/classes/sun/awt/datatransfer/SunClipboard.java
> @@ -87,7 +87,7 @@
>
>      public SunClipboard(String name) {
>          super(name);
> -        CLIPBOARD_FLAVOR_LISTENER_KEY = new StringBuffer(name +
> "_CLIPBOARD_FLAVOR_LISTENER_KEY");
> +        CLIPBOARD_FLAVOR_LISTENER_KEY = new
> StringBuffer(name).append("_CLIPBOARD_FLAVOR_LISTENER_KEY");
>      }
>
>      public synchronized void setContents(Transferable contents,
>
>
>
>
>
>
>
> diff --git a/src/share/classes/sun/security/acl/AclEntryImpl.java
> b/src/share/classes/sun/security/acl/AclEntryImpl.java
> --- a/src/share/classes/sun/security/acl/AclEntryImpl.java
> +++ b/src/share/classes/sun/security/acl/AclEntryImpl.java
> @@ -147,7 +147,7 @@
>              s.append("Group.");
>          else
>              s.append("User.");
> -        s.append(user + "=");
> +        s.append(user).append("=");
>          Enumeration<Permission> e = permissions();
>          while(e.hasMoreElements()) {
>              Permission p = e.nextElement();
>
>
>
>
> diff --git
> a/src/share/classes/sun/security/krb5/internal/crypto/dk/DkCrypto.java
> b/src/share/classes/sun/security/krb5/internal/crypto/dk/DkCrypto.java
> --- a/src/share/classes/sun/security/krb5/internal/crypto/dk/DkCrypto.java
> +++ b/src/share/classes/sun/security/krb5/internal/crypto/dk/DkCrypto.java
> @@ -640,7 +640,7 @@
>
>          for (int i = 0; i < digest.length; i++) {
>              if ((digest[i] & 0x000000ff) < 0x10) {
> -                digestString.append("0" +
> +                digestString.append("0").append(
>                      Integer.toHexString(digest[i] & 0x000000ff));
>              } else {
>                  digestString.append(
>
>
>
>
> diff --git a/src/share/classes/sun/security/ssl/HandshakeMessage.java
> b/src/share/classes/sun/security/ssl/HandshakeMessage.java
> --- a/src/share/classes/sun/security/ssl/HandshakeMessage.java
> +++ b/src/share/classes/sun/security/ssl/HandshakeMessage.java
> @@ -1478,7 +1478,7 @@
>                  boolean opened = false;
>                  for (SignatureAndHashAlgorithm signAlg : algorithms) {
>                      if (opened) {
> -                        buffer.append(", " + signAlg.getAlgorithmName());
> +                        buffer.append(",
> ").append(signAlg.getAlgorithmName());
>                      } else {
>                          buffer.append(signAlg.getAlgorithmName());
>                          opened = true;
>
>
>
>
> diff --git a/src/share/classes/sun/security/krb5/KrbException.java
> b/src/share/classes/sun/security/krb5/KrbException.java
> --- a/src/share/classes/sun/security/krb5/KrbException.java
> +++ b/src/share/classes/sun/security/krb5/KrbException.java
> @@ -96,7 +96,7 @@
>
>
>      public String krbErrorMessage() {
> -        StringBuffer strbuf = new StringBuffer("krb_error " + returnCode);
> +        StringBuffer strbuf = new StringBuffer("krb_error
> ").append(returnCode);
>          String msg =  getMessage();
>          if (msg != null) {
>              strbuf.append(" ");
>
>
>
>
> diff --git a/src/share/classes/sun/security/x509/PolicyInformation.java
> b/src/share/classes/sun/security/x509/PolicyInformation.java
> --- a/src/share/classes/sun/security/x509/PolicyInformation.java
> +++ b/src/share/classes/sun/security/x509/PolicyInformation.java
> @@ -258,8 +258,8 @@
>       * Return a printable representation of the PolicyInformation.
>       */
>      public String toString() {
> -        StringBuilder s = new StringBuilder("  [" +
> policyIdentifier.toString());
> -        s.append(policyQualifiers + "  ]\n");
> +        StringBuilder s = new StringBuilder("  [").append(
> policyIdentifier.toString());
> +        s.append(policyQualifiers).append("  ]\n");
>          return s.toString();
>      }
>
>
>
>
>
> diff --git
> a/src/share/classes/sun/security/pkcs/SigningCertificateInfo.java
> b/src/share/classes/sun/security/pkcs/SigningCertificateInfo.java
> --- a/src/share/classes/sun/security/pkcs/SigningCertificateInfo.java
> +++ b/src/share/classes/sun/security/pkcs/SigningCertificateInfo.java
> @@ -158,8 +158,8 @@
>          }
>          buffer.append(hexDumper.encode(certHash));
>          if (issuer != null && serialNumber != null) {
> -            buffer.append("\n\tIssuer: " + issuer + "\n");
> -            buffer.append("\t" + serialNumber);
> +            buffer.append("\n\tIssuer: " ).append( issuer ).append( "\n");
> +            buffer.append("\t").append( serialNumber);
>          }
>          buffer.append("\n]");
>          return buffer.toString();
>
>
>
>
>
>
>
> --
> Atenciosamente.
>
> Otávio Gonçalves de Santana
>
> blog:     http://otaviosantana.blogspot.com.br/
> twitter: http://twitter.com/otaviojava
> site:     http://www.otaviojava.com.br
> (11)     98255-3513
>
>


-- 
Atenciosamente.

Otávio Gonçalves de Santana

blog:     http://otaviosantana.blogspot.com.br/
twitter: http://twitter.com/otaviojava
site:     http://www.otaviojava.com.br
(11)     98255-3513



More information about the core-libs-dev mailing list