Class QueryStringEncoder

java.lang.Object
io.netty.handler.codec.http.QueryStringEncoder

public class QueryStringEncoder extends java.lang.Object
Creates a URL-encoded URI from a path string and key-value parameter pairs. This encoder is for one time use only. Create a new instance for each URI.
QueryStringEncoder encoder = new QueryStringEncoder("/hello");
encoder.addParam("recipient", "world");
assert encoder.toString().equals("/hello?recipient=world");
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final char[]
     
    private final java.nio.charset.Charset
     
    private boolean
     
    private final java.lang.StringBuilder
     
    private static final byte
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    QueryStringEncoder(java.lang.String uri)
    Creates a new encoder that encodes a URI that starts with the specified path string.
    QueryStringEncoder(java.lang.String uri, java.nio.charset.Charset charset)
    Creates a new encoder that encodes a URI that starts with the specified path string in the specified charset.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addParam(java.lang.String name, java.lang.String value)
    Adds a parameter with the specified name and value to this encoder.
    private void
     
    private static boolean
    Determines whether the given character is a unreserved character.
    private void
    encodeComponent(java.lang.CharSequence s)
     
    private void
    encodeNonUtf8Component(java.lang.CharSequence s)
    Encode the String as per RFC 3986, Section 2.
    private void
    encodeUtf8Component(java.lang.CharSequence s)
     
    private void
    encodeUtf8Component(java.lang.CharSequence s, int encodingStart, int len)
     
    private void
    encodeUtf8ComponentSlow(java.lang.CharSequence s, int start, int len)
     
    private static char
    forDigit(int digit)
    Convert the given digit to a upper hexadecimal char.
    java.lang.String
    Returns the URL-encoded URI which was created from the path string specified in the constructor and the parameters added by addParam(String, String) method.
    java.net.URI
    Returns the URL-encoded URI object which was created from the path string specified in the constructor and the parameters added by addParam(String, String) method.
    private void
    writeUtf8Surrogate(char c, char c2)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • charset

      private final java.nio.charset.Charset charset
    • uriBuilder

      private final java.lang.StringBuilder uriBuilder
    • hasParams

      private boolean hasParams
    • WRITE_UTF_UNKNOWN

      private static final byte WRITE_UTF_UNKNOWN
      See Also:
    • CHAR_MAP

      private static final char[] CHAR_MAP
  • Constructor Details

    • QueryStringEncoder

      public QueryStringEncoder(java.lang.String uri)
      Creates a new encoder that encodes a URI that starts with the specified path string. The encoder will encode the URI in UTF-8.
    • QueryStringEncoder

      public QueryStringEncoder(java.lang.String uri, java.nio.charset.Charset charset)
      Creates a new encoder that encodes a URI that starts with the specified path string in the specified charset.
  • Method Details

    • addParam

      public void addParam(java.lang.String name, java.lang.String value)
      Adds a parameter with the specified name and value to this encoder.
    • encodeComponent

      private void encodeComponent(java.lang.CharSequence s)
    • toUri

      public java.net.URI toUri() throws java.net.URISyntaxException
      Returns the URL-encoded URI object which was created from the path string specified in the constructor and the parameters added by addParam(String, String) method.
      Throws:
      java.net.URISyntaxException
    • toString

      public java.lang.String toString()
      Returns the URL-encoded URI which was created from the path string specified in the constructor and the parameters added by addParam(String, String) method.
      Overrides:
      toString in class java.lang.Object
    • encodeNonUtf8Component

      private void encodeNonUtf8Component(java.lang.CharSequence s)
      Encode the String as per RFC 3986, Section 2.

      There is a little different between the JDK's encode method : URLEncoder.encode(String, String). The JDK's encoder encode the space to + and this method directly encode the blank to %20 beyond that , this method reuse the uriBuilder in this class rather then create a new one, thus generates less garbage for the GC.

      Parameters:
      s - The String to encode
    • encodeUtf8Component

      private void encodeUtf8Component(java.lang.CharSequence s)
      See Also:
    • encodeUtf8Component

      private void encodeUtf8Component(java.lang.CharSequence s, int encodingStart, int len)
    • encodeUtf8ComponentSlow

      private void encodeUtf8ComponentSlow(java.lang.CharSequence s, int start, int len)
    • writeUtf8Surrogate

      private void writeUtf8Surrogate(char c, char c2)
    • appendEncoded

      private void appendEncoded(int b)
    • forDigit

      private static char forDigit(int digit)
      Convert the given digit to a upper hexadecimal char.
      Parameters:
      digit - the number to convert to a character.
      Returns:
      the char representation of the specified digit in hexadecimal.
    • dontNeedEncoding

      private static boolean dontNeedEncoding(char ch)
      Determines whether the given character is a unreserved character.

      unreserved characters do not need to be encoded, and include uppercase and lowercase letters, decimal digits, hyphen, period, underscore, and tilde.

      unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" / "*"

      Parameters:
      ch - the char to be judged whether it need to be encode
      Returns:
      true or false