Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * ====================================================================
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License is distributed on an
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  * KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations
  * under the License.
  * ====================================================================
  *
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
  * <http://www.apache.org/>.
  *
  */
 
 package com.amazonaws.util;
 
 import java.util.List;
 
 
 // Copied and extracted from httpcomponents-client-4.3.6.
A collection of utilities for encoding URLs.

Since:
4.0
 
 class URLEncodedUtils {
     private static final char QP_SEP_A = '&';
     private static final String NAME_VALUE_SEPARATOR = "=";

    
Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Parameters:
parameters The parameters to include.
charset The encoding to use.
Returns:
An application/x-www-form-urlencoded string
 
     public static String format(
             final List <? extends NameValuePairparameters,
             final String charset) {
         return format(parameterscharset);
     }

    
Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Parameters:
parameters The parameters to include.
parameterSeparator The parameter separator, by convention, '&' or ';'.
charset The encoding to use.
Returns:
An application/x-www-form-urlencoded string
Since:
4.3
 
     public static String format(
             final List <? extends NameValuePairparameters,
             final char parameterSeparator,
             final String charset) {
         final StringBuilder result = new StringBuilder();
         for (final NameValuePair parameter : parameters) {
             final String encodedName = encodeFormFields(parameter.getName(), charset);
             final String encodedValue = encodeFormFields(parameter.getValue(), charset);
             if (result.length() > 0) {
                 result.append(parameterSeparator);
             }
             result.append(encodedName);
             if (encodedValue != null) {
                 result.append();
                 result.append(encodedValue);
             }
         }
         return result.toString();
     }

    
Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Parameters:
parameters The parameters to include.
charset The encoding to use.
Returns:
An application/x-www-form-urlencoded string
Since:
4.2
    public static String format(
            final Iterable<? extends NameValuePairparameters,
            final Charset charset) {
        return format(parameterscharset);
    }

    
Returns a String that is suitable for use as an application/x-www-form-urlencoded list of parameters in an HTTP PUT or HTTP POST.

Parameters:
parameters The parameters to include.
parameterSeparator The parameter separator, by convention, '&' or ';'.
charset The encoding to use.
Returns:
An application/x-www-form-urlencoded string
Since:
4.3
    public static String format(
            final Iterable<? extends NameValuePairparameters,
            final char parameterSeparator,
            final Charset charset) {
        final StringBuilder result = new StringBuilder();
        for (final NameValuePair parameter : parameters) {
            final String encodedName = encodeFormFields(parameter.getName(), charset);
            final String encodedValue = encodeFormFields(parameter.getValue(), charset);
            if (result.length() > 0) {
                result.append(parameterSeparator);
            }
            result.append(encodedName);
            if (encodedValue != null) {
                result.append();
                result.append(encodedValue);
            }
        }
        return result.toString();
    }

    
Unreserved characters, i.e. alphanumeric, plus: _ - ! . ~ ' ( ) *

This list is the same as the unreserved list in RFC 2396

    private static final BitSet UNRESERVED   = new BitSet(256);
    
Punctuation characters: , ; : $ & + =

These are the additional characters allowed by userinfo.

    private static final BitSet PUNCT        = new BitSet(256);
    
Characters which are safe to use in userinfo, i.e. UNRESERVED plus PUNCTuation
    private static final BitSet USERINFO     = new BitSet(256);
    
Characters which are safe to use in a path, i.e. UNRESERVED plus PUNCTuation plus /

:
    private static final BitSet PATHSAFE     = new BitSet(256);
    
Characters which are safe to use in a query or a fragment, i.e. RESERVED plus UNRESERVED
    private static final BitSet URIC     = new BitSet(256);

    
Reserved characters, i.e. ;/?:@&=+$,[]

This list is the same as the reserved list in RFC 2396 as augmented by RFC 2732

    private static final BitSet RESERVED     = new BitSet(256);


    
Safe characters for x-www-form-urlencoded data, as per java.net.URLEncoder and browser behaviour, i.e. alphanumeric plus "-", "_", ".", "*"
    private static final BitSet URLENCODER   = new BitSet(256);
    static {
        // unreserved chars
        // alpha characters
        for (int i = 'a'i <= 'z'i++) {
            .set(i);
        }
        for (int i = 'A'i <= 'Z'i++) {
            .set(i);
        }
        // numeric characters
        for (int i = '0'i <= '9'i++) {
            .set(i);
        }
        .set('_'); // these are the charactes of the "mark" list
        .set('-');
        .set('.');
        .set('*');
        .or(); // skip remaining unreserved characters
        .set('!');
        .set('~');
        .set('\'');
        .set('(');
        .set(')');
        // punct chars
        .set(',');
        .set(';');
        .set(':');
        .set('$');
        .set('&');
        .set('+');
        .set('=');
        // Safe for userinfo
        .or();
        .or();
        // URL path safe
        .or();
        .set('/'); // segment separator
        .set(';'); // param separator
        .set(':'); // rest as per list in 2396, i.e. : @ & = + $ ,
        .set('@');
        .set('&');
        .set('=');
        .set('+');
        .set('$');
        .set(',');
        .set(';');
        .set('/');
        .set('?');
        .set(':');
        .set('@');
        .set('&');
        .set('=');
        .set('+');
        .set('$');
        .set(',');
        .set('['); // added by RFC 2732
        .set(']'); // added by RFC 2732
        .or();
        .or();
    }
    private static final int RADIX = 16;
    private static String urlEncode(
            final String content,
            final Charset charset,
            final BitSet safechars,
            final boolean blankAsPlus) {
        if (content == null) {
            return null;
        }
        final StringBuilder buf = new StringBuilder();
        final ByteBuffer bb = charset.encode(content);
        while (bb.hasRemaining()) {
            final int b = bb.get() & 0xff;
            if (safechars.get(b)) {
                buf.append((charb);
            } else if (blankAsPlus && b == ' ') {
                buf.append('+');
            } else {
                buf.append("%");
                final char hex1 = Character.toUpperCase(Character.forDigit((b >> 4) & 0xF, ));
                final char hex2 = Character.toUpperCase(Character.forDigit(b & 0xF, ));
                buf.append(hex1);
                buf.append(hex2);
            }
        }
        return buf.toString();
    }

    
Encode/escape www-url-form-encoded content.

Uses the URLENCODER set of characters, rather than the UNRSERVED set; this is for compatibilty with previous releases, URLEncoder.encode() and most browsers.

Parameters:
content the content to encode, will convert space to '+'
charset the charset to use
Returns:
encoded string
    private static String encodeFormFields(final String contentfinal String charset) {
        if (content == null) {
            return null;
        }
        return urlEncode(contentcharset != null ? Charset.forName(charset) : .true);
    }

    
Encode/escape www-url-form-encoded content.

Uses the URLENCODER set of characters, rather than the UNRSERVED set; this is for compatibilty with previous releases, URLEncoder.encode() and most browsers.

Parameters:
content the content to encode, will convert space to '+'
charset the charset to use
Returns:
encoded string
    private static String encodeFormFields (final String contentfinal Charset charset) {
        if (content == null) {
            return null;
        }
        return urlEncode(contentcharset != null ? charset : .true);
    }

    
Encode a String using the USERINFO set of characters.

Used by URIBuilder to encode the userinfo segment.

Parameters:
content the string to encode, does not convert space to '+'
charset the charset to use
Returns:
the encoded string
    static String encUserInfo(final String contentfinal Charset charset) {
        return urlEncode(contentcharsetfalse);
    }

    
Encode a String using the URIC set of characters.

Used by URIBuilder to encode the query and fragment segments.

Parameters:
content the string to encode, does not convert space to '+'
charset the charset to use
Returns:
the encoded string
    static String encUric(final String contentfinal Charset charset) {
        return urlEncode(contentcharsetfalse);
    }

    
Encode a String using the PATHSAFE set of characters.

Used by URIBuilder to encode path segments.

Parameters:
content the string to encode, does not convert space to '+'
charset the charset to use
Returns:
the encoded string
    static String encPath(final String contentfinal Charset charset) {
        return urlEncode(contentcharsetfalse);
    }
New to GrepCode? Check out our FAQ X