Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7871387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:53:28+00:00 2026-06-03T01:53:28+00:00

I often have to url encode or decode a large collection or array of

  • 0

I often have to url encode or decode a large collection or array of strings. Besides iterating through them and using the static URLDecoder.decode(string, “UTF-8”), are there any libraries out there that will make this type of operation more performant?

A colleague insists that using the static method to decode the strings in-place is not thread safe. Why would that be?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T01:53:31+00:00Added an answer on June 3, 2026 at 1:53 am

    The JDK URLDecoder wasn’t implemented efficiently. Most notably, internally it relies on StringBuffer (which unnecessarily introduces synchronization in the case of URLDecoder). The Apache commons provides URLCodec, but it has also been reported to have similar issues in regards to performance but I haven’t verified that’s still the case in most recent version.

    Mark A. Ziesemer wrote a post a while back regarding the issues and performance with URLDecoder. He logged some bug reports and ended up writing a complete replacement. Because this is SO, I’ll quote some key excerpts here, but you should really read the entire source article here: http://blogger.ziesemer.com/2009/05/improving-url-coder-performance-java.html

    Selected quotes:

    Java provides a default implementation of this functionality in
    java.net.URLEncoder and java.net.URLDecoder. Unfortunately, it is not
    the best performing, due to both how the API was written as well as
    details within the implementation. A number of performance-related
    bugs have been filed on sun.com in relation to URLEncoder.

    There is an alternative: org.apache.commons.codec.net.URLCodec from
    Apache Commons Codec. (Commons Codec also provides a useful
    implementation for Base64 encoding.) Unfortunately, Commons’ URLCodec
    suffers some of the same issues as Java’s URLEncoder/URLDecoder.

    …

    Recommendations for both the JDK and Commons:

    When constructing any of the “buffer” classes, e.g.
    ByteArrayOutputStream, CharArrayWriter, StringBuilder, or
    StringBuffer, estimate and pass-in an estimated capacity. The JDK’s
    URLEncoder currently does this for its StringBuffer, but should do
    this for its CharArrayWriter instance as well. Common’s URLCodec
    should do this for its ByteArrayOutputStream instance. If the classes’
    default buffer sizes are too small, they may have to resize by copying
    into new, larger buffers – which isn’t exactly a “cheap” operation. If
    the classes’ default buffer sizes are too large, memory may be
    unnecessarily wasted.

    Both implementations are dependent on Charsets, but only accept them
    as their String name. Charset provides a simple and small cache for
    name lookups – storing only the last 2 Charsets used. This should not
    be relied upon, and both should accept Charset instances for other
    interoperability reasons as well.

    Both implementations only handle fixed-size inputs and outputs. The
    JDK’s URLEncoder only works with String instances. Commons’ URLCodec
    is also based on Strings, but also works with byte[] arrays. This is a
    design-level constraint that essentially prevents efficient processing
    of larger or variable-length inputs. Instead, the “stream-supporting”
    interfaces such as CharSequence, Appendable, and java.nio’s Buffer
    implementations of ByteBuffer and CharBuffer should be supported.

    …

    Note that com.ziesemer.utils.urlCodec is over 3x as fast as the JDK
    URLEncoder, and over 1.5x as fast as the JDK URLDecoder. (The JDK’s
    URLDecoder was faster than the URLEncoder, so there wasn’t as much
    room for improvement.)

    I think your colleague is wrong to suggest URLDecode is not thread-safe. Other answers here explain in detail.

    EDIT [2012-07-03] – Per later comment posted by OP

    Not sure if you were looking for more ideas or not? You are correct that if you intend to operate on the list as an atomic collection, then you would have to synchronize all access to the list, including references outside of your method. However, if you are okay with the returned list contents potentially differing from the original list, then a brute force approach for operating on a “batch” of strings from a collection that might be modified by other threads could look something like this:

    /**
     * @param origList will be copied by this method so that origList can continue
     *                 to be read/write by other threads. 
     * @return list containing  decoded strings for each entry that was 
               in origList at time of copy.
     */
    public List<String> decodeListOfStringSafely(List<String> origList)
            throws UnsupportedEncodingException {
        List<String> snapshotList = new ArrayList<String>(origList);
        List<String> newList  = new ArrayList<String>(); 
    
        for (String urlStr : snapshotList) {
          String decodedUrlStr  = URLDecoder.decode(urlStr, "UTF8");
              newList.add(decodedUrlStr);
        }
    
        return newList;
    }
    

    If that does not help, then I’m still not sure what you are after and you would be better served to create a new, more concise, question. If that is what you were asking about, then be careful because this example out of context is not a good idea for many reasons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Squirrel SQL with Oracle. I often have to write quick queries for
Using C# for ASP.NET and MOSS development, we often have to embed JavaScript into
I have an R function which calls a webserver database through it's API using
When I am communicating with my backend I often have URL like this: http://myserver.com/api/endpoint?param1={PARAM1}&param2={PARAM2}
I often have to deal with XML documents that contain namespaced elements, but doesn't
I have found that I often have to implement some sort of a scheduler
Python and Matlab quite often have integer date representations as follows: 733828.0 733829.0 733832.0
When I have a controller, e.g. article I often have a action_view() that handles
When creating gems, I often have a directory structure like this: |--lib |-- helpers.rb
As an Actionscript programmer shifting to JS/jQuery I often have to author multipage apps

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.