Java URL Encode urlEncode(byte[] rs)

Here you can find the source of urlEncode(byte[] rs)

Description

url Encode

License

Apache License

Declaration

public static String urlEncode(byte[] rs) 

Method Source Code

//package com.java2s;
/*//w w  w.j a  v a  2 s.  c o m
 * Copyright (C) 2011 noctarius
 *
 * Licensed 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.
 */

public class Main {
    private static final String HEX_DIGITS = "0123456789ABCDEF";

    public static String urlEncode(byte[] rs) {
        StringBuffer sb = new StringBuffer(rs.length * 2);

        for (int i = 0; i < rs.length; i++) {
            char c = (char) rs[i];

            switch (c) {
            case '_':
            case '.':
            case '*':
            case '-':
            case '/':
                sb.append(c);
                break;

            case ' ':
                sb.append('+');
                break;

            default:
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
                    sb.append(c);
                } else {
                    sb.append('%');
                    sb.append(HEX_DIGITS.charAt((c & 0xF0) >> 4));
                    sb.append(HEX_DIGITS.charAt(c & 0x0F));
                }
            }

        }

        return sb.toString();
    }
}

Related

  1. unpaddedBase64UrlEncoded(final String unencodedString)
  2. uriDecode(String src)
  3. uriDecode(String uri)
  4. uriEncode(String uriRef)
  5. URLEncode(byte[] input)
  6. urlencode(byte[] unencodedBytes)
  7. URLEncode(String in)
  8. urlEncode(String origString)
  9. URLencode(String s)