JDK-6837325 : URLEncoder and URLDecoder classes should use StringBuilder, not StringBuffer
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6u10
  • Priority: P5
  • Status: Open
  • Resolution: Unresolved
  • OS: windows_vista
  • CPU: x86
  • Submitted: 2009-05-05
  • Updated: 2011-02-16
Description
FULL PRODUCT VERSION :
All versions of JDK SE 1.5/5.0 and 1.6/6.0

ADDITIONAL OS VERSION INFORMATION :
N/A, OS-generic

A DESCRIPTION OF THE PROBLEM :
The java.net.URLEncoder and URLDecoder classes could still use some performance improvements.

Particularly, they are using synchronized classes when no synchronization is needed.  (The instances are internal to the static encode / decode methods.)  Uses of StringBuffer should be replaced with StringBuilder, introduced in Java 1.5.

URLEncoder also uses CharArrayWriter, which adds additional and unnecessary synchronization.  Also, as the length of the input String is provided as a hint to the StringBuffer to prevent unnecessary resizing, the same should be done for the CharArrayWriter (or its replacement).

The synchronization issues are more applicable to Java 1.5 than 1.6, due to the introduction of the escape analysis in 1.6.  However, they should still replaced with non-synchronized versions for good practice and for Java 1.5 users.

Finally, overloaded methods should be provided that allow the passing-in of existing Charset instances, rather than just a String that is passed to Charset.forName() each time.  (Consider when multiple encode/decode calls need to be made sequentially.)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
These classes demonstrate less-than-possible performance always.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
These classes should demonstrate better performance by addressing the items listed in the description.
ACTUAL -
These classes demonstrate less-than-possible performance always.

REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
Using an alternate implementation as a replacement for URLEncoder and URLDecoder.