Class UrlEncoder


  • public class UrlEncoder
    extends java.lang.Object
    Adapted from java.net.URLEncoder, but defines instances for query string encoding versus URL path component encoding.

    The difference is important because a space is encoded as a + in a query string, but this is a valid value in a path component (and is therefore not decode back to a space).

    Author:
    Doug Donohoe
    See Also:
    URLEncoder, RFC-2396
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  UrlEncoder.Type
      encoder types
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected static int caseDiff  
      protected java.util.BitSet dontNeedEncoding
      List of what not to encode, i.e.
      static UrlEncoder HEADER_INSTANCE
      Encoder used to encode a header.
      static UrlEncoder PATH_INSTANCE
      Encoder used to encode segments of a path.

      For example: http://org.acme/foo/thispart/orthispart?butnot=thispart
      static UrlEncoder QUERY_INSTANCE
      Encoder used to encode name or value components of a query string.

      For example: http://org.acme/notthis/northis/oreventhis?buthis=isokay&asis=thispart
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected UrlEncoder​(UrlEncoder.Type type)
      Allow subclass to call constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String encode​(java.lang.String unsafeInput, java.lang.String charsetName)  
      java.lang.String encode​(java.lang.String s, java.nio.charset.Charset charset)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • dontNeedEncoding

        protected java.util.BitSet dontNeedEncoding
        List of what not to encode, i.e. characters (e.g. A-Z) and other allowed signs (e.g. !) that are allowed but don't have a special meaning.
      • QUERY_INSTANCE

        public static final UrlEncoder QUERY_INSTANCE
        Encoder used to encode name or value components of a query string.

        For example: http://org.acme/notthis/northis/oreventhis?buthis=isokay&asis=thispart
      • PATH_INSTANCE

        public static final UrlEncoder PATH_INSTANCE
        Encoder used to encode segments of a path.

        For example: http://org.acme/foo/thispart/orthispart?butnot=thispart
      • HEADER_INSTANCE

        public static final UrlEncoder HEADER_INSTANCE
        Encoder used to encode a header.
    • Constructor Detail

      • UrlEncoder

        protected UrlEncoder​(UrlEncoder.Type type)
        Allow subclass to call constructor.
        Parameters:
        type - encoder type
    • Method Detail

      • encode

        public java.lang.String encode​(java.lang.String s,
                                       java.nio.charset.Charset charset)
        Parameters:
        s - string to encode
        charset - charset to use for encoding
        Returns:
        encoded string
        See Also:
        URLEncoder.encode(String, String)
      • encode

        public java.lang.String encode​(java.lang.String unsafeInput,
                                       java.lang.String charsetName)
        Parameters:
        unsafeInput - string to encode
        charsetName - encoding to use
        Returns:
        encoded string
        See Also:
        URLEncoder.encode(String, String)